ShopCar.php 6.5 KB
<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/11/18
 * Time: 16:26
 */

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 购物车接口
 */
class ShopCar extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 购物车接口
     * @ApiTitle    (添加购物车)
     * @ApiSummary  (添加购物车)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Shop_Car/InsertShopCar)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="Goods_id", type="integer", required=true, description=" 商品ID")
     * @ApiParams   (name="num", type="integer", required=true, description="数量")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function InsertShopCar()
    {
        $UserId = $this->is_token($this->request->header());
        $params = $this->request->param();
        //Json转数组 json_decode($Json, true);
        //检查是否存在商品
        $IsCar = Db::name('shopcar')->where('user_id', $UserId)->where('Goods_id', $params['Goods_id'])->find();
        if (empty($IsCar)) {
            //为空 检查库存
            $this->StockLook($params['Goods_id'], $params['num']);
            $data = [
                'user_id' => $UserId,
                'Goods_id' => $params['Goods_id'],
                'num' => $params['num']
            ];
            $Res = Db::name('shopcar')->insert($data);
        } else {
            // 检查库存
            $this->StockLook($params['Goods_id'], $params['num'] + $IsCar['num']);
            $Res = Db::name('shopcar')->where('user_id', $UserId)->where('Goods_id', $params['Goods_id'])->update(
                [
                    'num' => $params['num'] + $IsCar['num']
                ]
            );
        }
        $this->res($Res);
    }


    /**
     * 购物车接口
     * @ApiTitle    (删除购物车)
     * @ApiSummary  (删除购物车)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Shop_Car/DeleteShopCar)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="integer", required=true, description="1=删除单个,2=一键清空")
     * @ApiParams   (name="id", type="integer", required=true, description="购物车ID 一键清空为0")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function DeleteShopCar()
    {
        $UserId = $this->is_token($this->request->header());
        $params = $this->request->param();
        $map = [];
        if ($params['type'] == 2) {
            //一键清空
            $map['id'] = ['EQ', $params['id']];
        }
        $Res = Db::name('shopcar')->where('user_id', $UserId)->where($map)->delete();
        $this->res($Res);
    }


    /**
     * 购物车接口
     * @ApiTitle    (购物车操作)
     * @ApiSummary  (购物车操作)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Shop_Car/ShopcarAddDel)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="integer", required=true, description="1=增加,2=减少")
     * @ApiParams   (name="id", type="integer", required=true, description="购物车ID")
     * @ApiParams   (name="num", type="integer", required=true, description="数量")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function ShopcarAddDel()
    {
        $UserId = $this->is_token($this->request->header());
        $params = $this->request->param();
        $ShopCarArray = Db::name('shopcar')->where('user_id', $UserId)->where('id', $params['id'])->find();
        if (empty($ShopCarArray)) {
            $this->error('参数错误', 0);
            die;
        }
        if ($params['type'] == 1) {
            //增加
            //检查库存
            $this->StockLook($ShopCarArray['Goods_id'], $params['num'] + $ShopCarArray['num']);
            $Res = Db::name('shopcar')->where('user_id', $UserId)->where('id', $params['id'])->update(
                [
                    'num' => $params['num'] + $ShopCarArray['num']
                ]
            );
        } else {
            //减少
            $Res = Db::name('shopcar')->where('user_id', $UserId)->where('id', $params['id'])->update(
                [
                    'num' => $ShopCarArray['num'] - $params['num']
                ]
            );
        }
        $this->res($Res);
    }


    /**
     * 购物车接口
     * @ApiTitle    (购物车列表)
     * @ApiSummary  (购物车列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Shop_Car/ShopcarList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function ShopcarList()
    {
        $UserId = $this->is_token($this->request->header());
        $Array = Db::name('shopcar')
            ->alias('a')
            ->where('a.user_id', $UserId)
            ->order('a.id desc')
            ->join('goods g', 'g.id=a.Goods_id')
            ->field('a.id,g.id as Goods_id,g.image,g.title,g.price,g.delete_price,a.num')
            ->select();
        $List = [];
        if (!empty($Array)) {
            foreach ($Array as $k => $v) {
                $List[$k]['id'] = $v['id'];
                $List[$k]['image'] = cdnurl($v['image']);
                $List[$k]['title'] = $v['title'];
                $List[$k]['Goods_id'] = $v['Goods_id'];
                $List[$k]['price'] = $v['price'];
                $List[$k]['delete_price'] = $v['delete_price'];
                $List[$k]['num'] = $v['num'];
            }
        }
        $this->success('成功', $List);
    }
}