ApicartController.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Home\Controller;
  15. class ApicartController extends CommonController {
  16. //结算
  17. function checkout(){
  18. $buy_type = I('get.buy_type','dan');
  19. //undefined
  20. if($buy_type == 'undefined')
  21. {
  22. $buy_type = 'dan';
  23. }
  24. $token = I('get.token');
  25. $voucher_id = I('get.voucher_id',0);
  26. $use_quan_str = I('get.use_quan_str','');
  27. $use_quan_arr = array();
  28. if($use_quan_str != '')
  29. {
  30. $use_quan_arr_tmp = explode('@',$use_quan_str );
  31. foreach($use_quan_arr_tmp as $val)
  32. {
  33. $tmp_arr = explode('_', $val);
  34. $use_quan_arr[$tmp_arr[0]] = $tmp_arr[1];
  35. }
  36. }
  37. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  38. $member_id = $weprogram_token['member_id'];
  39. if( empty($member_id) )
  40. {
  41. //需要登录
  42. echo json_encode( array('code' =>5) );
  43. die();
  44. }
  45. $cart=new \Lib\Cart();
  46. if ((!$cart->has_goodswecar($buy_type,$token) ) ) {
  47. //购物车中没有商品
  48. echo json_encode( array('code' =>4) );
  49. die();
  50. }
  51. $member_info = M('member')->where( array('member_id' => $member_id) )->find();
  52. $goods=$cart->get_all_goodswecar($buy_type, $token);
  53. //var_dump($goods);die();
  54. $add_where = array('member_id'=>$member_id );
  55. $address = M('address')->where( $add_where )->order('is_default desc,address_id desc')->find();
  56. if($address){
  57. $province_info = M('area')->field('area_name')->where( array('area_id' => $address['province_id']) )->find();
  58. $city_info = M('area')->field('area_name')->where( array('area_id' => $address['city_id']) )->find();
  59. $country_info = M('area')->field('area_name')->where( array('area_id' => $address['country_id']) )->find();
  60. $address['province_name'] = $province_info['area_name'];
  61. $address['city_name'] = $city_info['area_name'];
  62. $address['country_name'] = $country_info['area_name'];
  63. }
  64. $seller_goodss = array();
  65. $show_voucher = 0;
  66. //var_dump($goods);die();
  67. foreach($goods as $key => $val)
  68. {
  69. $goods_store_field = M('goods')->field('store_id')->where( array('goods_id' => $val['goods_id']) )->find();
  70. $seller_goodss[ $goods_store_field['store_id'] ]['goods'][$key] = $val;
  71. }
  72. $quan_model = D('Home/Voucher');
  73. $pin_model = D('Home/Pin');
  74. $voucher_price = 0;
  75. $is_pin_over = 0;
  76. //计算优惠券
  77. foreach($seller_goodss as $store_id => $val)
  78. {
  79. //total
  80. $seller_voucher_list = array();
  81. $seller_total_fee = 0;
  82. $total_trans_free = 0;
  83. $is_no_quan = false;
  84. foreach($val['goods'] as $kk =>$d_goods)
  85. {
  86. $seller_total_fee += $d_goods['total'];
  87. //'pin_id' => $pin_id,
  88. if($buy_type == 'pin' && $d_goods['pin_id'] > 0)
  89. {
  90. $is_pin_over = $pin_model->getNowPinState($d_goods['pin_id']);
  91. }
  92. //$d_goods['goods_id']
  93. $tp_goods_info = M('goods')->field('type')->where( array('goods_id' => $d_goods['goods_id']) )->find();
  94. if($tp_goods_info['type'] == 'integral')
  95. {
  96. $is_no_quan = true;
  97. }
  98. if($d_goods['shipping']==1)
  99. {
  100. //统一运费
  101. $d_goods[$kk]['trans_free'] = $d_goods['goods_freight'];
  102. }else {
  103. //运费模板
  104. if(!empty($address))
  105. {
  106. $trans_free = D('Home/Transport')->calc_transport($d_goods['transport_id'], $d_goods['quantity']*$d_goods['weight'], $address['city_id'] );
  107. }else{
  108. $trans_free = 0;
  109. }
  110. $d_goods[$kk]['trans_free'] = $trans_free;
  111. }
  112. $total_trans_free += $d_goods[$kk]['trans_free'];
  113. $val['goods'][$kk] = $d_goods;
  114. }
  115. $chose_vouche = array();
  116. $show_voucher = 0;
  117. if(!$is_no_quan)
  118. {
  119. $vouche_list = $quan_model->get_user_canpay_voucher($member_id,$store_id,$seller_total_fee);
  120. if(!empty($vouche_list) && empty($use_quan_arr) ) {
  121. $show_voucher = 1;
  122. reset($vouche_list);
  123. $chose_vouche = current($vouche_list);
  124. $voucher_price += $chose_vouche['credit'];
  125. //credit
  126. $seller_total_fee = round( $seller_total_fee - $chose_vouche['credit'], 2);
  127. }else if( !empty($vouche_list) && !empty($use_quan_arr) )
  128. {
  129. //var_dump($use_quan_arr);die();
  130. foreach($vouche_list as $tmp_voucher)
  131. {
  132. if($tmp_voucher['id'] == $use_quan_arr[$store_id])
  133. {
  134. $show_voucher = 1;
  135. $chose_vouche = $tmp_voucher;
  136. $seller_total_fee = round( $seller_total_fee - $chose_vouche['credit'], 2);
  137. $voucher_price += $chose_vouche['credit'];
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. $val['chose_vouche'] = $chose_vouche;
  144. $val['show_voucher'] = $show_voucher;
  145. $val['voucher_list'] = $vouche_list;
  146. $val['total'] = $seller_total_fee;
  147. if($val['total'] < 0)
  148. {
  149. $val['total'] = 0;
  150. }
  151. $val['trans_free'] = $total_trans_free;
  152. //trans_free
  153. //seller/2017-10-06/59d74b0611381.png
  154. $store_info = M('seller')->field('s_id,s_true_name,s_logo')->where( array('s_id' => $store_id) )->find();
  155. $store_info['s_logo'] = C('SITE_URL').'Uploads/image/'.$store_info['s_logo'];
  156. $val['store_info'] = $store_info;
  157. //unset($val['goods']);
  158. $seller_goodss[$store_id] = $val;
  159. }
  160. $trans_free_toal = 0;//运费
  161. //D('Transport')->calc_transport($v['id'], $goods_data['quantity'], $address['city_id'] );
  162. $total_free = 0;
  163. $is_ziti = 0;
  164. //is_ziti: res.data.is_ziti,
  165. // pick_up_arr: res.data.pick_up_arr,
  166. $pick_up_arr = array();
  167. foreach($goods as $key => $good)
  168. {
  169. //goods_id
  170. $goods_info = M('goods')->field('pick_just,pick_up,store_id')->where( array('goods_id' => $good['goods_id']) )->find();
  171. if($goods_info['pick_just'] >= 1)
  172. {
  173. $pick_up = $goods_info['pick_up'];
  174. $is_ziti = $goods_info['pick_just'];
  175. }
  176. if($good['shipping']==1)
  177. {
  178. //统一运费
  179. $trans_free_toal += $good['goods_freight'];
  180. $goods[$key]['trans_free'] = $good['goods_freight'];
  181. }else {
  182. //运费模板
  183. if(!empty($address))
  184. {
  185. $trans_free = D('Home/Transport')->calc_transport($good['transport_id'], $good['quantity']*$good['weight'], $address['city_id'] );
  186. }else{
  187. $trans_free = 0;
  188. }
  189. $goods[$key]['trans_free'] = $trans_free;
  190. $trans_free_toal +=$trans_free;
  191. }
  192. $total_free += $good['total'];
  193. }
  194. if(!empty($pick_up))
  195. {
  196. $pick_up = unserialize($pick_up);
  197. $pick_up_ids = implode(',',$pick_up);
  198. $pick_up_arr = M('pick_up')->where( array('id'=>array('in',$pick_up_ids)) )->select();
  199. }
  200. //$this->trans_free_toal = $trans_free_toal;
  201. // $this->goods = $goods;
  202. $pick_up_name = '';
  203. $pick_up_mobile = '';
  204. if($is_ziti >= 1)
  205. {
  206. //寻找上一个订单的自提电话 自提姓名
  207. //delivery = pickup member_id
  208. $last_order_info = M('order')->where( array('member_id' => $member_id,'delivery' => 'pickup') )->order('order_id desc')->find();
  209. if(!empty($last_order_info))
  210. {
  211. $pick_up_name = $last_order_info['shipping_name'];
  212. $pick_up_mobile = $last_order_info['telephone'];
  213. }
  214. }
  215. $need_data = array();
  216. $need_data['code'] = 1;
  217. $need_data['is_pin_over'] = $is_pin_over;
  218. $need_data['is_integer'] = $is_no_quan ? 1: 0;
  219. $need_data['pick_up_arr'] = $pick_up_arr;
  220. $need_data['is_ziti'] = $is_ziti;
  221. $need_data['ziti_name'] = $pick_up_name;
  222. $need_data['ziti_mobile'] = $pick_up_mobile;
  223. $need_data['seller_goodss'] = $seller_goodss;
  224. $need_data['show_voucher'] = $show_voucher;
  225. $need_data['buy_type'] = $buy_type;
  226. $need_data['address'] = $address;
  227. $need_data['trans_free_toal'] = $trans_free_toal;
  228. //https://mall.shiziyu888.com/dan/index.php?s=/Apicart/checkout/token/73853c285fe7354fe8008306fe15a1bb/buy_type/pin/dispatching/pickup
  229. //https://mall.shiziyu888.com/dan/index.php?s=/Apicart/checkout/token/73853c285fe7354fe8008306fe15a1bb/buy_type/pin/dispatching/express
  230. $dispatching = I('get.dispatching','express');//pickup dispatching/express
  231. //is_ziti == 2
  232. if($dispatching == 'express')
  233. {
  234. $need_data['total_free'] = $total_free + $trans_free_toal - $voucher_price;
  235. }else{
  236. $need_data['total_free'] = $total_free - $voucher_price;
  237. }
  238. if($is_ziti == 2)
  239. {
  240. $need_data['total_free'] = $total_free - $voucher_price;
  241. }
  242. if($need_data['total_free'] < 0)
  243. {
  244. $need_data['total_free'] = 0;
  245. }
  246. //判断是否可以余额支付
  247. //member_info account_money
  248. $is_yue_open_info = M('config')->where( array('name' => 'is_yue_open') )->find();
  249. $is_yue_open = $is_yue_open_info['value'];
  250. $need_data['is_yue_open'] = $is_yue_open;
  251. $need_data['can_yupay'] = 0;
  252. if($is_yue_open == 1 && $need_data['total_free'] >=0 && $member_info['account_money'] >= $need_data['total_free'])
  253. {
  254. $need_data['can_yupay'] = 1;
  255. }
  256. //member_info yu_money account_money
  257. $need_data['yu_money'] = $member_info['account_money'];
  258. $need_data['goods'] = $goods;
  259. echo json_encode($need_data);
  260. die();
  261. //$this->display('checkout');
  262. }
  263. public function get_user_pay_voucher()
  264. {
  265. //{store_id:store_id,total_free:total_free}
  266. $voucher_id = I('post.voucher_id',0);
  267. $store_id = I('post.store_id');
  268. $total_free = I('post.total_free');
  269. $store_info = M('seller')->where( array('s_id' => $store_id) )->find();
  270. $this->store_info = $store_info;
  271. $this->voucher_id = $voucher_id;
  272. $result = array('code' => '0');
  273. $quan_model = D('Home/Voucher');
  274. $user_vouche_list = $quan_model->get_user_canpay_voucher(is_login(),$store_id,$total_free);
  275. // $user_vouche_list = array();
  276. $this->user_vouche_list = $user_vouche_list;
  277. $where = "total_count>send_count and ( store_id ={$store_id} or store_id = 0 ) and is_index_show=1 and end_time>".time();
  278. $quan_list = M('voucher')->where($where)->order('add_time desc')->limit(8)->select();
  279. $this->quan_list = $quan_list;
  280. $html = $this->fetch('Cart:voucher_ajax_fetch');
  281. $result['html'] = $html;
  282. if(!empty($user_vouche_list)) {
  283. $result['code'] = 1;
  284. }else if(!empty($quan_list)) {
  285. // $result['code'] = 2;
  286. }
  287. echo json_encode($result);
  288. die();
  289. }
  290. //显示购物车中商品列表
  291. function show_cart_goods(){
  292. $buy_type = I('get.buy_type','dan');
  293. $token = I('get.token');
  294. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  295. $member_id = $weprogram_token['member_id'];
  296. if( empty($member_id) )
  297. {
  298. //需要登录
  299. echo json_encode( array('code' =>5) );
  300. die();
  301. }
  302. $cart=new \Lib\Cart();
  303. $goods=$cart->get_all_goodswecar($buy_type, $token, 0);
  304. //$cart = M('car')->where("token = '{$token}' and carkey like 'cart.%' ")->select();
  305. //$goods=$cart->get_all_goodswecar($buy_type, $token, 0); singledel
  306. $seller_goodss = array();
  307. foreach($goods as $key => $val)
  308. {
  309. $goods_store_field = M('goods')->field('store_id')->where( array('goods_id' => $val['goods_id']) )->find();
  310. $seller_goodss[ $goods_store_field['store_id'] ]['goods'][$key] = $val;
  311. }
  312. $ck_goodstype_count = 0;
  313. foreach($seller_goodss as $store_id => $val)
  314. {
  315. //total
  316. $seller_voucher_list = array();
  317. $seller_total_fee = 0;
  318. $total_trans_free = 0;
  319. $tmp_goods = array();
  320. $is_store_ck = false;
  321. foreach($val['goods'] as $kk =>$d_goods)
  322. {
  323. $seller_total_fee += $d_goods['total'];
  324. $total_trans_free += $d_goods[$kk]['trans_free'];
  325. $val['goods'][$kk] = $d_goods;
  326. $tp_val = array();
  327. $tp_val['id'] = $d_goods['goods_id'];
  328. $tp_val['key'] = $d_goods['key'];
  329. if($d_goods['singledel'] == 1)
  330. {
  331. $tp_val['isselect'] = true;
  332. $is_store_ck = true;
  333. $ck_goodstype_count++;
  334. } else {
  335. $tp_val['isselect'] = false;
  336. }
  337. $tp_val['imgurl'] = $d_goods['image'];
  338. $tp_val['edit'] = 'inline';
  339. $tp_val['title'] = $d_goods['name'];
  340. $tp_val['finish'] = 'none';
  341. $tp_val['description'] = 'description';
  342. $option_arr = array();
  343. $option_str = "";
  344. foreach($d_goods['option'] as $option_val)
  345. {
  346. $option_arr[] = $option_val['name'].':'.$option_val['value'];
  347. }
  348. if(!empty($option_arr))
  349. {
  350. $option_str = implode(',', $option_arr);
  351. }
  352. $tp_val['goodstype'] = $option_str;
  353. $tp_val['goodstypeedit'] = $option_str;
  354. $tp_val['goodsnum'] = $d_goods['quantity'];
  355. $tp_val['max_quantity'] = $d_goods['max_quantity'];
  356. $tp_val['cartype'] = 'inline';
  357. $tp_val['currntprice'] = $d_goods['price'];
  358. $tp_val['price'] = $d_goods['shop_price'];
  359. $tmp_goods[] = $tp_val;
  360. }
  361. $store_info = M('seller')->field('s_id,s_true_name,s_logo')->where( array('s_id' => $store_id) )->find();
  362. $store_info['s_logo'] = C('SITE_URL').'Uploads/image/'.$store_info['s_logo'];
  363. $val['store_info'] = $store_info;
  364. $store_data = array();
  365. $store_data['id'] = $store_info['s_id'];
  366. if($is_store_ck)
  367. {
  368. $store_data['isselect'] = true;
  369. } else {
  370. $store_data['isselect'] = false;
  371. }
  372. $store_data['shopname'] = $store_info['s_true_name'];
  373. $store_data['caredit'] = 'inline';
  374. $store_data['finish'] = 'none';
  375. $store_data['count'] = '0.00';
  376. $store_data['goodstype'] = 2;
  377. $store_data['goodstypeselect'] = 0;
  378. $store_data['shopcarts'] = $tmp_goods;
  379. $seller_goodss[$store_id] = $store_data;
  380. $i++;
  381. }
  382. $need_data = array();
  383. $need_data['code'] = 0;
  384. $need_data['carts'] = $seller_goodss;
  385. echo json_encode( $need_data );
  386. die();
  387. }
  388. //更新购物车商品数量
  389. function update_quantity(){
  390. $d=I('post.');
  391. $cart=new \Lib\Cart();
  392. $key=$d['cart_key'];
  393. $goods_id = explode(':',$key);
  394. $goods_id = $goods_id[0];
  395. $quantity = $d['quantity'];
  396. $data =session('cart.'.$key);
  397. //'sku_str'=>$data['sku_str']
  398. $goods=M('goods')->find($goods_id);
  399. $json = array('code' =>0);
  400. //商品存在
  401. if($goods){
  402. if($goods['quantity']<$quantity){
  403. $json['msg']='商品数量不足,剩余'.$goods['quantity'].'个!!';
  404. echo json_encode($json);
  405. die();
  406. }
  407. $option = array();
  408. if( !empty($data['sku_str'])){
  409. $option = explode('_', $data['sku_str']);
  410. }
  411. $json=array('code' =>0);
  412. $goods_model = D('Home/Goods');
  413. $goods_quantity=$cart->get_goods_quantity($goods_id);
  414. if($goods_quantity<$quantity){
  415. $json['code'] =3;
  416. $json['msg']='商品数量不足,剩余'.$goods_quantity.'个!!';
  417. echo json_encode($json);
  418. die();
  419. }
  420. $goods_options=$goods_model->get_goods_options($goods_id);
  421. if(!empty($option))
  422. {
  423. $mul_opt_arr = array();
  424. $goods_option_mult_value = M('goods_option_mult_value')->where( array('rela_goodsoption_valueid' => $data['sku_str'],'goods_id'=>$goods_id) )->find();
  425. if( !empty($goods_option_mult_value) )
  426. {
  427. if($goods_option_mult_value['quantity']<$quantity){
  428. $json['code'] =3;
  429. $json['msg']='商品数量不足,剩余'.$goods_option_mult_value['quantity'].'个!!';
  430. echo json_encode($json);
  431. die();
  432. }
  433. }
  434. }
  435. $cart->update($key,$quantity);
  436. $goods_list=$cart->get_all_goods();
  437. $squantity=0;
  438. $total=0;
  439. $total_all_price=0;
  440. $weight = 0;
  441. $w=new \Lib\Weight();
  442. foreach ($goods_list as $k => $v) {
  443. $squantity += $v['quantity'];
  444. $goods[$k] = $v;
  445. $total_all_price+=$v['total'];
  446. if($v['key'] ==$key)
  447. {
  448. $price = $v['price'];
  449. }
  450. if ($v['shipping']) {
  451. $weight += $w->convert($v['weight'], $v['weight_class_id'],C('WEIGHT_ID'));
  452. }
  453. }
  454. //商品数量
  455. session('cart_total',$squantity);
  456. $json['code'] =1;
  457. $json['cart_total']=$squantity;
  458. //商品单价
  459. $json['price']=$price;
  460. //单个商品总价
  461. $json['total_price']=$price * $quantity;
  462. //所有商品总价
  463. $json['total_all_price']=$total_all_price;
  464. //所有商品重量
  465. $json['weight']=$weight;
  466. echo json_encode($json);
  467. die();
  468. }
  469. }
  470. //删除商品
  471. function remove(){
  472. $cart=new \Lib\Cart();
  473. $cart_key = I('post.cart_key');
  474. $cart->remove($cart_key);
  475. $goods_list=$cart->get_all_goods();
  476. $squantity=0;
  477. $total=0;
  478. $total_all_price=0;
  479. $weight = 0;
  480. $w=new \Lib\Weight();
  481. foreach ($goods_list as $k => $v) {
  482. $squantity += $v['quantity'];
  483. $goods[$k] = $v;
  484. $total_all_price+=$v['total'];
  485. if ($v['shipping']) {
  486. $weight += $w->convert($v['weight'], $v['weight_class_id'],C('WEIGHT_ID'));
  487. }
  488. }
  489. $json = array();
  490. //商品数量
  491. session('cart_total',$squantity);
  492. $json['code'] =1;
  493. $json['cart_total']=$squantity;
  494. //所有商品总价
  495. $json['total_all_price']=$total_all_price;
  496. //所有商品重量
  497. $json['weight']=$weight;
  498. echo json_encode($json);
  499. die();
  500. }
  501. /**
  502. * 清空购物车
  503. * 拼团等这种无需购物车的逻辑使用
  504. */
  505. function clear_all_cart()
  506. {
  507. $cart=new \Lib\Cart();
  508. $cart->removeAll();
  509. $total=$cart->count_goods();
  510. session('cart_total',$total);
  511. }
  512. public function del_car_goods()
  513. {
  514. $token = I('get.token');
  515. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  516. $member_id = $weprogram_token['member_id'];
  517. $data_json = file_get_contents('php://input');
  518. $data = json_decode($data_json, true);
  519. $carkey = $data['carkey'];//I('post.car_key');
  520. $all_cart = M('car')->where("token = '{$token}' and carkey = '{$carkey}' ")->delete();
  521. echo json_encode( array('code' => 0) );
  522. die();
  523. }
  524. public function clear_dan_cars()
  525. {
  526. $token = I('get.token');
  527. $all_cart = M('car')->where("token = '{$token}' and carkey like 'cart.%' ")->select();
  528. if(!empty($all_cart))
  529. {
  530. foreach($all_cart as $val)
  531. {
  532. $tmp_format_data = unserialize($val['format_data']);
  533. if($tmp_format_data['singledel'] == 1)
  534. {
  535. M('car')->where( array('id' => $val['id']) )->delete();
  536. }
  537. }
  538. }
  539. echo json_encode( array('code' => 0) );
  540. die();
  541. }
  542. /**
  543. 检测是否限购商品
  544. **/
  545. public function check_car_buy_count()
  546. {
  547. }
  548. public function checkout_flushall()
  549. {
  550. $token = I('get.token');
  551. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  552. $member_id = $weprogram_token['member_id'];
  553. $data_json = file_get_contents('php://input');
  554. $data = json_decode($data_json, true);
  555. $car_key = $data['car_key'];//I('post.car_key');
  556. $all_keys_arr = $data['all_keys_arr'];
  557. $save_keys = array();
  558. foreach($all_keys_arr as $val)
  559. {
  560. $tmp_val = explode('_', $val);
  561. $save_keys[ $tmp_val[0] ] = $tmp_val[1];
  562. }
  563. $all_cart = M('car')->where("token = '{$token}' and carkey like 'cart.%' ")->select();
  564. if(!empty($all_cart))
  565. {
  566. foreach($all_cart as $val)
  567. {
  568. $tmp_format_data = unserialize($val['format_data']);
  569. $tmp_format_data['singledel'] = 0;
  570. $tmp_format_data['quantity'] = $save_keys[$val['carkey']];
  571. //检测库存是否足够
  572. M('car')->where( array('id' => $val['id']) )->save( array('format_data' => serialize($tmp_format_data) ) );
  573. }
  574. }
  575. foreach( $car_key as $key )
  576. {
  577. $car_info = M('car')->where( array('carkey' => $key,'token' => $token) )->find();
  578. if( !empty($car_info) )
  579. {
  580. $tmp_format_data = unserialize($car_info['format_data']);
  581. $tmp_format_data['singledel'] = 1;
  582. $quantity = $tmp_format_data['quantity'];
  583. $goods_id = $tmp_format_data['goods_id'];
  584. $sku_str = $tmp_format_data['sku_str'];
  585. $check_json = $this->_check_can_buy($member_id, $goods_id,$quantity);
  586. if($check_json['code'] != 0)
  587. {
  588. $tmp_format_data['quantity'] = $check_json['count'];
  589. M('car')->where( array('id' => $car_info['id']) )->save( array('format_data' => serialize($tmp_format_data) ) );
  590. echo json_encode( array('code' => 6,'msg' => $check_json['msg']) );
  591. die();
  592. }
  593. $check_json = $this->_check_goods_quantity($goods_id,$quantity,$sku_str);
  594. //var_dump($check_json);die();
  595. if($check_json['code'] != 0)
  596. {
  597. echo json_encode( array('code' => 6,'msg' => $check_json['msg']) );
  598. die();
  599. }
  600. //_check_goods_quantity($goods_id,$quantity,$sku_str) sku_str
  601. M('car')->where( array('id' => $car_info['id']) )->save( array('format_data' => serialize($tmp_format_data) ) );
  602. }
  603. }
  604. echo json_encode( array('code' => 0) );
  605. die();
  606. }
  607. private function _check_can_buy($member_id, $goods_id,$quantity)
  608. {
  609. $goods_model = D('Home/Goods');
  610. $can_buy_count = $goods_model->check_goods_user_canbuy_count($member_id, $goods_id);
  611. $goods_info = M('goods')->field('name')->where( array('goods_id' => $goods_id) )->find();
  612. $goods_description = M('goods_description')->field('per_number')->where( array('goods_id' => $goods_id) )->find();
  613. //per_number
  614. $json = array();
  615. if($can_buy_count == -1)
  616. {
  617. $json['code'] =6;
  618. $json['msg']=mb_substr($goods_info['name'],0,4,'utf-8').'...,每人最多购买'.$goods_description['per_number'].'个哦';
  619. }else if($can_buy_count >0 && $quantity >$can_buy_count)
  620. {
  621. $json['code'] =6;
  622. $json['msg']=mb_substr($goods_info['name'],0,4,'utf-8').'...,您还能购买'.$can_buy_count.'份';
  623. $json['count']=$can_buy_count;
  624. }else{
  625. $json['code'] = 0;
  626. }
  627. return $json;
  628. }
  629. public function _check_goods_quantity($goods_id,$quantity,$sku_str)
  630. {
  631. $cart=new \Lib\Cart();
  632. $goods_model = D('Home/Goods');
  633. $goods_info = M('goods')->field('name')->where( array('goods_id' => $goods_id) )->find();
  634. $goods_quantity=$cart->get_goods_quantity($goods_id);
  635. $json = array('code' => 0);
  636. if($goods_quantity<$quantity){
  637. $json['code'] =3;
  638. $json['msg']= mb_substr($goods_info['name'],0,4,'utf-8').'...,商品数量不足,剩余'.$goods_quantity.'个!!';
  639. }else if(!empty($sku_str))
  640. {
  641. $mul_opt_arr = array();
  642. $goods_option_mult_value = M('goods_option_mult_value')->where( array('rela_goodsoption_valueid' => $sku_str,'goods_id'=>$goods_id) )->find();
  643. if( !empty($goods_option_mult_value) )
  644. {
  645. if($goods_option_mult_value['quantity']<$quantity+$cart_goods_quantity){
  646. $json['code'] =3;
  647. $json['msg']=mb_substr($goods_info['name'],0,4,'utf-8').'...,商品数量不足,剩余'.$goods_option_mult_value['quantity'].'个!!';
  648. }
  649. }
  650. }
  651. return $json;
  652. }
  653. //加入购物车
  654. function add(){
  655. $data_json = file_get_contents('php://input');
  656. $data = json_decode($data_json, true);
  657. //$data['buy_type'] == 'dan'
  658. if( !isset($data['buy_type']) || empty($data['buy_type']) )
  659. {
  660. $data['buy_type'] = 'dan';
  661. }
  662. $token = I('get.token');
  663. $weprogram_token = M('weprogram_token')->field('member_id')->where( array('token' =>$token) )->find();
  664. $member_id = $weprogram_token['member_id'];
  665. $is_just_addcar = empty($data['is_just_addcar']) ? 0: 1;
  666. $goods_id = $data['goods_id'];
  667. if( empty($member_id))
  668. {
  669. $result = array('code' =>4);
  670. echo json_encode($result);
  671. die();
  672. }
  673. if (isset($data['goods_id'])) {
  674. $goods_id = $data['goods_id'];
  675. } else {
  676. $goods_id = 0;
  677. }
  678. $product=M('goods')->find($goods_id);
  679. if( $product['status'] != 1)
  680. {
  681. $json['code'] =6;
  682. $json['msg']='商品已下架!';
  683. echo json_encode($json);
  684. die();
  685. }
  686. //判断是否积分兑换
  687. //type integral
  688. if( $product['type'] == 'integral')
  689. {
  690. //判断积分是否足够 member_id
  691. $integral_model = D('Seller/Integral');
  692. $check_result = $integral_model->check_user_score_can_pay($member_id, $data['sku_str'], $goods_id);
  693. if( $check_result['code'] == 1 )
  694. {
  695. $json['code'] =6;
  696. $json['msg']='剩余'.$check_result['cur_score'].'积分,积分不足!';
  697. echo json_encode($json);
  698. die();
  699. }
  700. }
  701. //6
  702. if($is_just_addcar == 1)
  703. {
  704. if($product['pick_just'] > 0)
  705. {
  706. $json['code'] =6;
  707. $json['msg']='自提商品,请立即购买';
  708. echo json_encode($json);
  709. die();
  710. }
  711. }
  712. //商品存在
  713. if($product){
  714. $cart=new \Lib\Cart();
  715. if (isset($data['quantity'])) {
  716. $quantity = $data['quantity'];
  717. } else {
  718. $quantity = 1;
  719. }
  720. $option = array();
  721. if( !empty($data['sku_str'])){
  722. $option = explode('_', $data['sku_str']);
  723. }
  724. $cart_goods_quantity = $cart->get_wecart_goods($goods_id,$data['sku_str'] ,$token);
  725. $json=array('code' =>0);
  726. $goods_model = D('Home/Goods');
  727. $goods_quantity=$cart->get_goods_quantity($goods_id);
  728. //检测商品限购 6
  729. $can_buy_count = $goods_model->check_goods_user_canbuy_count($member_id, $goods_id);
  730. $goods_description = M('goods_description')->field('per_number')->where( array('goods_id' => $goods_id) )->find();
  731. if($can_buy_count == -1)
  732. {
  733. $json['code'] =6;
  734. //$json['msg']='已经不能再买了';
  735. $json['msg']='每人最多购买'.$goods_description['per_number'].'个哦';
  736. echo json_encode($json);
  737. die();
  738. }else if($can_buy_count >0 && $quantity >$can_buy_count)
  739. {
  740. $json['code'] =6;
  741. $json['msg']='您还能购买'.$can_buy_count.'份';
  742. echo json_encode($json);
  743. die();
  744. }
  745. //已加入购物车的总数
  746. if($goods_quantity<$quantity+$cart_goods_quantity){
  747. $json['code'] =3;
  748. $json['msg']='商品数量不足,剩余'.$goods_quantity.'个!!';
  749. echo json_encode($json);
  750. die();
  751. }
  752. $goods_options=$goods_model->get_goods_options($goods_id);
  753. if(!empty($option))
  754. {
  755. $mul_opt_arr = array();
  756. $goods_option_mult_value = M('goods_option_mult_value')->where( array('rela_goodsoption_valueid' => $data['sku_str'],'goods_id'=>$goods_id) )->find();
  757. if( !empty($goods_option_mult_value) )
  758. {
  759. if($goods_option_mult_value['quantity']<$quantity+$cart_goods_quantity){
  760. $json['code'] =3;
  761. $json['msg']='商品数量不足,剩余'.$goods_option_mult_value['quantity'].'个!!';
  762. echo json_encode($json);
  763. die();
  764. }
  765. }
  766. }
  767. //buy_type
  768. // $this->clear_all_cart();
  769. $format_data_array = array('quantity' => $quantity,'goods_id' => $goods_id,'sku_str'=>$data['sku_str'],'buy_type' =>$data['buy_type']);
  770. //区分活动商品还是普通商品。做两个购物车,活动商品是需要直接购买的,单独购买商品加入正常的购物车TODO....
  771. //is_just_addcar 0 1
  772. if($data['buy_type'] == 'dan' && $is_just_addcar == 0)
  773. {
  774. //$cart->removedancar($token);
  775. //清空一下购物车
  776. //singledel
  777. $format_data_array['is_just_addcar'] = 0;
  778. $format_data_array['singledel'] = 1;
  779. $cart->addwecar($token,$goods_id,$format_data_array,$data['sku_str']);
  780. $total=$cart->count_goodscar($token);
  781. }
  782. else if($data['buy_type'] == 'dan' && $is_just_addcar == 1)
  783. {
  784. //singledel
  785. $format_data_array['is_just_addcar'] = 1;
  786. $format_data_array['singledel'] = 1;
  787. $cart->addwecar($token,$goods_id,$format_data_array,$data['sku_str']);
  788. $total=$cart->count_goodscar($token);
  789. }
  790. else {
  791. //buy_type:pin 活动购物车。
  792. $pin_id = isset($data['pin_id']) ? $data['pin_id'] : 0;
  793. $pin_goods = M('pin_goods')->where(array('goods_id' => $goods_id))->find();
  794. //lottery
  795. if( $pin_goods['type'] == 'lottery' && $product['type'] == 'lottery' )
  796. {
  797. $now_time = time();
  798. $lottery_goods_info = M('lottery_goods')->where( array('goods_id' => $goods_id) )->find();
  799. if($lottery_goods_info['end_time'] < $now_time)
  800. {
  801. $json['code'] =6;
  802. $json['msg']='抽奖活动已结束';
  803. echo json_encode($json);
  804. die();
  805. }
  806. }
  807. //检测商品是否老带新,新人才能参团
  808. if($pin_id > 0 )
  809. {
  810. if($pin_goods['type'] == 'newman')
  811. {
  812. $new_mamn_buy = $goods_model->check_goods_new_manbug($member_id);
  813. if($new_mamn_buy>0)
  814. {
  815. $json['code'] =5;
  816. $json['msg']='该商品只能新人参团';
  817. echo json_encode($json);
  818. die();
  819. }
  820. }
  821. }
  822. $format_data_array['pin_id'] = $pin_id;
  823. $cart->add_activitycar($token, $goods_id,$format_data_array,$data['sku_str']);
  824. $total=$cart->count_activitycar($token);
  825. }
  826. $carts = M('car')->where( "token = '{$token}' and carkey ='cart_total'" )->find();
  827. if( !empty($carts) ) {
  828. M('car')->where( "token = '{$token}' and carkey ='cart_total'" )->save( array('format_data' => $total) );
  829. } else{
  830. M('car')->add( array( 'token' => $token,'carkey' =>'cart_total', 'format_data' => $total) );
  831. }
  832. //session('cart_total',$total);
  833. $json ['code'] = 1;
  834. if( $data['buy_type'] != 'dan' )
  835. {
  836. $json ['code'] = 2;
  837. }
  838. $json['success']='成功加入购物车!!';
  839. $json['total']=$total;
  840. echo json_encode($json);
  841. die();
  842. }
  843. }
  844. }