member_cart.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * 我的购物车
  4. *
  5. *
  6. *
  7. *
  8. */
  9. //use Shopnc\Tpl;
  10. defined('InShopNC') or exit('Access Invalid!');
  11. class member_cartControl extends mobileMemberControl
  12. {
  13. public function __construct() {
  14. parent::__construct();
  15. }
  16. /**
  17. * 购物车列表
  18. */
  19. public function cart_listOp() {
  20. $model_cart = Model('cart');
  21. $model_goods = Model('goods');
  22. $condition = array('buyer_id' => $this->member_info['member_id']);
  23. $cart_list = $model_cart->listCart('db', $condition);
  24. $sum = 0;
  25. foreach ($cart_list as $key => $value)
  26. {
  27. $goods_id = $value['goods_id'];
  28. $goods_promotion = $model_goods->getGoodsInfoAndPromotionById($goods_id);
  29. $goos_storage = $model_goods->getGoodsStorageById($goods_id);
  30. $promotion_type = '';
  31. $promotion_price = '0.00';
  32. if(!empty( $goods_promotion['groupbuy_info'])){
  33. $promotion_type = '抢购';
  34. $promotion_price = $goods_promotion['groupbuy_info']['groupbuy_price'];
  35. }
  36. if(!empty( $goods_promotion['xianshi_info'])){
  37. $promotion_type = '限时折扣';
  38. $promotion_price = $goods_promotion['xianshi_info']['xianshi_price'];
  39. }
  40. $goods_spec = unserialize($goods_promotion['goods_spec']);
  41. $goods_spec_array = array();
  42. foreach ($goods_spec as $val) {
  43. array_push($goods_spec_array, $val);
  44. }
  45. $cart_list[$key]['goos_storage'] = $goos_storage;
  46. $cart_list[$key]['goods_spec'] = implode(',', $goods_spec_array);
  47. $cart_list[$key]['promotion_type'] = $promotion_type;
  48. $cart_list[$key]['promotion_price'] = $promotion_price;
  49. $cart_list[$key]['goods_image_url'] = cthumb($value['goods_image'], $value['store_id']);
  50. $cart_list[$key]['goods_sum'] = ncPriceFormat($value['goods_price'] * $value['goods_num']);
  51. $sum += $cart_list[$key]['goods_sum'];
  52. }
  53. joutput_data(array('cart_list' => $cart_list, 'sum' => ncPriceFormat($sum)));
  54. }
  55. /**
  56. * 购物车添加
  57. */
  58. public function cart_addOp()
  59. {
  60. $goods_id = intval($_POST['goods_id']);
  61. $quantity = intval($_POST['quantity']);
  62. if($goods_id <= 0 || $quantity <= 0) {
  63. joutput_error(errcode::ErrCart, '参数错误');
  64. }
  65. $model_goods = Model('goods');
  66. $model_cart = Model('cart');
  67. $logic_buy_1 = Logic('buy_1');
  68. $goods_info = $model_goods->getGoodsOnlineInfoAndPromotionById($goods_id);
  69. //验证是否可以购买
  70. if(empty($goods_info)) {
  71. joutput_error(errcode::ErrCart, '商品已下架或不存在');
  72. }
  73. //抢购
  74. $logic_buy_1->getGroupbuyInfo($goods_info);
  75. //限时折扣
  76. $logic_buy_1->getXianshiInfo($goods_info,$quantity);
  77. if ($goods_info['store_id'] == $this->member_info['store_id']) {
  78. joutput_error(errcode::ErrCart, '不能购买自己发布的商品');
  79. }
  80. if(intval($goods_info['goods_storage']) < 1 || intval($goods_info['goods_storage']) < $quantity) {
  81. joutput_error(errcode::ErrCart, '库存不足');
  82. }
  83. $param = array();
  84. $param['buyer_id'] = $this->member_info['member_id'];
  85. $param['store_id'] = $goods_info['store_id'];
  86. $param['goods_id'] = $goods_info['goods_id'];
  87. $param['goods_name'] = $goods_info['goods_name'];
  88. $param['goods_price'] = $goods_info['goods_price'];
  89. $param['goods_image'] = $goods_info['goods_image'];
  90. $param['store_name'] = $goods_info['store_name'];
  91. $result = $model_cart->addCart($param, 'db', $quantity);
  92. if($result) {
  93. joutput_data(array('result' => '1'));
  94. } else {
  95. joutput_error(errcode::ErrCart, '收藏失败');
  96. }
  97. }
  98. public function cart_editOp()
  99. {
  100. $cart_list = explode(',', urldecode($_POST['cart_list']));
  101. $new_cart_list= array();
  102. if (is_array($cart_list))
  103. {
  104. foreach ($cart_list as $value)
  105. {
  106. if (preg_match_all('/^(\d{1,10})\|(\d{1,6})$/', $value, $match)) {
  107. if (intval($match[2][0]) > 0) {
  108. $new_cart_list[$match[1][0]] = $match[2][0];
  109. }
  110. }
  111. }
  112. $model_cart = Model('cart');
  113. $condition = array('buyer_id' => $this->member_info['member_id']);
  114. $original_cart_list = $model_cart->listCart('db', $condition);
  115. $quantity_error = array();
  116. foreach ($original_cart_list as $key => $value)
  117. {
  118. if(array_key_exists($value['cart_id'], $new_cart_list))
  119. {
  120. $quantity = intval($new_cart_list[$value['cart_id']]);
  121. if(intval($value['goods_num']) !== $quantity)
  122. {
  123. if(!$this->_check_goods_storage($value, $quantity, $this->member_info['member_id'])) {
  124. array_push($quantity_error, array('cart_id' => $value['cart_id'], 'quantity' => $quantity));
  125. }
  126. else
  127. {
  128. $data = array();
  129. $data['goods_num'] = $quantity;
  130. $update = $model_cart->editCart($data, array('cart_id'=> $value['cart_id']));
  131. if(!$update) {
  132. joutput_error(errcode::ErrCart, '购物车修改失败');
  133. return;
  134. }
  135. }
  136. }
  137. }
  138. else
  139. {
  140. $delete = $model_cart->delCart('db', array('cart_id'=>$value['cart_id']));
  141. if(!$delete) {
  142. joutput_error(errcode::ErrCart, '购物车修改失败');
  143. return;
  144. }
  145. }
  146. }
  147. if(count($quantity_error) > 0) {
  148. joutput_data(array('result'=> '0', 'quantity_error' => $quantity_error));
  149. } else {
  150. joutput_data(array('result' => '1'));
  151. }
  152. }else{
  153. joutput_error(errcode::ErrCart, '购物车列表格式错误');
  154. }
  155. }
  156. /**
  157. * 购物车删除
  158. */
  159. public function cart_delOp()
  160. {
  161. $cart_id = intval($_POST['cart_id']);
  162. $model_cart = Model('cart');
  163. if($cart_id > 0) {
  164. $condition = array();
  165. $condition['buyer_id'] = $this->member_info['member_id'];
  166. $condition['cart_id'] = $cart_id;
  167. $model_cart->delCart('db', $condition);
  168. }
  169. joutput_data(array('result' => '1'));
  170. }
  171. /**
  172. * 更新购物车购买数量
  173. */
  174. public function cart_edit_quantityOp()
  175. {
  176. $cart_id = intval(abs($_POST['cart_id']));
  177. $quantity = intval(abs($_POST['quantity']));
  178. if(empty($cart_id) || empty($quantity)) {
  179. joutput_error(errcode::ErrCart, '参数错误');
  180. }
  181. $model_cart = Model('cart');
  182. $cart_info = $model_cart->getCartInfo(array('cart_id'=>$cart_id, 'buyer_id' => $this->member_info['member_id']));
  183. //检查是否为本人购物车
  184. if($cart_info['buyer_id'] != $this->member_info['member_id']) {
  185. joutput_error(errcode::ErrCart, '参数错误');
  186. }
  187. //检查库存是否充足
  188. if(!$this->_check_goods_storage($cart_info, $quantity, $this->member_info['member_id'])) {
  189. joutput_error(errcode::ErrCart, '库存不足');
  190. }
  191. $data = array();
  192. $data['goods_num'] = $quantity;
  193. $update = $model_cart->editCart($data, array('cart_id'=>$cart_id));
  194. if ($update) {
  195. $return = array();
  196. $return['quantity'] = $quantity;
  197. $return['goods_price'] = ncPriceFormat($cart_info['goods_price']);
  198. $return['total_price'] = ncPriceFormat($cart_info['goods_price'] * $quantity);
  199. joutput_data($return);
  200. } else {
  201. joutput_error(errcode::ErrCart, '修改失败');
  202. }
  203. }
  204. /**
  205. * 检查库存是否充足
  206. */
  207. private function _check_goods_storage($cart_info, $quantity, $member_id)
  208. {
  209. $model_goods= Model('goods');
  210. $model_bl = Model('p_bundling');
  211. $logic_buy_1 = Logic('buy_1');
  212. if ($cart_info['bl_id'] == '0') {
  213. //普通商品
  214. $goods_info = $model_goods->getGoodsOnlineInfoAndPromotionById($cart_info['goods_id']);
  215. //抢购
  216. $logic_buy_1->getGroupbuyInfo($goods_info);
  217. //限时折扣
  218. $logic_buy_1->getXianshiInfo($goods_info,$quantity);
  219. if(intval($goods_info['goods_storage']) < $quantity) {
  220. return false;
  221. }
  222. } else {
  223. //优惠套装商品
  224. $bl_goods_list = $model_bl->getBundlingGoodsList(array('bl_id' => $cart_info['bl_id']));
  225. $goods_id_array = array();
  226. foreach ($bl_goods_list as $goods) {
  227. $goods_id_array[] = $goods['goods_id'];
  228. }
  229. $bl_goods_list = $model_goods->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
  230. //如果有商品库存不足,更新购买数量到目前最大库存
  231. foreach ($bl_goods_list as $goods_info) {
  232. if (intval($goods_info['goods_storage']) < $quantity) {
  233. return false;
  234. }
  235. }
  236. }
  237. return true;
  238. }
  239. }