Cart.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace addons\shopro\controller;
  3. use app\admin\model\shopro\Cart as CartModel;
  4. use app\admin\model\shopro\goods\Goods;
  5. use app\admin\model\shopro\goods\SkuPrice;
  6. use think\Db;
  7. class Cart extends Common
  8. {
  9. protected $noNeedLogin = [];
  10. protected $noNeedRight = ['*'];
  11. public function number(){
  12. $user = auth_user();
  13. $number = Db::name('shopro_cart')->where('user_id',$user['id'])->count();
  14. $this->success(1,$number);
  15. }
  16. public function index()
  17. {
  18. $user = auth_user();
  19. // 被物理删除的商品直接删掉购物车,只删除自己的
  20. CartModel::whereNotExists(function ($query) {
  21. $goodsTableName = (new Goods())->getQuery()->getTable();
  22. $query = $query->table($goodsTableName)->where($goodsTableName . '.id=goods_id'); // 软删除的商品购物车暂时不删,标记为失效
  23. return $query;
  24. })->where('user_id', $user->id)->delete();
  25. $carts = CartModel::with([
  26. 'goods' => function ($query) {
  27. $query->removeOption('soft_delete');
  28. }, 'sku_price'
  29. ])->where('user_id', $user->id)->order('id', 'desc')->select();
  30. $carts = collection($carts)->each(function ($cart) {
  31. $cart->tags = $cart->tags; // 标签
  32. $cart->status = $cart->status; // 状态
  33. $cart->goods->image = localpath_to_netpath($cart->goods->image);//主图
  34. $cart->goods->images = array_domain_image($cart->goods->images);//轮播图
  35. $cart->sku_price->image = localpath_to_netpath($cart->sku_price->image);//规格图
  36. });
  37. $carts = json_decode(json_encode($carts),true);
  38. $choose_price = '0';
  39. foreach($carts as $key => $val){
  40. if($val['choose'] == 1){
  41. $choose_price = bcadd($choose_price,$val['sku_price']['price'],2);
  42. }
  43. }
  44. $result = [
  45. 'choose_price' => $choose_price,
  46. 'list' => $carts,
  47. ];
  48. $this->success('获取成功', $result);
  49. }
  50. //新增到购物车,或编辑数量
  51. public function update()
  52. {
  53. $user = auth_user();
  54. $params = $this->request->only(['goods_id', 'goods_sku_price_id', 'goods_num', 'type']);
  55. $goods_num = $params['goods_num'] ?? 1;
  56. $type = $params['type'] ?? 'inc';
  57. if($goods_num < 1){
  58. $this->error('数量错误');
  59. }
  60. $cart = CartModel::where('user_id', $user->id)
  61. ->where('goods_id', $params['goods_id'])
  62. ->where('goods_sku_price_id', $params['goods_sku_price_id'])
  63. ->find();
  64. $skuPrice = SkuPrice::where('goods_id', $params['goods_id'])->where('id', $params['goods_sku_price_id'])->find();
  65. if (!$skuPrice) {
  66. $this->error('商品规格未找到');
  67. }
  68. if ($cart) {
  69. if ($type == 'dec') {
  70. // 减
  71. $cart->snapshot_price = $skuPrice->price;
  72. $cart->save();
  73. $cart->setDec('goods_num', $goods_num);
  74. } else if ($type == 'cover') {
  75. $cart->goods_num = $goods_num;
  76. $cart->snapshot_price = $skuPrice->price;
  77. $cart->save();
  78. } else {
  79. // 加
  80. $cart->snapshot_price = $skuPrice->price;
  81. $cart->save();
  82. $cart->setInc('goods_num', $goods_num);
  83. }
  84. } else {
  85. $cart = new CartModel();
  86. $cart->user_id = $user->id;
  87. $cart->goods_id = $params['goods_id'];
  88. $cart->goods_sku_price_id = $params['goods_sku_price_id'];
  89. $cart->goods_num = $goods_num;
  90. $cart->snapshot_price = $skuPrice->price;
  91. $cart->save();
  92. }
  93. $this->success('更新成功', $cart);
  94. }
  95. /**
  96. * @ApiTitle (修改购物车选中状态)
  97. * @ApiSummary (修改购物车选中状态)
  98. * @ApiMethod (POST)
  99. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  100. * @ApiParams (name="trueArr", type=string, required=true, description="选中的购物车id,多个的话用','号隔开")
  101. * @ApiParams (name="falseArr", type=string, required=true, description="不选的购物车id,多个的话用','号隔开")
  102. * @ApiReturn ({"code":1,"msg":"","data":数量})
  103. */
  104. public function choose_change()
  105. {
  106. $trueArr = input('trueArr', false);
  107. $falseArr = input('falseArr', false);
  108. $user = auth_user();
  109. $user_id = $user['id'];
  110. try {
  111. $cart = new CartModel();
  112. if ($trueArr) {
  113. $cart->save(['choose' => CartModel::CHOOSE_ON], function ($query) use ($user_id, $trueArr) {
  114. $query->where('user_id', $user_id)->where('id', 'IN', $trueArr);
  115. });
  116. }
  117. if ($falseArr) {
  118. $cart->save(['choose' => CartModel::CHOOSE_OFF], function ($query) use ($user_id, $falseArr) {
  119. $query->where('user_id', $user_id)->where('id', 'IN', $falseArr);
  120. });
  121. }
  122. } catch (Exception $e) {
  123. $this->error('更新失败', 0);
  124. }
  125. $this->success('', 1);
  126. }
  127. public function delete()
  128. {
  129. $user = auth_user();
  130. $id = $this->request->param('id');
  131. CartModel::where('user_id', $user->id)->whereIn('id', $id)->delete();
  132. $this->success('删除成功');
  133. }
  134. }