| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 | 
							- <?php
 
- namespace app\api\controller;
 
- use app\common\model\Carts;
 
- use app\common\model\Sku;
 
- use app\common\model\Goods;
 
- use app\common\Service\CartService;
 
- use app\api\validate\Cart as CartValidate;
 
- use app\common\Enum\GoodsEnum;
 
- /**
 
-  * 购物车接口
 
-  */
 
- class Cart extends Base
 
- {
 
-     protected $noNeedLogin = [];
 
-     //添加购物车
 
-     public function add()
 
-     {
 
-         $goods_id = $this->request->post('goods_id'); //单规格使用
 
-         $goods_sku_id = $this->request->post('goods_sku_id'); //多规格使用
 
-         $nums = $this->request->post('nums/d', 1);
 
-         // 增加验证器
 
-         $validate = new CartValidate();
 
-         $validate->scene('add')->check([
 
-             'goods_id' => $goods_id,
 
-             'goods_sku_id' => $goods_sku_id,
 
-             'nums' => $nums,
 
-         ]);
 
-         if ($validate->getError()) {
 
-             $this->error($validate->getError());
 
-         }
 
-         $goods = Goods::where('id', $goods_id)
 
-          ->where('status', GoodsEnum::STATUS_ON_SALE)
 
-          ->find();
 
-         
 
-         if (empty($goods)) {
 
-             $this->error('商品不存在');
 
-         }
 
-         if ($goods['spec_type'] && !$goods_sku_id) {
 
-             $this->error("请选择规格");
 
-         }
 
-         if (empty($goods_id) && empty($goods_sku_id)) {
 
-             $this->error('参数错误');
 
-         }
 
-         if ($nums <= 0) {
 
-             $this->error('数量必须大于0');
 
-         }
 
-         if (!empty($goods_sku_id)) {
 
-             $row = Sku::with([
 
-                 'goods' => function ($query) {
 
-                     $query->where('status', GoodsEnum::STATUS_ON_SALE);
 
-                 }
 
-             ])->where('id', $goods_sku_id)->find();
 
-             if (empty($row) || empty($row->goods)) {
 
-                 $this->error('商品已下架');
 
-             }
 
-             if ($row->stocks < $nums) {
 
-                 $this->error('库存数量不足' . $nums . '件');
 
-             }
 
-         } else {
 
-             if ($goods->stocks < $nums) {
 
-                 $this->error('库存数量不足' . $nums . '件');
 
-             }
 
-         }
 
-         //去添加购物车
 
-         $cart_id = CartService::push($goods_id, $goods_sku_id, $nums, $this->auth->id);
 
-         $this->success('添加成功', $cart_id);
 
-     }
 
-     //删除购物车
 
-     public function del()
 
-     {
 
-         $arrIds = $this->request->post('id/a');
 
-         $userId = $this->auth->id;
 
-         // 验证器
 
-         $validate = new CartValidate();
 
-         $validate->scene('del')->check([
 
-             'id' => $arrIds,
 
-         ]);
 
-         if ($validate->getError()) {
 
-             $this->error($validate->getError());
 
-         }
 
-         $status = CartService::deleteCart($arrIds, $userId);
 
-         if ($status) {
 
-             $this->success('删除成功');
 
-         }
 
-         $this->error('删除失败');
 
-     }
 
-     //购物车商品数量+-
 
-     public function set_nums()
 
-     {
 
-         $id = $this->request->post('id');
 
-         if (!$id) {
 
-             $this->error('参数错误');
 
-         }
 
-         $nums = $this->request->post('nums/d');
 
-         // 使用验证器验证数量
 
-         $validate = new CartValidate();
 
-         $validate->scene('set_nums')->check([
 
-             'nums' => $nums,
 
-         ]);
 
-         $row = Carts::with(['Goods', 'Sku'])->where('id', $id)->where('user_id', $this->auth->id)->find();
 
-         if (!$row) {
 
-             $this->error('未找到记录');
 
-         }
 
-         if ($row->goods_sku_id) {
 
-             if (!$row->sku) {
 
-                 $this->error('商品不存在');
 
-             }
 
-             if ($row->sku->stocks < $nums) {
 
-                 $this->error('库存不足');
 
-             }
 
-         } else {
 
-             if (!$row->goods) {
 
-                 $this->error('商品不存在');
 
-             }
 
-             if ($row->goods->stocks < $nums) {
 
-                 $this->error('库存不足');
 
-             }
 
-         }
 
-         $row->nums = $nums;
 
-         $row->save();
 
-         $this->success('操作成功');
 
-     }
 
-     //购物车列表
 
-     public function index()
 
-     {
 
-         $ids = $this->request->param('ids');
 
-         $list = CartService::getGoodsList($ids, $this->auth->id);
 
-         //没有商品过滤数据
 
-         foreach ($list as $key => $item) {
 
-             if (empty($item->goods)) {
 
-                 $item->delete();
 
-                 unset($list[$key]);
 
-                 continue;
 
-             }
 
-             // 如果sku 的 图片为空
 
-             if (empty($item->sku->image)) {
 
-                 $item->sku->image = $item->goods->image;
 
-             }
 
-             $item['subtotal'] = bcmul($item['nums'], ($item->sku->price ?? $item->goods->price), 2);
 
-             $item->goods->visible(explode(',', 'id,title,price,image,marketprice'));
 
-         }
 
-         $this->success('', $list);
 
-     }
 
-     //获取购物车的数量
 
-     public function cart_nums()
 
-     {
 
-         $total = Carts::where('user_id', $this->auth->id)->count();
 
-         $this->success('', $total);
 
-     }
 
-      /**
 
-      * 选中或取消商品
 
-      * 
 
-      */
 
-     public function checked()
 
-     {
 
-         $cartIds  =  $this->request->post('cartIds/a');
 
-         $isChecked = $this->request->post('isChecked');
 
-         $userId = $this->auth->id;
 
-         // 验证器
 
-         $validate = new CartValidate();
 
-         $validate->scene('checked')->check([
 
-             'cartIds' => $cartIds,
 
-             'isChecked' => $isChecked,
 
-         ]);
 
-         if ($validate->getError()) {
 
-             $this->error($validate->getError());
 
-         }
 
-         CartService::updateChecked(
 
-             $userId,
 
-             $cartIds,
 
-             $isChecked);
 
-         $this->success('操作成功');
 
-     }
 
- }
 
 
  |