作者 李忠强

更新

  1 +<?php
  2 +
  3 +
  4 +namespace app\api\controller;
  5 +
  6 +
  7 +use app\common\controller\Api;
  8 +
  9 +/**
  10 + * 购物车
  11 + */
  12 +class Cart extends Api
  13 +{
  14 + protected $noNeedLogin = ['*'];
  15 + protected $noNeedRight = ['*'];
  16 +
  17 + /**
  18 + * @ApiTitle (加入购物车)
  19 + * @ApiMethod (POST)
  20 + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  21 + * @ApiParams (name="sku_id", type="integer", required=true, description="规格id")
  22 + * @ApiParams (name="goods_id", type="integer", required=true, description="商品id")
  23 + * @ApiParams (name="number", type="integer", required=true, description="购买数量")
  24 + * @ApiReturn ({
  25 + 'code':'1',
  26 + 'msg':'SUCCESS'
  27 + })
  28 + */
  29 + public function addCart()
  30 + {
  31 + $sku_id = $this->request->post('sku_id');
  32 + $goods_id = $this->request->post('goods_id');
  33 + $number = $this->request->post('number');
  34 +
  35 + if (!is_numeric($sku_id) || !is_numeric($goods_id) || !is_numeric($number)) $this->error('参数不合法');
  36 +
  37 + $data = [
  38 + 'user_id' => $this->auth->id,
  39 + 'sku_id' => $sku_id,
  40 + 'goods_id' => $goods_id,
  41 + 'number' => $number,
  42 + ];
  43 + $model = new \app\api\model\Cart();
  44 + $model->save($data);
  45 + $this->success('SUCCESS');
  46 + }
  47 +
  48 + /**
  49 + * @ApiTitle (增加购物车数量)
  50 + * @ApiMethod (POST)
  51 + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  52 + * @ApiParams (name="id", type="integer", required=true, description="购物车id")
  53 + * @ApiParams (name="number", type="integer", required=true, description="增加数量")
  54 + * @ApiReturn ({
  55 + 'code':'1',
  56 + 'msg':'SUCCESS'
  57 + })
  58 + */
  59 + public function addCartNumber()
  60 + {
  61 + $id = $this->request->post('id');
  62 + $number = $this->request->post('number');
  63 +
  64 + if (!is_numeric($id) || !is_numeric($number)) $this->error('参数不合法');
  65 +
  66 + $model = new \app\api\model\Cart();
  67 + $model->where('id',$id)->setInc('number',$number);
  68 + $this->success('SUCCESS');
  69 + }
  70 +
  71 + /**
  72 + * @ApiTitle (购物车列表)
  73 + * @ApiMethod (POST)
  74 + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  75 + * @ApiReturn ({
  76 + 'code':'1',
  77 + 'msg':'SUCCESS'
  78 + })
  79 + */
  80 + public function cartList()
  81 + {
  82 + $model = new \app\api\model\Cart();
  83 + $list = $model
  84 + ->with(['goods','sku'])
  85 + ->where('user_id',$this->auth->id)
  86 + ->select();
  87 + $this->success('SUCCESS',$list);
  88 + }
  89 +}
  1 +<?php
  2 +
  3 +
  4 +namespace app\api\model;
  5 +
  6 +
  7 +use think\Model;
  8 +
  9 +class Cart extends Model
  10 +{
  11 + protected $createTime = 'createtime';
  12 + protected $updateTime = '';
  13 +
  14 + public function goods()
  15 + {
  16 + return $this->belongsTo('Goods','goods_id','id');
  17 + }
  18 +
  19 + public function sku()
  20 + {
  21 + return $this->belongsTo('GoodsSpec','sku_id','id');
  22 + }
  23 +}