From dd9c1e0db492080c085df5b3be726aa209307a21 Mon Sep 17 00:00:00 2001 From: wangxiaowei Date: Tue, 29 Oct 2024 15:37:34 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=9C=AC=E5=9C=B0=E7=94=9F?= =?UTF-8?q?=E6=B4=BB=E8=B4=AD=E7=89=A9=E8=BD=A6=E5=95=86=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/LocalStoreGoods.php | 11 +-- app/api/logic/LocalStoreGoodsCartLogic.php | 82 +++++++++++++++++++ app/api/logic/LocalStoreGoodsLogic.php | 55 ------------- .../model/localstore/LocalStoreGoodsCart.php | 22 +++++ 4 files changed, 110 insertions(+), 60 deletions(-) create mode 100644 app/api/logic/LocalStoreGoodsCartLogic.php diff --git a/app/api/controller/LocalStoreGoods.php b/app/api/controller/LocalStoreGoods.php index 4e7f4a35..03db80e6 100755 --- a/app/api/controller/LocalStoreGoods.php +++ b/app/api/controller/LocalStoreGoods.php @@ -2,16 +2,17 @@ namespace app\api\controller; +use app\api\logic\LocalStoreGoodsCartLogic; use app\api\logic\LocalStoreGoodsLogic; use app\api\validate\LocalStoreGoodsCartValidate; use app\api\validate\LocalStoreGoodsValidate; use app\common\basics\Api; -use app\common\model\localstore\LocalStoreGoodsCart; + use app\common\server\JsonServer; class LocalStoreGoods extends Api { - public $like_not_need_login = ['lists', 'addCart', 'num']; + public $like_not_need_login = ['lists', 'addCart', 'getCartGoods', 'num']; // 商品列表 public function lists() @@ -105,7 +106,7 @@ class LocalStoreGoods extends Api (new LocalStoreGoodsCartValidate())->goCheck('add', ['user_id' => $this->user_id]); try { - $result = LocalStoreGoodsLogic::addCart($form, $this->user_id); + $result = LocalStoreGoodsCartLogic::addCart($form, $this->user_id); if ($result) { return JsonServer::success('操作成功'); } @@ -118,13 +119,13 @@ class LocalStoreGoods extends Api public function getCartGoods() { $form = $this->request->post(); - + LocalStoreGoodsCartLogic::getCartGoods($form, $this->user_id); } // 获取购物车数量 public function num() { $form = $this->request->post(); - return JsonServer::success('', LocalStoreGoodsLogic::cartNum($form, $this->user_id)); + return JsonServer::success('', LocalStoreGoodsCartLogic::cartNum($form, $this->user_id)); } } \ No newline at end of file diff --git a/app/api/logic/LocalStoreGoodsCartLogic.php b/app/api/logic/LocalStoreGoodsCartLogic.php new file mode 100644 index 00000000..2d1db554 --- /dev/null +++ b/app/api/logic/LocalStoreGoodsCartLogic.php @@ -0,0 +1,82 @@ + $post['lsg_id'], 'state' => 1, 'audit_status' => 1, 'delete_time' => 0])->find(); + if (empty($goods)) { + throw new \Exception('商品信息错误'); + } + + $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]) + ->update([ + 'goods_num' => $post['goods_num'] + $cart['goods_num'], + ]); + } else { + // 购物车创建商品 + LocalStoreGoodsCart::create([ + 'user_id' => $user_id, + 'lstore_id' => $post['lstore_id'], + 'lsg_id' => $post['lsg_id'], + 'item_id' => $post['item_id'], + 'goods_num' => $post['goods_num'], + 'create_time' => time() + ]); + } + Db::commit(); + return true; + } catch (Exception $e) { + Db::rollback(); + self::$error = $e->getMessage(); + return false; + } + } + + // 获取对应的购物车商品 + public static function getCartGoods($post, $user_id) + { + $cart = LocalStoreGoodsCart::alias('c') + ->join('local_store_goods g', 'c.lsg_id = g.id') + ->where(['c.user_id' => $user_id, 'c.lstore_id' => $post['lstore_id'], 'c.is_delete' => 0, 'g.state' => 1, 'g.audit_status' => 1, 'g.delete_time' => 0]) + ->field('c.id, c.item_id, c.goods_num, g.id as lsg_id, g.title, g.price') + ->append(['pic', 'item']) + ->select(); + } + + public static function cartNum($post, $user_id) + { + $num = LocalStoreGoodsCart::alias('c') + ->join('local_store_goods g', 'c.lsg_id = g.id') + ->where(['c.user_id' => $user_id, 'c.lstore_id' => $post['lstore_id'], 'c.is_delete' => 0, 'g.state' => 1, 'g.audit_status' => 1, 'g.delete_time' => 0]) + ->sum('c.goods_num'); + return ['num' => $num ?? 0]; + } +} \ No newline at end of file diff --git a/app/api/logic/LocalStoreGoodsLogic.php b/app/api/logic/LocalStoreGoodsLogic.php index 41b692f5..d6d7043b 100644 --- a/app/api/logic/LocalStoreGoodsLogic.php +++ b/app/api/logic/LocalStoreGoodsLogic.php @@ -380,59 +380,4 @@ class LocalStoreGoodsLogic extends Logic LocalStoreGoodsItem::insertAll($data); } } - - public static function addCart($post, $user_id) - { - Db::startTrans(); - try { - // 验证商品 - $goods = LocalStoreGoods::where(['id' => $post['lsg_id'], 'state' => 1, 'audit_status' => 1, 'delete_time' => 0])->find(); - if (empty($goods)) { - throw new \Exception('商品信息错误'); - } - - $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]) - ->update([ - 'goods_num' => $post['goods_num'] + $cart['goods_num'], - ]); - } else { - // 购物车创建商品 - LocalStoreGoodsCart::create([ - 'user_id' => $user_id, - 'lstore_id' => $post['lstore_id'], - 'lsg_id' => $post['lsg_id'], - 'item_id' => $post['item_id'], - 'goods_num' => $post['goods_num'], - 'create_time' => time() - ]); - } - Db::commit(); - return true; - } catch (Exception $e) { - Db::rollback(); - self::$error = $e->getMessage(); - return false; - } - } - - public static function cartNum($post, $user_id) - { - $num = LocalStoreGoodsCart::where(['user_id' => $user_id, 'lstore_id' => $post['lstore_id'], 'is_delete' => 0])->sum('goods_num'); - return ['num' => $num ?? 0]; - } } \ No newline at end of file diff --git a/app/common/model/localstore/LocalStoreGoodsCart.php b/app/common/model/localstore/LocalStoreGoodsCart.php index d162bc4d..73021466 100644 --- a/app/common/model/localstore/LocalStoreGoodsCart.php +++ b/app/common/model/localstore/LocalStoreGoodsCart.php @@ -6,5 +6,27 @@ use app\common\basics\Models; class LocalStoreGoodsCart extends Models { + public function goods() + { + return $this->hasOne(LocalStoreGoods::class, 'id', 'lsg_id'); + } + // 商品规格为空,获取封面图 + public function getPicAttr($value, $data) + { + if ($data['item_id'] == 0) { + $pic = LocalStoreGoodsPic::where('lsg_id', $data['lsg_id'])->field('url')->find(); + if ($pic) { + return $pic['url']; + } + } + } + + // 获取带有商品规格的属性 + public function getItemAttr($value, $data) + { + if ($data['item_id'] > 0) { + return LocalStoreGoodsItem::where('id', $data['item_id'])->field('image, spec_value_str, price')->find()->toArray(); + } + } } \ No newline at end of file