ShopCar.php 6.7 KB
<?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)
     * @ApiRoute    (/api/Shop_Car/AddShoppingCart)
     * @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();
        $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]);
        }
        if ($res) {
            $this->success('成功', 1);
        } else {
            $this->error('失败', 0);
        }
    }


    /**
     * @ApiTitle    (购物车接口-购物车渲染)
     * @ApiSummary  (购物车渲染)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Shop_Car/ShoppingCartRendering)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="shop_id", type="string", required=true, description="购物车ID[无传null]")
     * @ApiParams   (name="num", type="int", 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": {
    "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"
    }pa'g
    ]
    }
    ]
    }
    })
     */
    public function ShoppingCartRendering()
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
        $product_id = Db::name('shopcar')->where(['id' => $param['shop_id']])->value('product_id');
        $stock = Db::name('product')->where(['id' => $product_id])->value('stock');
        if (!empty($param['shop_id']) || $param['shop_id'] != null || $param['shop_id'] != '') {
            $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{
                echo '1';
                Db::name('shopcar')->where(['user_id' => $user_id])->where(['id' => $param['shop_id']])->update(['buy_num' => $param['num']]);
            }
        }
        $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']);
        }
        if (empty($list)) {
            $listitem = [];
            $return['count_price'] = 0;
            $return['count_num'] = 0;
        } else {
            foreach ($list as $value) {
                foreach ($value as $v) {
                    $listitem[] = $v;
                }
            }
            $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;
        }
        $return['list'] = $listitem;
        $this->success('', $return);
    }


    /**
     * @ApiTitle    (购物车接口-购物车删除)
     * @ApiSummary  (购物车渲染)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Shop_Car/DeleteCart)
     * @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);
            }
        }
    }
}