Browse Source

fix:增加是否热门字段

super-yimizi 1 month ago
parent
commit
9bb77df0b9

+ 3 - 25
application/api/controller/Goods.php

@@ -20,29 +20,7 @@ use app\common\Service\SkuSpec as SkuSpecService;
  */
 class Goods extends Base
 {
-    protected $noNeedLogin = ['index', 'detail', 'lists', 'getWxCode'];
-
-    //首页推荐商品
-    public function index()
-    {
-        $hots = GoodsModel::where('status', 'normal')
-            ->order('weigh desc')
-            ->limit(12)
-            ->cache(false)
-            ->select();
-        $recommends = GoodsModel::getIndexGoodsList();
-        foreach ($hots as $item) {
-            $item->visible(explode(',', 'id,title,price,marketprice,sales,views,image'));
-        }
-        foreach ($recommends as $item) {
-            $item->visible(explode(',', 'id,title,price,marketprice,sales,views,image'));
-        }
-        $this->success('获取成功', [
-            'hots'       => $hots,
-            'recommends' => $recommends
-        ]);
-    }
-
+    protected $noNeedLogin = [ 'detail', 'lists', 'getWxCode'];
     //详情
     public function detail()
     {
@@ -136,7 +114,7 @@ class Goods extends Base
         // }
         // $row->coupon = $couponList;
 
-        $row->visible(explode(',', 'id,title,type,spec_type,subtitle,category_ids,price,marketprice,sales,views,
+        $row->visible(explode(',', 'id,title,type,spec_type,sub_title,category_ids,price,lineation_price,sales,views,is_hot,
         image,content,images,sku_spec,sku,comment,is_collect,guarantee,attributes,favor_rate,coupon'));
         $row = $row->toArray();
         $row['content'] = \app\common\library\Service::formatTplToUniapp($row['content']);
@@ -210,7 +188,7 @@ class Goods extends Base
         $list = $query->paginate($pageSize);
 
         foreach ($list as $item) {
-            $item->visible(explode(',', 'id,title,image,price,sales,views,description,marketprice,createtime'));
+            $item->visible(explode(',', 'id,title,image,price,sales,views,description,lineation_price,is_hot,createtime'));
         }
 
         $this->success('', $list);

+ 81 - 0
application/common/Service/DiscountService.php

@@ -295,4 +295,85 @@ class DiscountService
             'seconds' => $seconds
         ];
     }
+
+     /**
+     * 获取商品的折扣信息
+     * @param $value
+     * @param $data
+     * @return array|null
+     */
+    public function getDiscountInfoAttr($value, $data)
+    {
+        if (!isset($data['id'])) {
+            return null;
+        }
+        
+        // 静态缓存,避免重复查询
+        static $discountCache = [];
+        $goodsId = $data['id'];
+        
+        if (!isset($discountCache[$goodsId])) {
+            $discountCache[$goodsId] = \app\common\Service\DiscountService::getBatchGoodsDiscountInfo([$goodsId]);
+        }
+        
+        return isset($discountCache[$goodsId][$goodsId]) ? $discountCache[$goodsId][$goodsId] : [];
+    }
+    
+    /**
+     * 检查商品是否有活动折扣
+     * @param int $skuId 规格ID,单规格商品传0
+     * @return bool
+     */
+    public function hasDiscount($skuId = 0)
+    {
+        return self::hasActiveDiscount($this->id, $skuId);
+    }
+    
+    /**
+     * 获取商品的最低折扣价格
+     * @return float|null
+     */
+    public function getMinDiscountPrice()
+    {
+        $discountInfo = $this->discount_info;
+        
+        if (empty($discountInfo)) {
+            return null;
+        }
+        
+        $minPrice = null;
+        foreach ($discountInfo as $item) {
+            $price = floatval($item['discount_price']);
+            if ($minPrice === null || $price < $minPrice) {
+                $minPrice = $price;
+            }
+        }
+        
+        return $minPrice;
+    }
+    
+    /**
+     * 获取商品的最大折扣力度
+     * @return float|null
+     */
+    public function getMaxDiscount()
+    {
+        $discountInfo = $this->discount_info;
+        
+        if (empty($discountInfo)) {
+            return null;
+        }
+        
+        $maxDiscount = null;
+        foreach ($discountInfo as $item) {
+            $discount = floatval($item['discount']);
+            if ($maxDiscount === null || $discount < $maxDiscount) { // 折扣越小力度越大
+                $maxDiscount = $discount;
+            }
+        }
+        
+        return $maxDiscount;
+    }
+
+}
 } 

+ 1 - 80
application/common/model/Goods.php

@@ -26,7 +26,6 @@ class Goods extends Model
     protected $deleteTime = 'deletetime';
     // 追加属性
     protected $append = [
-        'discount_info'
     ];
     protected static $config = [];
 
@@ -236,83 +235,5 @@ class Goods extends Model
         return $this->belongsTo('Brand', 'brand_id', 'id', [], 'LEFT');
     }
     
-    /**
-     * 获取商品的折扣信息
-     * @param $value
-     * @param $data
-     * @return array|null
-     */
-    public function getDiscountInfoAttr($value, $data)
-    {
-        if (!isset($data['id'])) {
-            return null;
-        }
-        
-        // 静态缓存,避免重复查询
-        static $discountCache = [];
-        $goodsId = $data['id'];
-        
-        if (!isset($discountCache[$goodsId])) {
-            $discountCache[$goodsId] = \app\common\Service\DiscountService::getBatchGoodsDiscountInfo([$goodsId]);
-        }
-        
-        return isset($discountCache[$goodsId][$goodsId]) ? $discountCache[$goodsId][$goodsId] : [];
-    }
-    
-    /**
-     * 检查商品是否有活动折扣
-     * @param int $skuId 规格ID,单规格商品传0
-     * @return bool
-     */
-    public function hasDiscount($skuId = 0)
-    {
-        return \app\common\Service\DiscountService::hasActiveDiscount($this->id, $skuId);
-    }
-    
-    /**
-     * 获取商品的最低折扣价格
-     * @return float|null
-     */
-    public function getMinDiscountPrice()
-    {
-        $discountInfo = $this->discount_info;
-        
-        if (empty($discountInfo)) {
-            return null;
-        }
-        
-        $minPrice = null;
-        foreach ($discountInfo as $item) {
-            $price = floatval($item['discount_price']);
-            if ($minPrice === null || $price < $minPrice) {
-                $minPrice = $price;
-            }
-        }
-        
-        return $minPrice;
-    }
-    
-    /**
-     * 获取商品的最大折扣力度
-     * @return float|null
-     */
-    public function getMaxDiscount()
-    {
-        $discountInfo = $this->discount_info;
-        
-        if (empty($discountInfo)) {
-            return null;
-        }
-        
-        $maxDiscount = null;
-        foreach ($discountInfo as $item) {
-            $discount = floatval($item['discount']);
-            if ($maxDiscount === null || $discount < $maxDiscount) { // 折扣越小力度越大
-                $maxDiscount = $discount;
-            }
-        }
-        
-        return $maxDiscount;
-    }
-
+   
 }