作者 SHW\戥岁。。

购物车加入错误

... ... @@ -4,6 +4,7 @@
namespace app\api\controller;
use app\admin\model\litestore\Litestoregoodsspec;
use app\api\model\GoodsSpec;
use app\api\model\SpecValue;
use app\common\controller\Api;
... ... @@ -38,29 +39,32 @@ class Cart extends Api
if (!is_numeric($sku_id) || !is_numeric($goods_id) || !is_numeric($number)) $this->error('参数不合法');
$category_id = (new \app\api\model\Goods())->where('goods_id', $goods_id)->value('category_id');
$where = [
'goods_id' => $goods_id,
'sku_id' => $sku_id,
'user_id' => $this->auth->id,
];
$data = [
'goods_id' => $goods_id,
'sku_id' => $sku_id,
'user_id' => $this->auth->id,
'category_id' => $category_id,
'sku_id' => $sku_id,
'goods_id' => $goods_id,
];
$model = new \app\api\model\Cart();
$cart = $model->where($where)->find();
if ($number == 0) {
$id = $model->where($where)->find();
if (empty($id)) {
if (empty($cart)) {
$this->error('购物车不存在');
}
$model->where('id', $id['id'])->delete();
$model->where('id', $cart['id'])->delete();
}
$cart = $model->where($data)->find();
$spec_sku = new Litestoregoodsspec();
if ($cart) {
$spec_sku->where('goods_spec_id', $sku_id)->find();
if ($spec_sku['stock_num'] + $cart['number'] < $number) {
$this->error('库存不足');
}
$cart->setInc('number', $number);
} else {
$spec_sku->where('goods_spec_id', $sku_id)->find();
if ($spec_sku['stock_num'] < $number) {
$this->error('库存不足');
}
$data['number'] = $number;
$model->isUpdate(false)->save($data);
}
... ... @@ -106,17 +110,9 @@ class Cart extends Api
{
$id = $this->request->post('id');
$number = $this->request->post('number');
// $sku_id = $this->request->post('sku_id');
// $goods_id = $this->request->post('goods_id');
// $number = $this->request->post('number');
if (!is_numeric($id) || !is_numeric($number)) $this->error('参数不合法');
// $where = [
// 'goods_id' => $goods_id,
// 'sku_id' => $sku_id,
// 'user_id' => $this->auth->id,
// ];
$model = new \app\api\model\Cart();
// $id = $model->where($where)->value('id');
$order = $model->with('sku')->where('id', $id)->find();
if ($number + $order['number'] > $order['sku']['stock_num']) $this->error('已达到最大库存,请勿重复提交');
$model->where('id', $id)->setInc('number', $number);
... ...