审查视图

application/api/controller/ShopCar.php 6.7 KB
wangzhi authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/7/15
 * Time: 16:02
 */

namespace app\api\controller;


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

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

    public function _initialize()
    {
        parent::_initialize();
    }



//    public function ceshi()
//    {
//        $arr=Db::name('shopcar')->where(['user_id'=>2])->select();
//        foreach ($arr as $k=>$v){
//            $list[$k] = $this->CalculateCommodityAmount($v['product_id'], $v['id'], $v['buy_num']);
//        }
//        foreach ($list as $value) {
//            foreach ($value as $v) {
//                $listitem[] = $v;
//            }
//        }
//        $return['list']=$listitem;
//        $this->success('', $return);
//    }


    /**
     * @ApiTitle    (购物车接口-添加购物车)
     * @ApiSummary  (添加购物车)
     * @ApiMethod   (POST)
wangzhi authored
50
     * @ApiRoute    (/api/Shop_Car/AddShoppingCart)
wangzhi authored
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="string", required=true, description="商品ID")
     * @ApiParams   (name="num", type="string", 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": "1"
    })
     */
    public function AddShoppingCart()
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
王智 authored
66 67 68 69 70 71 72
        $is_shop = Db::name('shopcar')->where(['user_id' => $user_id])->where(['product_id' => $param['id']])->value('buy_num');
        if (empty($is_shop)) {
            $res = Db::name('shopcar')->insert(['product_id' => $param['id'], 'buy_num' => $param['num'], 'user_id' => $user_id]);
        } else {
            $shop_num = $is_shop + $param['num'];
            $res = Db::name('shopcar')->where(['user_id' => $user_id])->where(['product_id' => $param['id']])->update(['buy_num' => $shop_num]);
        }
wangzhi authored
73 74 75 76 77 78 79 80 81 82 83 84
        if ($res) {
            $this->success('成功', 1);
        } else {
            $this->error('失败', 0);
        }
    }


    /**
     * @ApiTitle    (购物车接口-购物车渲染)
     * @ApiSummary  (购物车渲染)
     * @ApiMethod   (POST)
wangzhi authored
85
     * @ApiRoute    (/api/Shop_Car/ShoppingCartRendering)
wangzhi authored
86 87
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="shop_id", type="string", required=true, description="购物车ID[无传null]")
wangzhi authored
88
     * @ApiParams   (name="num", type="int", required=true, description="数量")
wangzhi authored
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    "count_price": 总价,
    "list": [
    {
    "product_id": 商品ID,
    "id": 购物车ID,
    "avatar": "http://qco519e0n.bkt.clouddn.com/uploads/20200703/Fpi3RGA38eqT3eDk_sh99hOZ7wAA.png",
    "name": "12kf(123)+10.50v",
    "class_con": "详细分类2",
    "logo": "品牌1",
    "buy_num": 数量,
    "price": 价格,
    "gradient": [
    {
    "tidu": "100",
    "price": "0.10"
王智 authored
110
    }pa'g
wangzhi authored
111 112 113 114 115 116 117 118 119 120
    ]
    }
    ]
    }
    })
     */
    public function ShoppingCartRendering()
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
王智 authored
121
        $product_id = Db::name('shopcar')->where(['id' => $param['shop_id']])->value('product_id');
王智 authored
122
        $stock = Db::name('product')->where(['id' => $product_id])->value('stock');
wangzhi authored
123
        if (!empty($param['shop_id']) || $param['shop_id'] != null || $param['shop_id'] != '') {
王智 authored
124 125 126 127 128
            $product_id = Db::name('shopcar')->where(['id' => $param['shop_id']])->value('product_id');
            $stock = Db::name('product')->where(['id' => $product_id])->value('stock');
            if ($param['num'] > $stock) {
                Db::name('shopcar')->where(['user_id' => $user_id])->where(['id' => $param['shop_id']])->update(['buy_num' => $stock]);
            }else{
王智 authored
129
                echo '1';
王智 authored
130 131
                Db::name('shopcar')->where(['user_id' => $user_id])->where(['id' => $param['shop_id']])->update(['buy_num' => $param['num']]);
            }
wangzhi authored
132 133 134 135 136
        }
        $arr = Db::name('shopcar')->where(['user_id' => $user_id])->select();
        foreach ($arr as $k => $v) {
            $list[$k] = $this->CalculateCommodityAmount($v['product_id'], $v['id'], $v['buy_num']);
        }
王智 authored
137 138
        if (empty($list)) {
            $listitem = [];
王智 authored
139 140
            $return['count_price'] = 0;
            $return['count_num'] = 0;
王智 authored
141 142 143 144 145
        } else {
            foreach ($list as $value) {
                foreach ($value as $v) {
                    $listitem[] = $v;
                }
wangzhi authored
146
            }
王智 authored
147 148 149 150
            $count_price = array_sum(array_column($listitem, 'price'));
            $count_num = array_sum(array_column($listitem, 'buy_num'));
            $return['count_price'] = round($count_price, 2);
            $return['count_num'] = $count_num;
wangzhi authored
151 152 153 154 155 156 157 158 159 160
        }
        $return['list'] = $listitem;
        $this->success('', $return);
    }


    /**
     * @ApiTitle    (购物车接口-购物车删除)
     * @ApiSummary  (购物车渲染)
     * @ApiMethod   (POST)
wangzhi authored
161
     * @ApiRoute    (/api/Shop_Car/DeleteCart)
wangzhi authored
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="shop_id", type="string", required=true, description="购物车ID[单个不带,],[多选删除,分割]")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    }
    })
     */
    public function DeleteCart()
    {
        $shop_id = input('shop_id');
        if (stristr($shop_id, ',')) {
            $shop_id_arr = explode(',', $shop_id);
            foreach ($shop_id_arr as $k => $v) {
                $res[$k] = Db::name('shopcar')->where(['id' => $v])->delete();
            }
            if ($res) {
                $this->success('删除成功', 1);
            } else {
                $this->error('删除失败', 0);
            }
        } else {
            $res = Db::name('shopcar')->where(['id' => $shop_id])->delete();
            if ($res) {
                $this->success('删除成功', 1);
            } else {
                $this->error('删除失败', 0);
            }
        }
    }
}