Parcourir la source

server-side 支持前端顯示賠付比例

joe il y a 3 ans
Parent
commit
592f149259

+ 3 - 0
app/api/controller/PublicController.php

@@ -21,6 +21,7 @@ use crmeb\services\CacheService;
 use crmeb\services\UtilService;
 use crmeb\services\workerman\ChannelService;
 use think\facade\Cache;
+use think\facade\Config;
 use crmeb\services\upload\Upload;
 use app\models\redis\SystemCarousel;
 
@@ -40,6 +41,7 @@ class PublicController
     public function index(Request $request)
     {
         $filing_info = sys_config('filing_info') ?? '';
+        $show_benefit = Config::get('app.show_benefit', false);
         $banner = sys_data('routine_home_banner') ?: []; //TODO 首页banner图
         $menus = sys_data('routine_home_menus') ?: []; //TODO 首页按钮
         $roll = SystemCarousel::getFirst(20); // sys_data('routine_home_roll_news') ?: [];//TODO 首页滚动新闻 update to carousel
@@ -77,6 +79,7 @@ class PublicController
         $tengxun_map_key = sys_config('tengxun_map_key');
         return app('json')->successful(compact(
             'filing_info',
+            'show_benefit',
             'banner',
             'menus',
             'roll',

+ 25 - 0
app/common.php

@@ -135,6 +135,31 @@ function get_extract_name($extract): array
     return ['未知', ''];
 }
 
+/**
+ * 獲取活動賠付比例
+ * @cid 分類ID
+ */
+function getRate($sid): float
+{
+    $activities = [
+        ThinkConf::get('activity.clearance_cate_id') => ThinkConf::get('activity.clearrance_rate'),
+        ThinkConf::get('activity.lucky_cate_id') => ThinkConf::get('activity.luck_rate'),
+        ThinkConf::get('activity.lucky_a_cate_id') => ThinkConf::get('activity.luck_a_rate'),
+        ThinkConf::get('activity.lucky_b_cate_id') => ThinkConf::get('activity.luck_b_rate'),
+    ];
+    return $activities[$sid] ?? 0.0;
+}
+
+/**
+ * 获取商品需要赔付多少钱
+ * @product: 商品各属性, 必须设置 price, cost 字段
+ * @rate: 当前商品所属活动的赔付百分比
+ */
+function getReputation($product, $rate): float
+{
+    return bcadd(bcmul(bcsub($product['price'], $product['cost'], 2), $rate, 2), $product['price'], 2);
+}
+
 if (!function_exists('exception')) {
     /**
      * 抛出异常处理

+ 26 - 4
app/models/store/StoreProduct.php

@@ -18,6 +18,7 @@ use app\models\store\{
     StoreBargain, StoreCombination, StoreSeckill
 };
 use app\models\user\UserSearch;
+use think\facade\Config;
 
 /**
  * TODO 产品Model
@@ -59,10 +60,19 @@ class StoreProduct extends BaseModel
         return htmlspecialchars_decode($value);
     }
 
-    public static function getValidProduct($productId, $field = 'add_time,browse,cate_id,code_path,ficti,give_integral,id,image,is_sub,is_bargain,is_benefit,is_best,is_del,is_hot,is_new,is_postage,is_seckill,is_show,keyword,mer_id,mer_use,ot_price,postage,price,sales,slider_image,sort,stock,store_info,store_name,unit_name,vip_price,spec_type,IFNULL(sales,0) + IFNULL(ficti,0) as fsales,video_link')
+    public static function getValidProduct($productId, $field = 'add_time,browse,cate_id,code_path,ficti,give_integral,id,image,is_sub,is_bargain,is_benefit,is_best,is_del,is_hot,is_new,is_postage,is_seckill,is_show,keyword,mer_id,mer_use,ot_price,postage,price,sales,slider_image,sort,stock,store_info,store_name,unit_name,vip_price,spec_type,IFNULL(sales,0) + IFNULL(ficti,0) as fsales,video_link,cost')
     {
-        $Product = self::where('is_del', 0)->where('is_show', 1)->where('id', $productId)->field($field)->find();
-        if ($Product) return $Product->toArray();
+        $product = self::where('is_del', 0)->where('is_show', 1)->where('id', $productId)->field($field)->find();
+        if ($product) {
+            $product = $product->toArray();
+
+            if (Config::get('app.show_benefit')) {
+                $rate = getRate($product['cate_id']);
+                $product['reputation'] = '赔付' . getReputation($product, $rate);
+            }
+            unset($product['cost']);
+            return $product;
+        }
         else return false;
     }
 
@@ -117,7 +127,13 @@ class StoreProduct extends BaseModel
         if ($salesOrder) $baseOrder = $salesOrder == 'desc' ? 'sales DESC' : 'sales ASC';//虚拟销量
         if ($baseOrder) $baseOrder .= ', ';
         $model->order($baseOrder . 'sort DESC, add_time DESC');
-        $list = $model->page((int)$page, (int)$limit)->field('id,store_name,cate_id,image,IFNULL(sales,0) + IFNULL(ficti,0) as sales,price,stock,spec_type')->select()->each(function ($item) use ($uid, $type) {
+        $show_benefit = Config::get('app.show_benefit', false);
+        $fields = 'id,store_name,cate_id,image,IFNULL(sales,0) + IFNULL(ficti,0) as sales,price,stock,spec_type';
+        if ($show_benefit) {
+            $fields .= ',cost';
+        }
+        $list = $model->page((int)$page, (int)$limit)->field($fields)->select()->each(function ($item) 
+            use ($uid, $type, $show_benefit, $sId) {
             if ($type) {
                 if ($item['spec_type']) {
                     $item['is_att'] = StoreProductAttrValueModel::where(['product_id' => $item['id'], 'type' => 0])->count() ? true : false;
@@ -128,6 +144,12 @@ class StoreProduct extends BaseModel
                 else $item['cart_num'] = 0;
                 if (is_null($item['cart_num'])) $item['cart_num'] = 0;
             }
+            // show benefit
+            if ($show_benefit) {
+                $rate = getRate($sId);
+                $item['reputation'] = '赔付' . getReputation($item, $rate);
+                unset($item['cost']);
+            }
         });
         $list = count($list) ? $list->toArray() : [];
         if (!empty($list)) {

+ 1 - 1
config/activity.php

@@ -4,7 +4,7 @@ return [
     // 根分类ID
     'root_cate_id' => 10,
     // 是否开启挖矿活动
-    'mining_enabled' => false,
+    'mining_enabled' => true,
     'mining_display_name' => '黑洞星球',
     'mining_display_pic' => 'http://x.png',
     // 挖矿活动ID

+ 2 - 0
config/app.php

@@ -43,6 +43,8 @@ return [
     // 显示错误信息
     'show_error_msg'   => false,
 
+    // 前端是否顯示商品賠付盈利
+    'show_benefit' => false,
     'off_days_before'  => 30,   // 超過(n)天數下架
     // 部署的云服务器或 VPS IP 地址
     'server_ip'        => '81.70.81.74',

+ 1 - 0
tw/command/Maintain.php

@@ -288,6 +288,7 @@ class Maintain extends Command
      */
     protected function trash()
     {
+        echo getRate(5);
     }
 
     /**