123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace addons\shopro\controller;
- use app\admin\model\shopro\Cart as CartModel;
- use app\admin\model\shopro\goods\Goods;
- use app\admin\model\shopro\goods\SkuPrice;
- use think\Db;
- class Cart extends Common
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- public function number(){
- $user = auth_user();
- $number = Db::name('shopro_cart')->where('user_id',$user['id'])->count();
- $this->success(1,$number);
- }
- public function index()
- {
- $user = auth_user();
- // 被物理删除的商品直接删掉购物车,只删除自己的
- CartModel::whereNotExists(function ($query) {
- $goodsTableName = (new Goods())->getQuery()->getTable();
- $query = $query->table($goodsTableName)->where($goodsTableName . '.id=goods_id'); // 软删除的商品购物车暂时不删,标记为失效
- return $query;
- })->where('user_id', $user->id)->delete();
- $carts = CartModel::with([
- 'goods' => function ($query) {
- $query->removeOption('soft_delete');
- }, 'sku_price'
- ])->where('user_id', $user->id)->order('id', 'desc')->select();
-
- $carts = collection($carts)->each(function ($cart) {
- $cart->tags = $cart->tags; // 标签
- $cart->status = $cart->status; // 状态
- $cart->goods->image = localpath_to_netpath($cart->goods->image);//主图
- $cart->goods->images = array_domain_image($cart->goods->images);//轮播图
- $cart->sku_price->image = localpath_to_netpath($cart->sku_price->image);//规格图
- });
- $carts = json_decode(json_encode($carts),true);
- $choose_price = '0';
- foreach($carts as $key => $val){
- if($val['choose'] == 1){
- $choose_price = bcadd($choose_price,$val['sku_price']['price'],2);
- }
- }
- $result = [
- 'choose_price' => $choose_price,
- 'list' => $carts,
- ];
- $this->success('获取成功', $result);
- }
- //新增到购物车,或编辑数量
- public function update()
- {
- $user = auth_user();
- $params = $this->request->only(['goods_id', 'goods_sku_price_id', 'goods_num', 'type']);
- $goods_num = $params['goods_num'] ?? 1;
- $type = $params['type'] ?? 'inc';
- if($goods_num < 1){
- $this->error('数量错误');
- }
- $cart = CartModel::where('user_id', $user->id)
- ->where('goods_id', $params['goods_id'])
- ->where('goods_sku_price_id', $params['goods_sku_price_id'])
- ->find();
-
- $skuPrice = SkuPrice::where('goods_id', $params['goods_id'])->where('id', $params['goods_sku_price_id'])->find();
- if (!$skuPrice) {
- $this->error('商品规格未找到');
- }
- if ($cart) {
- if ($type == 'dec') {
- // 减
- $cart->snapshot_price = $skuPrice->price;
- $cart->save();
- $cart->setDec('goods_num', $goods_num);
- } else if ($type == 'cover') {
- $cart->goods_num = $goods_num;
- $cart->snapshot_price = $skuPrice->price;
- $cart->save();
- } else {
- // 加
- $cart->snapshot_price = $skuPrice->price;
- $cart->save();
- $cart->setInc('goods_num', $goods_num);
- }
- } else {
- $cart = new CartModel();
- $cart->user_id = $user->id;
- $cart->goods_id = $params['goods_id'];
- $cart->goods_sku_price_id = $params['goods_sku_price_id'];
- $cart->goods_num = $goods_num;
- $cart->snapshot_price = $skuPrice->price;
- $cart->save();
- }
- $this->success('更新成功', $cart);
- }
- /**
- * @ApiTitle (修改购物车选中状态)
- * @ApiSummary (修改购物车选中状态)
- * @ApiMethod (POST)
- * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
- * @ApiParams (name="trueArr", type=string, required=true, description="选中的购物车id,多个的话用','号隔开")
- * @ApiParams (name="falseArr", type=string, required=true, description="不选的购物车id,多个的话用','号隔开")
- * @ApiReturn ({"code":1,"msg":"","data":数量})
- */
- public function choose_change()
- {
- $trueArr = input('trueArr', false);
- $falseArr = input('falseArr', false);
- $user = auth_user();
- $user_id = $user['id'];
- try {
- $cart = new CartModel();
- if ($trueArr) {
- $cart->save(['choose' => CartModel::CHOOSE_ON], function ($query) use ($user_id, $trueArr) {
- $query->where('user_id', $user_id)->where('id', 'IN', $trueArr);
- });
- }
- if ($falseArr) {
- $cart->save(['choose' => CartModel::CHOOSE_OFF], function ($query) use ($user_id, $falseArr) {
- $query->where('user_id', $user_id)->where('id', 'IN', $falseArr);
- });
- }
- } catch (Exception $e) {
- $this->error('更新失败', 0);
- }
- $this->success('', 1);
- }
- public function delete()
- {
- $user = auth_user();
- $id = $this->request->param('id');
- CartModel::where('user_id', $user->id)->whereIn('id', $id)->delete();
- $this->success('删除成功');
- }
- }
|