Pārlūkot izejas kodu

优化:购物车列表显示会员折扣价

Yahoo 4 gadi atpakaļ
vecāks
revīzija
c9b7ebfd4f

+ 15 - 13
app/api/controller/Cart.php

@@ -15,6 +15,7 @@ namespace app\api\controller;
 use app\api\model\Cart as CartModel;
 use app\api\service\Cart as CartService;
 use app\common\exception\BaseException;
+use think\response\Json;
 
 /**
  * 购物车管理
@@ -25,13 +26,14 @@ class Cart extends Controller
 {
     /**
      * 购物车商品列表
-     * @return array|\think\response\Json
+     * @return Json
      * @throws BaseException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
+     * @throws \cores\exception\BaseException
      */
-    public function list()
+    public function list(): Json
     {
         // 购物车商品列表
         $service = new CartService;
@@ -43,10 +45,11 @@ class Cart extends Controller
 
     /**
      * 购物车商品总数量
-     * @return array|\think\response\Json
+     * @return Json
      * @throws BaseException
      */
-    public function total() {
+    public function total(): Json
+    {
         $model = new CartModel;
         $cartTotal = $model->getCartTotal();
         return $this->renderSuccess(compact('cartTotal'));
@@ -54,16 +57,16 @@ class Cart extends Controller
 
     /**
      * 加入购物车
-     * @param int $goodsId 商品id
+     * @param int $goodsId 商品ID
      * @param string $goodsSkuId 商品sku索引
      * @param int $goodsNum 商品数量
-     * @return array|\think\response\Json
+     * @return Json
      * @throws BaseException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function add(int $goodsId, string $goodsSkuId, int $goodsNum)
+    public function add(int $goodsId, string $goodsSkuId, int $goodsNum): Json
     {
         $model = new CartModel;
         if (!$model->add($goodsId, $goodsSkuId, $goodsNum)) {
@@ -76,16 +79,16 @@ class Cart extends Controller
 
     /**
      * 更新购物车商品数量
-     * @param int $goodsId 商品id
+     * @param int $goodsId 商品ID
      * @param string $goodsSkuId 商品sku索引
      * @param int $goodsNum 商品数量
-     * @return array|\think\response\Json
+     * @return Json
      * @throws BaseException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function update(int $goodsId, string $goodsSkuId, int $goodsNum)
+    public function update(int $goodsId, string $goodsSkuId, int $goodsNum): Json
     {
         $model = new CartModel;
         if (!$model->sUpdate($goodsId, $goodsSkuId, $goodsNum)) {
@@ -99,10 +102,10 @@ class Cart extends Controller
     /**
      * 删除购物车中指定记录
      * @param array $cartIds 购物车ID集, 如果为空删除所有
-     * @return array|\think\response\Json
+     * @return Json
      * @throws BaseException
      */
-    public function clear(array $cartIds = [])
+    public function clear(array $cartIds = []): Json
     {
         $model = new CartModel;
         if (!$model->clear($cartIds)) {
@@ -112,5 +115,4 @@ class Cart extends Controller
         $cartTotal = $model->getCartTotal();
         return $this->renderSuccess(compact('cartTotal'), '操作成功');
     }
-
 }

+ 26 - 6
app/api/model/Goods.php

@@ -12,11 +12,11 @@ declare (strict_types=1);
 
 namespace app\api\model;
 
-use app\api\model\GoodsSpecRel as GoodsSpecRelModel;
+use app\api\service\Goods as GoodsService;
 use app\api\service\user\Grade as UserGradeService;
+use app\api\model\GoodsSpecRel as GoodsSpecRelModel;
 use app\common\model\Goods as GoodsModel;
 use app\common\enum\goods\Status as GoodsStatusEnum;
-use app\common\library\helper;
 use cores\exception\BaseException;
 
 /**
@@ -114,6 +114,20 @@ class Goods extends GoodsModel
         return $this->setGoodsListDataFromApi($data);
     }
 
+    /**
+     * 获取商品指定的sku信息并且设置商品的会员价
+     * @param $goodsInfo
+     * @param string $goodsSkuId
+     * @return \app\common\model\GoodsSku|array|null
+     * @throws BaseException
+     */
+    public static function getSkuInfo($goodsInfo, string $goodsSkuId)
+    {
+        $goodsInfo['skuInfo'] = GoodsService::getSkuInfo($goodsInfo['goods_id'], $goodsSkuId);
+        (new static)->setGoodsGradeMoney($goodsInfo);
+        return $goodsInfo['skuInfo'];
+    }
+
     /**
      * 设置商品展示的数据 api模块
      * @param $data
@@ -122,7 +136,7 @@ class Goods extends GoodsModel
     private function setGoodsListDataFromApi($data)
     {
         return $this->setGoodsListData($data, function ($goods) {
-            // 计算并设置商品会员价
+            // 整理商品数据 api模块
             $this->setGoodsDataFromApi($goods);
         });
     }
@@ -169,9 +183,15 @@ class Goods extends GoodsModel
         // 会员折扣价: 商品基础价格
         $goods['goods_price_min'] = UserGradeService::getDiscountPrice($goods['goods_price_min'], $discountRatio);
         $goods['goods_price_max'] = UserGradeService::getDiscountPrice($goods['goods_price_max'], $discountRatio);
-        // 会员折扣价: 商品sku
-        foreach ($goods['skuList'] as &$skuItem) {
-            $skuItem['goods_price'] = UserGradeService::getDiscountPrice($skuItem['goods_price'], $discountRatio);
+        // 会员折扣价: 商品sku列表
+        if ($goods->getRelation('skuList')) {
+            foreach ($goods['skuList'] as &$skuItem) {
+                $skuItem['goods_price'] = UserGradeService::getDiscountPrice($skuItem['goods_price'], $discountRatio);
+            }
+        }
+        // 会员折扣价: 已选择的商品sku(用于购物车)
+        if ($goods->getAttr('skuInfo')) {
+            $goods['skuInfo']['goods_price'] = UserGradeService::getDiscountPrice($goods['skuInfo']['goods_price'], $discountRatio);
         }
     }
 }

+ 9 - 11
app/api/service/Cart.php

@@ -14,9 +14,8 @@ namespace app\api\service;
 
 use app\api\model\Cart as CartModel;
 use app\api\model\Goods as GoodsModel;
-use app\api\model\GoodsSku as GoodsSkuModel;
 use app\api\service\User as UserService;
-use app\common\exception\BaseException;
+use cores\exception\BaseException;
 use app\common\library\helper;
 use app\common\service\BaseService;
 
@@ -68,7 +67,7 @@ class Cart extends BaseService
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function getOrderGoodsList(array $cartIds = [])
+    public function getOrderGoodsList(array $cartIds = []): array
     {
         // 购物车列表
         $cartList = $this->getList($cartIds);
@@ -95,18 +94,19 @@ class Cart extends BaseService
      * @param mixed $goodsList 商品列表
      * @param CartModel $item 购物车记录
      * @return false|mixed
+     * @throws BaseException
      */
     private function findGoods($goodsList, CartModel $item)
     {
         // 查找商品记录
-        $result = helper::getArrayItemByColumn($goodsList, 'goods_id', $item['goods_id']);
-        if (empty($result)) {
+        $goodsInfo = helper::getArrayItemByColumn($goodsList, 'goods_id', $item['goods_id']);
+        if (empty($goodsInfo)) {
             return false;
         }
         // 获取当前选择的商品SKU信息
-        $result['skuInfo'] = GoodsSkuModel::detail($result['goods_id'], $item['goods_sku_id']);
+        $goodsInfo['skuInfo'] = GoodsModel::getSkuInfo($goodsInfo, $item['goods_sku_id']);
         // 这里需要用到clone, 因对象是引用传递 后面的值会覆盖前面的
-        return clone $result;
+        return clone $goodsInfo;
     }
 
     /**
@@ -115,7 +115,7 @@ class Cart extends BaseService
      * @return bool
      * @throws BaseException
      */
-    public function clear(array $cartIds = [])
+    public function clear(array $cartIds = []): bool
     {
         $model = new CartModel;
         return $model->clear($cartIds);
@@ -141,7 +141,7 @@ class Cart extends BaseService
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    private function getCartList(array $cartIds = [])
+    private function getCartList(array $cartIds = []): \think\Collection
     {
         // 当前用户ID
         $userId = UserService::getCurrentLoginUserId();
@@ -158,6 +158,4 @@ class Cart extends BaseService
             ->where('is_delete', '=', 0)
             ->select();
     }
-
-
 }

+ 1 - 2
app/common/model/Goods.php

@@ -263,8 +263,7 @@ class Goods extends BaseModel
         // 商品状态
         $status > 0 && $filter[] = ['status', '=', $status];
         // 获取商品列表数据
-        $data = $this
-            // ->withoutField(['content'])
+        $data = $this->withoutField(['content'])
             ->with(['images.file'])
             ->where($filter)
             ->where('is_delete', '=', 0)