作者 李忠强

更新

<?php
namespace app\api\controller;
use app\common\controller\Api;
/**
* 购物车
*/
class Cart extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* @ApiTitle (加入购物车)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="sku_id", type="integer", required=true, description="规格id")
* @ApiParams (name="goods_id", type="integer", required=true, description="商品id")
* @ApiParams (name="number", type="integer", required=true, description="购买数量")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
})
*/
public function addCart()
{
$sku_id = $this->request->post('sku_id');
$goods_id = $this->request->post('goods_id');
$number = $this->request->post('number');
if (!is_numeric($sku_id) || !is_numeric($goods_id) || !is_numeric($number)) $this->error('参数不合法');
$data = [
'user_id' => $this->auth->id,
'sku_id' => $sku_id,
'goods_id' => $goods_id,
'number' => $number,
];
$model = new \app\api\model\Cart();
$model->save($data);
$this->success('SUCCESS');
}
/**
* @ApiTitle (增加购物车数量)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="购物车id")
* @ApiParams (name="number", type="integer", required=true, description="增加数量")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
})
*/
public function addCartNumber()
{
$id = $this->request->post('id');
$number = $this->request->post('number');
if (!is_numeric($id) || !is_numeric($number)) $this->error('参数不合法');
$model = new \app\api\model\Cart();
$model->where('id',$id)->setInc('number',$number);
$this->success('SUCCESS');
}
/**
* @ApiTitle (购物车列表)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
})
*/
public function cartList()
{
$model = new \app\api\model\Cart();
$list = $model
->with(['goods','sku'])
->where('user_id',$this->auth->id)
->select();
$this->success('SUCCESS',$list);
}
}
\ No newline at end of file
... ...
<?php
namespace app\api\model;
use think\Model;
class Cart extends Model
{
protected $createTime = 'createtime';
protected $updateTime = '';
public function goods()
{
return $this->belongsTo('Goods','goods_id','id');
}
public function sku()
{
return $this->belongsTo('GoodsSpec','sku_id','id');
}
}
\ No newline at end of file
... ...