Cart.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\goods\Cart as CartModel;
  4. use app\model\goods\Goods;
  5. class Cart extends BaseApi
  6. {
  7. /**
  8. * 添加信息
  9. */
  10. public function add()
  11. {
  12. $token = $this->checkToken();
  13. if ($token[ 'code' ] < 0) return $this->response($token);
  14. $sku_id = isset($this->params[ 'sku_id' ]) ? $this->params[ 'sku_id' ] : 0;
  15. $num = isset($this->params[ 'num' ]) ? $this->params[ 'num' ] : 0;
  16. $form_data = $this->params[ 'form_data' ] ?? '';
  17. if (empty($sku_id)) {
  18. return $this->response($this->error('', 'REQUEST_SKU_ID'));
  19. }
  20. if (empty($num)) {
  21. return $this->response($this->error('', 'REQUEST_NUM'));
  22. }
  23. $cart = new CartModel();
  24. $data = [
  25. 'site_id' => $this->site_id,
  26. 'member_id' => $this->member_id,
  27. 'sku_id' => $sku_id,
  28. 'num' => $num,
  29. 'form_data' => $form_data
  30. ];
  31. $res = $cart->addCart($data);
  32. return $this->response($res);
  33. }
  34. /**
  35. * 编辑信息
  36. */
  37. public function edit()
  38. {
  39. $token = $this->checkToken();
  40. if ($token[ 'code' ] < 0) return $this->response($token);
  41. $cart_id = isset($this->params[ 'cart_id' ]) ? $this->params[ 'cart_id' ] : 0;
  42. $num = isset($this->params[ 'num' ]) ? $this->params[ 'num' ] : 0;
  43. $form_data = $this->params[ 'form_data' ] ?? '';
  44. if (empty($cart_id)) {
  45. return $this->response($this->error('', 'REQUEST_CART_ID'));
  46. }
  47. if (empty($num)) {
  48. return $this->response($this->error('', 'REQUEST_NUM'));
  49. }
  50. $cart = new CartModel();
  51. $data = [
  52. 'cart_id' => $cart_id,
  53. 'member_id' => $this->member_id,
  54. 'num' => $num,
  55. 'form_data' => $form_data
  56. ];
  57. $res = $cart->editCart($data);
  58. return $this->response($res);
  59. }
  60. /**
  61. * 删除信息
  62. */
  63. public function delete()
  64. {
  65. $token = $this->checkToken();
  66. if ($token[ 'code' ] < 0) return $this->response($token);
  67. $cart_id = isset($this->params[ 'cart_id' ]) ? $this->params[ 'cart_id' ] : 0;
  68. if (empty($cart_id)) {
  69. return $this->response($this->error('', 'REQUEST_CART_ID'));
  70. }
  71. $cart = new CartModel();
  72. $data = [
  73. 'cart_id' => $cart_id,
  74. 'member_id' => $this->member_id,
  75. ];
  76. $res = $cart->deleteCart($data);
  77. return $this->response($res);
  78. }
  79. /**
  80. * 清空购物车
  81. */
  82. public function clear()
  83. {
  84. $token = $this->checkToken();
  85. if ($token[ 'code' ] < 0) return $this->response($token);
  86. $cart = new CartModel();
  87. $data = [
  88. 'member_id' => $this->member_id,
  89. ];
  90. $res = $cart->clearCart($data);
  91. return $this->response($res);
  92. }
  93. /**
  94. * 商品购物车列表
  95. */
  96. public function goodsLists()
  97. {
  98. $token = $this->checkToken();
  99. if ($token[ 'code' ] < 0) return $this->response($token);
  100. $this->initStoreData();
  101. $goods = new Goods();
  102. $condition = [
  103. [ 'ngc.site_id', '=', $this->site_id ],
  104. [ 'ngc.member_id', '=', $this->member_id ],
  105. [ 'ngs.is_delete', '=', 0 ]
  106. ];
  107. $field = 'ngc.cart_id, ngc.site_id, ngc.member_id, ngc.sku_id, ngc.num, ngs.sku_name,ngs.goods_id,
  108. ngs.sku_no, ngs.sku_spec_format,ngs.price,ngs.market_price, ngs.goods_spec_format,
  109. ngs.discount_price, ngs.promotion_type, ngs.start_time, ngs.end_time, ngs.stock,
  110. ngs.sku_image, ngs.sku_images, ngs.goods_state, ngs.goods_stock_alarm, ngs.is_virtual, ngs.goods_name, ngs.is_consume_discount, ngs.discount_config, ngs.member_price, ngs.discount_method,
  111. ngs.virtual_indate, ngs.is_free_shipping, ngs.shipping_template, ngs.unit, ngs.introduction,ngs.sku_spec_format, ngs.keywords, ngs.max_buy, ngs.min_buy, ns.site_name, ngs.is_limit, ngs.limit_type';
  112. $join = [
  113. [ 'goods_cart ngc', 'ngc.sku_id = ngs.sku_id', 'inner' ],
  114. [ 'site ns', 'ns.site_id = ngs.site_id', 'left' ]
  115. ];
  116. // 如果是连锁运营模式
  117. if ($this->store_data[ 'config' ][ 'store_business' ] == 'store') {
  118. $join[] = [ 'store_goods_sku sgs', 'ngs.sku_id = sgs.sku_id and sgs.store_id=' . $this->store_id, 'left' ];
  119. $field .= ',IFNULL(sgs.status, 0) as store_goods_status';
  120. $field = str_replace('ngs.price', 'IFNULL(IF(ngs.is_unify_price = 1,ngs.price,sgs.price), ngs.price) as price', $field);
  121. $field = str_replace('ngs.discount_price', 'IFNULL(IF(ngs.is_unify_price = 1,ngs.discount_price,sgs.price), ngs.discount_price) as discount_price', $field);
  122. if ($this->store_data[ 'store_info' ][ 'stock_type' ] == 'store') {
  123. $field = str_replace('ngs.stock', 'IFNULL(sgs.stock, 0) as stock', $field);
  124. }
  125. }
  126. $list = $goods->getGoodsSkuList($condition, $field, 'ngc.cart_id desc', null, 'ngs', $join);
  127. if (!empty($list[ 'data' ])) {
  128. $list[ 'data' ] = $goods->getGoodsListMemberPrice($list[ 'data' ], $this->member_id);
  129. }
  130. return $this->response($list);
  131. }
  132. /**
  133. * 获取购物车数量
  134. * @return string
  135. */
  136. public function count()
  137. {
  138. $token = $this->checkToken();
  139. if ($token[ 'code' ] < 0) return $this->response($token);
  140. $this->initStoreData();
  141. $cart = new CartModel();
  142. $condition = [
  143. [ 'gc.member_id', '=', $this->member_id ],
  144. [ 'gc.site_id', '=', $this->site_id ],
  145. [ 'gs.goods_state', '=', 1 ],
  146. [ 'gs.is_delete', '=', 0 ]
  147. ];
  148. $join = [
  149. [ 'goods_sku gs', 'gc.sku_id = gs.sku_id', 'inner' ]
  150. ];
  151. if ($this->store_data[ 'config' ][ 'store_business' ] == 'store') {
  152. $join[] = [ 'store_goods_sku sgs', 'gs.sku_id = sgs.sku_id and sgs.store_id=' . $this->store_id, 'left' ];
  153. $condition[] = [ 'sgs.status', '=', 1 ];
  154. }
  155. $count = $cart->getCartSum($condition, 'gc.num', 'gc', $join);
  156. return $this->response($count);
  157. }
  158. /**
  159. * 购物车关联列表
  160. * @return false|string
  161. */
  162. public function lists()
  163. {
  164. $token = $this->checkToken();
  165. if ($token[ 'code' ] < 0) return $this->response($token);
  166. $this->initStoreData();
  167. $goods = new Goods();
  168. $condition = [
  169. [ 'ngc.site_id', '=', $this->site_id ],
  170. [ 'ngc.member_id', '=', $this->member_id ],
  171. [ 'ngs.is_delete', '=', 0 ]
  172. ];
  173. $field = 'ngc.cart_id,ngc.sku_id,ngs.goods_id,ngs.discount_price,ngc.num';
  174. $join = [
  175. [ 'goods_cart ngc', 'ngc.sku_id = ngs.sku_id', 'inner' ]
  176. ];
  177. // 如果是连锁运营模式
  178. if ($this->store_data[ 'config' ][ 'store_business' ] == 'store') {
  179. $join[] = [ 'store_goods_sku sgs', 'ngs.sku_id = sgs.sku_id and sgs.store_id=' . $this->store_id, 'left' ];
  180. $condition[] = [ 'sgs.status', '=', 1 ];
  181. $field = str_replace('ngs.discount_price', 'IFNULL(IF(ngs.is_unify_price = 1,ngs.discount_price,sgs.price), ngs.discount_price) as discount_price', $field);
  182. }
  183. $list = $goods->getGoodsSkuList($condition, $field, 'ngc.cart_id desc', null, 'ngs', $join);
  184. if (!empty($list[ 'data' ])) {
  185. $goods = new Goods();
  186. foreach ($list[ 'data' ] as $k => $v) {
  187. // 是否参与会员等级折扣
  188. $goods_member_price = $goods->getGoodsPrice($v[ 'sku_id' ], $this->member_id, $this->store_id);
  189. $goods_member_price = $goods_member_price[ 'data' ];
  190. if (!empty($goods_member_price[ 'member_price' ]) && $goods_member_price[ 'member_price' ] < $v[ 'discount_price' ]) {
  191. $list[ 'data' ][ $k ][ 'discount_price' ] = $goods_member_price[ 'price' ];
  192. }
  193. $list[ 'data' ][ $k ][ 'total_money' ] = $list[ 'data' ][ $k ][ 'discount_price' ] * $v[ 'num' ];
  194. }
  195. }
  196. return $this->response($list);
  197. }
  198. /**
  199. * 获取会员购物车中商品数量
  200. * @return false|string
  201. */
  202. public function goodsNum()
  203. {
  204. $token = $this->checkToken();
  205. if ($token[ 'code' ] < 0) return $this->response($token);
  206. $goods_id = $this->params[ 'goods_id' ] ?? 0;
  207. $condition = [
  208. [ 'gc.member_id', '=', $this->member_id ],
  209. [ 'gc.site_id', '=', $this->site_id ],
  210. [ 'gs.goods_id', '=', $goods_id ]
  211. ];
  212. $join = [
  213. [
  214. 'goods_sku gs',
  215. 'gc.sku_id = gs.sku_id',
  216. 'left'
  217. ]
  218. ];
  219. $cart = new CartModel();
  220. $data = $cart->getCartSum($condition, 'gc.num', 'gc', $join);
  221. return $this->response($data);
  222. }
  223. public function editCartSku()
  224. {
  225. $token = $this->checkToken();
  226. if ($token[ 'code' ] < 0) return $this->response($token);
  227. $cart_id = $this->params[ 'cart_id' ] ?? 0;
  228. $num = $this->params[ 'num' ] ?? 0;
  229. $sku_id = $this->params[ 'sku_id' ] ?? 0;
  230. if (empty($cart_id)) return $this->response($this->error('', 'REQUEST_CART_ID'));
  231. if (empty($num)) return $this->response($this->error('', 'REQUEST_NUM'));
  232. if (empty($sku_id)) return $this->response($this->error('', 'REQUEST_SKU_ID'));
  233. $cart = new CartModel();
  234. $data = [
  235. 'cart_id' => $cart_id,
  236. 'site_id' => $this->site_id,
  237. 'member_id' => $this->member_id,
  238. 'num' => $num,
  239. 'sku_id' => $sku_id
  240. ];
  241. $res = $cart->editCartSku($data);
  242. return $this->response($res);
  243. }
  244. }