diff --git a/app/api/logic/LocalStoreGoodsLogic.php b/app/api/logic/LocalStoreGoodsLogic.php index 8bd5b172..41b692f5 100644 --- a/app/api/logic/LocalStoreGoodsLogic.php +++ b/app/api/logic/LocalStoreGoodsLogic.php @@ -392,6 +392,18 @@ class LocalStoreGoodsLogic extends Logic } $cart = LocalStoreGoodsCart::where(['user_id' => $user_id, 'lstore_id' => $post['lstore_id'], 'lsg_id' => $post['lsg_id'], 'item_id' => $post['item_id'], 'is_delete' => 0])->find(); + // 验证商品库存 + $item = LocalStoreGoodsItem::where(['lstore_id' => $post['lstore_id'], 'goods_id' => $post['lsg_id']])->field('stock')->find(); + + // 普通商品 + if ($post['item_id'] == 0 && $post['goods_num'] > 999) { + throw new \Exception('商品库存不足'); + } + + if ($post['item_id'] > 0 && ($post['goods_num'] + $cart['goods_num']) > $item['stock']) { + throw new \Exception('商品库存不足'); + } + if ($cart) { // 如果存在于购物车,就增加数量 LocalStoreGoodsCart::where(['user_id' => $user_id, 'lstore_id' => $post['lstore_id'], 'lsg_id' => $post['lsg_id'], 'item_id' => $post['item_id'], 'is_delete' => 0])