Cart.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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('获取成功', $carts);
  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. $type = 'cover';
  58. $cart = CartModel::where('user_id', $user->id)
  59. ->where('goods_id', $params['goods_id'])
  60. ->where('goods_sku_price_id', $params['goods_sku_price_id'])
  61. ->find();
  62. $skuPrice = SkuPrice::where('goods_id', $params['goods_id'])->where('id', $params['goods_sku_price_id'])->find();
  63. if (!$skuPrice) {
  64. $this->error('商品规格未找到');
  65. }
  66. if ($cart) {
  67. if ($type == 'dec') {
  68. // 减
  69. $cart->snapshot_price = $skuPrice->price;
  70. $cart->save();
  71. $cart->setDec('goods_num', $goods_num);
  72. } else if ($type == 'cover') {
  73. $cart->goods_num = $goods_num;
  74. $cart->snapshot_price = $skuPrice->price;
  75. $cart->save();
  76. } else {
  77. // 加
  78. $cart->snapshot_price = $skuPrice->price;
  79. $cart->save();
  80. $cart->setInc('goods_num', $goods_num);
  81. }
  82. } else {
  83. $cart = new CartModel();
  84. $cart->user_id = $user->id;
  85. $cart->goods_id = $params['goods_id'];
  86. $cart->goods_sku_price_id = $params['goods_sku_price_id'];
  87. $cart->goods_num = $goods_num;
  88. $cart->snapshot_price = $skuPrice->price;
  89. $cart->save();
  90. }
  91. $this->success('更新成功', $cart);
  92. }
  93. /**
  94. * @ApiTitle (修改购物车选中状态)
  95. * @ApiSummary (修改购物车选中状态)
  96. * @ApiMethod (POST)
  97. * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  98. * @ApiParams (name="trueArr", type=string, required=true, description="选中的购物车id,多个的话用','号隔开")
  99. * @ApiParams (name="falseArr", type=string, required=true, description="不选的购物车id,多个的话用','号隔开")
  100. * @ApiReturn ({"code":1,"msg":"","data":数量})
  101. */
  102. public function choose_change()
  103. {
  104. $trueArr = input('trueArr', false);
  105. $falseArr = input('falseArr', false);
  106. $user = auth_user();
  107. $user_id = $user['id'];
  108. try {
  109. $cart = new CartModel();
  110. if ($trueArr) {
  111. $cart->save(['choose' => CartModel::CHOOSE_ON], function ($query) use ($user_id, $trueArr) {
  112. $query->where('user_id', $user_id)->where('id', 'IN', $trueArr);
  113. });
  114. }
  115. if ($falseArr) {
  116. $cart->save(['choose' => CartModel::CHOOSE_OFF], function ($query) use ($user_id, $falseArr) {
  117. $query->where('user_id', $user_id)->where('id', 'IN', $falseArr);
  118. });
  119. }
  120. } catch (Exception $e) {
  121. $this->error('更新失败', 0);
  122. }
  123. $this->success('', 1);
  124. }
  125. public function delete()
  126. {
  127. $user = auth_user();
  128. $id = $this->request->param('id');
  129. CartModel::where('user_id', $user->id)->whereIn('id', $id)->delete();
  130. $this->success('删除成功');
  131. }
  132. }