Cart.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2019/10/27
  6. * Time: 5:37 下午
  7. */
  8. namespace addons\unishop\controller;
  9. use addons\unishop\extend\Hashids;
  10. use addons\unishop\model\Config;
  11. use addons\unishop\model\Product;
  12. use addons\unishop\model\Cart as CartModel;
  13. use think\Exception;
  14. /**
  15. * 购物车
  16. */
  17. class Cart extends Base
  18. {
  19. /**
  20. * 允许频繁访问的接口
  21. * @var array
  22. */
  23. protected $frequently = ['number_change', 'choose_change'];
  24. /**
  25. * @ApiTitle (列表)
  26. * @ApiSummary (购物车列表)
  27. * @ApiMethod (GET)
  28. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  29. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  30. * @ApiReturn ({"code":1,"msg":"","data":[]})
  31. *
  32. * @ApiReturnParams (name="market_price", type="string", description="市场价")
  33. * @ApiReturnParams (name="sales_price", type="string", description="销售价")
  34. * @ApiReturnParams (name="stock", type="integer", description="库存")
  35. * @ApiReturnParams (name="sales", type="integer", description="销量")
  36. * @ApiReturnParams (name="image", type="string", description="图片")
  37. * @ApiReturnParams (name="title", type="string", description="商品名称")
  38. * @ApiReturnParams (name="choose", type="integer", description="是否选中")
  39. * @ApiReturnParams (name="isset", type="integer", description="是否生效")
  40. * @ApiReturnParams (name="cart_id", type="integer", description="购物车id")
  41. * @ApiReturnParams (name="spec", type="string", description="选中的规格")
  42. * @ApiReturnParams (name="number", type="integer", description="数量")
  43. * @ApiReturnParams (name="oldPrice", type="string", description="旧价格")
  44. * @ApiReturnParams (name="nowPrice", type="string", description="现价格")
  45. * @ApiReturnParams (name="product_id", type="string", description="商品id")
  46. *
  47. */
  48. public function index()
  49. {
  50. $carts = (new CartModel)->where(['user_id' => $this->auth->id])
  51. ->with([
  52. 'product' => function ($query) {
  53. $query->field(['id', 'image', 'title', 'specTableList','sales','market_price','sales_price','stock','use_spec', 'switch']);
  54. }
  55. ])
  56. ->order(['createtime' => 'desc'])
  57. ->select();
  58. if (!$carts) {
  59. $this->success('', []);
  60. }
  61. $data = [];
  62. $productExtend = new \addons\unishop\extend\Product;
  63. foreach ($carts as $item) {
  64. $oldProduct = json_decode($item['snapshot'], true);
  65. $oldData = $productExtend->getBaseData($oldProduct, $item['spec'] ?? '');
  66. if (empty($item['product'])) {
  67. $tempData = $oldData;
  68. $tempData['isset'] = false; // 失效
  69. $tempData['title'] = $oldProduct['title'];
  70. $tempData['choose'] = 0;
  71. } else {
  72. $productData = $item['product']->getData();
  73. $tempData = $productExtend->getBaseData($productData, $item['spec'] ?? '');
  74. $tempData['title'] = $item['product']['title'];
  75. $tempData['choose'] = $item['choose']; //是否选中
  76. $tempData['isset'] = true;
  77. if ($productData['switch'] == Product::SWITCH_OFF) {
  78. $tempData['isset'] = false; // 失效
  79. $tempData['choose'] = 0;
  80. }
  81. }
  82. $tempData['cart_id'] = $item['id'];
  83. $tempData['spec'] = $item['spec'];
  84. $tempData['number'] = $item['number'];
  85. $tempData['image'] = Config::getImagesFullUrl($oldData['image']);
  86. $tempData['oldPrice'] = round($oldData['sales_price'], 2);
  87. $tempData['nowPrice'] = round($tempData['sales_price'], 2);
  88. $tempData['product_id'] = Hashids::encodeHex($item['product_id']);
  89. $data[] = $tempData;
  90. }
  91. $this->success('', $data);
  92. }
  93. /**
  94. * @ApiTitle (添加)
  95. * @ApiSummary (添加商品到购物车)
  96. * @ApiMethod (GET)
  97. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  98. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  99. * @ApiParams (name="id", type="string", description="商品id")
  100. * @ApiReturn ({"code":1,"msg":"添加成功","data":1})
  101. */
  102. public function add()
  103. {
  104. $id = $this->request->get('id', 0);
  105. $id = \addons\unishop\extend\Hashids::decodeHex($id);
  106. $product = (new Product)->where(['id' => $id, 'switch' => Product::SWITCH_ON])->find();
  107. if (!$product) {
  108. $this->error('产品不存在或已下架');
  109. }
  110. $spec = $this->request->get('spec', '');
  111. $productBase = (new \addons\unishop\extend\Product())->getBaseData($product->getData(), $spec);
  112. if (!$productBase['stock'] || $productBase['stock'] <= 0) {
  113. $this->error('库存不足');
  114. }
  115. $user_id = $this->auth->id;
  116. $cartModel = new \addons\unishop\model\Cart();
  117. $cartModel->where(['user_id' => $user_id, 'product_id' => $id]);
  118. $spec && $cartModel->where('spec', $spec);
  119. $oldCart = $cartModel->find();
  120. if ($oldCart) {
  121. $this->error('商品已存在购物车');
  122. // $oldCart->number++;
  123. // $result = $oldCart->save();
  124. } else {
  125. $cartModel->user_id = $user_id;
  126. $cartModel->product_id = $id;
  127. $spec && $cartModel->spec = $spec;
  128. $cartModel->number = 1;
  129. $cartModel->snapshot = json_encode($product->getData(), true);
  130. $result = $cartModel->save();
  131. }
  132. if ($result) {
  133. $this->success('添加成功', 1);
  134. } else {
  135. $this->error('添加失败');
  136. }
  137. }
  138. /**
  139. * @ApiTitle (删除)
  140. * @ApiSummary (删除购物车的商品支持多个删除用','号隔开购物车id)
  141. * @ApiMethod (POST)
  142. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  143. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  144. * @ApiParams (name="id", type="string", description="购物车id,多个的话用','号隔开")
  145. * @ApiReturn ({"code":1,"msg":"删除成功","data":1})
  146. */
  147. public function delete()
  148. {
  149. $id = $this->request->post('id', 0);
  150. $userId = $this->auth->id;
  151. $result = CartModel::destroy(function ($query) use ($id, $userId) {
  152. $query->whereIn('id', $id)->where(['user_id' => $userId]);
  153. });
  154. if ($result) {
  155. $this->success('删除成功', 1);
  156. } else {
  157. $this->error('删除失败', 0);
  158. }
  159. }
  160. /**
  161. * @ApiTitle (修改购物车数量)
  162. * @ApiSummary (修改购物车数量)
  163. * @ApiMethod (GET)
  164. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  165. * @ApiParams (name="id", type=integer, required=true, description="购物车id")
  166. * @ApiParams (name="number", type=integer, required=true, description="数量")
  167. * @ApiReturn ({"code":1,"msg":"更改成功","data":数量})
  168. */
  169. public function number_change()
  170. {
  171. $cart_id = $this->request->get('id', 0);
  172. $number = $this->request->get('number', 1);
  173. $cart = CartModel::get(['id' => $cart_id, 'user_id' => $this->auth->id]);
  174. if (empty($cart)) {
  175. $this->error('此商品不存在购物车');
  176. }
  177. $cart->number = $number;
  178. $result = $cart->save();
  179. if ($result) {
  180. $this->success('更改成功', $number);
  181. } else {
  182. $this->error('更改失败', $number);
  183. }
  184. }
  185. /**
  186. * @ApiTitle (修改购物车选中状态)
  187. * @ApiSummary (修改购物车选中状态)
  188. * @ApiMethod (POST)
  189. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  190. * @ApiParams (name="trueArr", type=string, required=true, description="选中的购物车id,多个的话用','号隔开")
  191. * @ApiParams (name="falseArr", type=string, required=true, description="不选的购物车id,多个的话用','号隔开")
  192. * @ApiReturn ({"code":1,"msg":"","data":数量})
  193. */
  194. public function choose_change()
  195. {
  196. $trueArr = $this->request->post('trueArr', false);
  197. $falseArr = $this->request->post('falseArr', false);
  198. $user_id = $this->auth->id;
  199. try {
  200. $cart = new CartModel();
  201. if ($trueArr) {
  202. $cart->save(['choose' => CartModel::CHOOSE_ON], function ($query) use ($user_id, $trueArr) {
  203. $query->where('user_id', $user_id)->where('id', 'IN', $trueArr);
  204. });
  205. }
  206. if ($falseArr) {
  207. $cart->save(['choose' => CartModel::CHOOSE_OFF], function ($query) use ($user_id, $falseArr) {
  208. $query->where('user_id', $user_id)->where('id', 'IN', $falseArr);
  209. });
  210. }
  211. } catch (Exception $e) {
  212. $this->error('更新失败', 0);
  213. }
  214. $this->success('', 1);
  215. }
  216. }