Wang Chen 4 年之前
父节点
当前提交
f23febb4d0

+ 1 - 1
app/Http/Controllers/QuickApp/Activity/Transformers/ReBuildData.php

@@ -51,7 +51,7 @@ class ReBuildData
 
             // 支付产品信息
             $product = collect($products)->firstWhere('id', $productId);
-            $price   = getProp($product, 'price');
+            $price   = (float)getProp($product, 'price');
             $total   = $price * 100 + (int)getProp($product, 'given');
 
             $result[] = [

+ 48 - 20
app/Modules/AdPosition/Services/AdPositionService.php

@@ -26,7 +26,7 @@ class AdPositionService
             return [];
         }
 
-        // 获取广告位
+        // 获取所有有效广告位
         $positions = AdPosition::getAllValidAdPositions();
         if (empty($positions)) {
             return [];
@@ -39,6 +39,9 @@ class AdPositionService
             return [];
         }
 
+        // 获取用户相关数据
+        $userData = $this->getUserFilterData($uid);
+
         // 针对分配数据筛选出优先级最高的分配任务
         $result     = [];
         $dispatches = collect($dispatches)->groupBy('ad_position_id')->all();
@@ -47,17 +50,27 @@ class AdPositionService
             $positionId = getProp($position, 'id');
             $ident      = getProp($position, 'ident');
 
-            // 获取优先级最高的任务
+            // 获取对应广告位所有任务
             $validDispatches = getProp($dispatches, $positionId, []);
-            $validDispatch   = $validDispatches ? $validDispatches[0] : [];
-            if (empty($validDispatch)) {
+            if (empty($validDispatches)) {
                 continue;
             }
 
-            // 过滤条件判断
-            $filterContentJson = getProp($validDispatch, 'filter_condition');
-            $matchFilter       = $this->filterDispatch($uid, $filterContentJson);
-            if (!$matchFilter) {
+            // 根据优先级循环匹配,直到匹配到为止
+            $validDispatches = collect($validDispatches)->sortByDesc('priority')->all();
+            $validDispatch   = [];
+            foreach ($validDispatches as $value) {
+                // 过滤条件判断
+                $filterContentJson = getProp($value, 'filter_condition');
+                $matchFilter       = $this->filterDispatch($filterContentJson, $userData);
+                if ($matchFilter) {
+                    $validDispatch = $value;
+                    break;
+                }
+            }
+
+            // 判断
+            if (empty($validDispatch)) {
                 continue;
             }
 
@@ -69,16 +82,35 @@ class AdPositionService
     }
 
     /**
-     * 过滤条件筛选
+     * 用户的数据
      * @param $uid
+     * @return array
+     */
+    private function getUserFilterData($uid): array
+    {
+        // 获取用户信息
+        $user         = QappUser::getUserByUid($uid);
+        $registerAt   = getProp($user, 'created_at');
+        $registerUnix = strtotime($registerAt);
+
+        // 获取用户付费情况
+        $order  = OrderService::getUserLastestOrder($uid);
+        $isPaid = $order ? 1 : 0;
+
+        return compact('registerUnix', 'isPaid');
+    }
+
+    /**
+     * 过滤条件筛选
      * @param $filterContentJson
+     * @param $userData
      * @return bool
      */
-    private function filterDispatch($uid, $filterContentJson): bool
+    private function filterDispatch($filterContentJson, $userData): bool
     {
         // 过滤条件
         $filterContent = json_decode($filterContentJson, true);
-        if (empty($uid) || empty($filterContentJson) || empty($filterContent)) {
+        if (empty($userData) || empty($filterContentJson) || empty($filterContent)) {
             return true;
         }
 
@@ -88,11 +120,7 @@ class AdPositionService
         if ($registerAtArr) {
             // 默认值
             $registerFilter = false;
-
-            // 获取用户信息
-            $user         = QappUser::getUserByUid($uid);
-            $registerAt   = getProp($user, 'created_at');
-            $registerUnix = strtotime($registerAt);
+            $registerUnix   = $userData['registerUnix'];
 
             // 循环条件,满足一条即可
             foreach ($registerAtArr as $register) {
@@ -113,19 +141,19 @@ class AdPositionService
         $paidFilter = true;
         if (getProp($filterContent, 'isPaid') !== '') {
             // 获取用户付费情况
-            $order = OrderService::getUserLastestOrder($uid);
+            $isPaid = getProp($userData, 'isPaid', 0);
 
             // 有付费
-            if ($order && getProp($filterContent, 'isPaid') === 1) {
+            if ($isPaid && getProp($filterContent, 'isPaid') === 1) {
                 $paidFilter = true;
             }
 
             // 未付费
-            if (!$order && getProp($filterContent, 'isPaid') === 0) {
+            if (!$isPaid && getProp($filterContent, 'isPaid') === 0) {
                 $paidFilter = true;
             }
         }
-        
+
         // 两者全部满足条件才可展示
         return $registerFilter && $paidFilter;
     }