Browse Source

add:首页option;

Wang Chen 4 years ago
parent
commit
0677961710

+ 10 - 0
app/Cache/AdPosition/AdPositionCache.php

@@ -0,0 +1,10 @@
+<?php
+
+
+namespace App\Cache\AdPosition;
+
+
+class AdPositionCache
+{
+
+}

+ 3 - 0
app/Cache/CacheKeys.php

@@ -25,6 +25,9 @@ class CacheKeys
         'push'     => [
             'user' => 'Push:%s'
         ],
+        'ad'       => [
+            'position' => 'Ad:position'
+        ],
         'activity' => [
             'statsPv' => 'Activity:pv:%s:%s',
             'statsUv' => 'Activity:uv:%s:%s',

+ 2 - 1
app/Http/Controllers/QuickApp/Activity/ActivityController.php

@@ -10,6 +10,7 @@ use App\Libs\Utils;
 use App\Modules\Activity\Services\ActivityService;
 use App\Modules\Channel\Services\ChannelService;
 use App\Modules\Product\Services\ProductService;
+use App\Modules\SendOrder\Models\QappSendOrder;
 use App\Modules\SendOrder\Models\QuickAppSendOrder;
 use App\Modules\Subscribe\Services\OrderService;
 use App\Modules\User\Models\QappChannelAccount;
@@ -207,7 +208,7 @@ class ActivityController extends BaseController
         $sendOrderId   = $this->send_order_id;
         $qappAccountId = (int)getProp($activity, 'qapp_account_id');
         if ($qappAccountId && $sendOrderId) {
-            $sendOrder   = QuickAppSendOrder::getSendOrderById($sendOrderId);
+            $sendOrder   = QappSendOrder::getSendOrderById($sendOrderId);
             $qappAccount = QappChannelAccount::getByAccount(getProp($sendOrder, 'account'));
             if ($qappAccountId !== (int)getProp($qappAccount, 'id')) {
                 Utils::throwError(ErrorConst::ACTIVITY_INVALID);

+ 16 - 3
app/Http/Controllers/QuickApp/WelcomeController.php

@@ -2,6 +2,10 @@
 
 namespace App\Http\Controllers\QuickApp;
 
+use App\Modules\AdPosition\Services\AdPositionService;
+use App\Modules\SendOrder\Models\QappSendOrder;
+use App\Modules\SendOrder\Models\QuickAppSendOrder;
+use App\Modules\User\Models\QappChannelAccount;
 use App\Modules\User\Services\QappPackageService;
 use Illuminate\Http\Request;
 use App\Modules\OfficialAccount\Services\CustomMsgService;
@@ -103,12 +107,21 @@ class WelcomeController extends BaseController
     public function getOptions(Request $request)
     {
         // 获取包名
-        $package = $request->header('x-package', '');
+        $package     = $request->header('x-package', '');
+        $sendOrderId = $request->header('send-order-id', '');
 
         // 获取客服信息
         $supports = config('option.supports');
         $support  = getProp($supports, $package, (object)[]);
 
+        // 获取派单信息
+        $adPositions = [];
+        if ($sendOrderId) {
+            $sendOrder      = QappSendOrder::getSendOrderById($sendOrderId);
+            $channelAccount = QappChannelAccount::getByAccount(getProp($sendOrder, 'account'));
+            $adPositions    = AdPositionService::getInstance()->getAdPositions($this->uid, getProp($channelAccount, 'id'));
+        }
+
         // 配置
         $data = [
             'support'     => $support,
@@ -117,8 +130,8 @@ class WelcomeController extends BaseController
                 'pay_back_alert_show' => 1,
             ],
             'position'    => [
-                'home_alert'    => [],
-                'reader_banner' => []
+                'home_alert'    => getProp($adPositions, 'home_alert', []),
+                'reader_banner' => getProp($adPositions, 'reader_banner', [])
             ]
         ];
 

+ 45 - 0
app/Libs/Singleton.php

@@ -0,0 +1,45 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: wangchen
+ * Date: 2019-04-23
+ * Time: 11:42
+ */
+
+namespace App\Libs;
+
+trait Singleton
+{
+    /**
+     * 该属性用来保存实例
+     * @var
+     */
+    private static $instance;
+
+    /**
+     * 创建一个用来实例化对象的方法
+     */
+    public static function getInstance(...$args)
+    {
+        return new static($args);
+//        if (!(self::$instance instanceof self)) {
+//            self::$instance = new static($args); // 后期静态绑定
+//        }
+//        return self::$instance;
+    }
+
+    /**
+     * 构造函数为private,防止创建对象
+     */
+    public function __construct()
+    {
+    }
+
+    /**
+     * 防止对象被复制
+     */
+    private function __clone()
+    {
+        trigger_error('Clone is not allowed!');
+    }
+}

+ 23 - 0
app/Modules/AdPosition/Models/AdPosition.php

@@ -0,0 +1,23 @@
+<?php
+
+
+namespace App\Modules\AdPosition\Models;
+
+
+use Illuminate\Database\Eloquent\Model;
+
+class AdPosition extends Model
+{
+    protected $table = 'qapp_ad_position';
+    protected $fillable = ['name', 'ident', 'max_num', 'content_type', 'property', 'show_type', 'status'];
+
+    /**
+     * 获取所有有效的广告位
+     * @return array
+     */
+    public static function getAllValidAdPositions(): array
+    {
+        $result = self::where('status', 1)->get();
+        return $result ? $result->toArray() : [];
+    }
+}

+ 38 - 0
app/Modules/AdPosition/Models/AdPositionDispatch.php

@@ -0,0 +1,38 @@
+<?php
+
+
+namespace App\Modules\AdPosition\Models;
+
+
+use Illuminate\Database\Eloquent\Model;
+
+class AdPositionDispatch extends Model
+{
+    protected $table = 'qapp_ad_position_dispatch';
+    protected $fillable = ['title', 'ad_position_id', 'link_url', 'link_type', 'image_url', 'desc', 'type', 'content_type',
+        'priority', 'status', 'filter_condition', 'channel_id', 'qapp_account_id', 'start_time', 'end_time'];
+
+    /**
+     * 获取有效的广告位分配
+     * @param $adPositionIds
+     * @param $qappAccountId
+     * @return array
+     */
+    public static function getValidPositionDispatches($adPositionIds, $qappAccountId): array
+    {
+        if (empty($adPositionIds) || empty($qappAccountId)) {
+            return [];
+        }
+
+        $result = self::whereIn('ad_position_id', $adPositionIds)
+            ->where('qapp_account_id', $qappAccountId)
+            ->where('status', 1)
+            ->where('start_time', '<=', date('Y-m-d H:i:s'))
+            ->where('end_time', '>=', date('Y-m-d H:i:s'))
+            ->orderBy('priority', 'DESC')
+            ->orderBy('end_time', 'ASC')
+            ->get();
+
+        return $result ? $result->toArray() : [];
+    }
+}

+ 152 - 0
app/Modules/AdPosition/Services/AdPositionService.php

@@ -0,0 +1,152 @@
+<?php
+
+
+namespace App\Modules\AdPosition\Services;
+
+
+use App\Libs\Singleton;
+use App\Modules\AdPosition\Models\AdPosition;
+use App\Modules\AdPosition\Models\AdPositionDispatch;
+use App\Modules\Trade\Services\OrderService;
+use App\Modules\User\Models\QappUser;
+
+class AdPositionService
+{
+    use Singleton;
+
+    /**
+     * @param $uid
+     * @param $accountId
+     * @return array
+     */
+    public function getAdPositions($uid, $accountId): array
+    {
+        // 获取广告位
+        $positions = AdPosition::getAllValidAdPositions();
+        if (empty($positions)) {
+            return [];
+        }
+
+        // 获取广告位分配信息
+        $positionIds = array_column($positions, 'id');
+        $dispatches  = AdPositionDispatch::getValidPositionDispatches($positionIds, $accountId);
+        if (empty($dispatches)) {
+            return [];
+        }
+
+        // 针对分配数据筛选出优先级最高的分配任务
+        $result     = [];
+        $dispatches = collect($dispatches)->groupBy('ad_position_id')->all();
+        foreach ($positions as $position) {
+            // 相关变量获取
+            $positionId        = getProp($position, 'id');
+            $ident             = getProp($position, 'ident');
+            $filterContentJson = getProp($position, 'filter_condition');
+
+            // 获取优先级最高的任务
+            $validDispatches = getProp($dispatches, $positionId, []);
+            $validDispatch   = $validDispatches ? $validDispatches[0] : [];
+            if (empty($validDispatch)) {
+                continue;
+            }
+
+            // 过滤条件判断
+            $matchFilter = $this->filterDispatch($uid, $filterContentJson);
+            if (!$matchFilter) {
+                continue;
+            }
+
+            // 添加到数据中
+            $result[$ident][] = $this->buildPositionData($validDispatch);
+        }
+
+        return $result;
+    }
+
+    /**
+     * 过滤条件筛选
+     * @param $uid
+     * @param $filterContentJson
+     * @return bool
+     */
+    private function filterDispatch($uid, $filterContentJson): bool
+    {
+        // 过滤条件
+        $filterContent = json_decode($filterContentJson, true);
+        if (empty($uid) || empty($filterContentJson) || empty($filterContent)) {
+            return true;
+        }
+
+        // 判断注册时间
+        $registerFilter = false;
+        $registerAtArr  = getProp($filterContent, 'registerAt');
+        if ($registerAtArr) {
+            // 获取用户信息
+            $user         = QappUser::getUserByUid($uid);
+            $registerAt   = getProp($user, 'created_at');
+            $registerUnix = strtotime($registerAt);
+
+            // 循环条件,满足一条即可
+            foreach ($registerAtArr as $register) {
+                $unit  = getProp($register, 'unit');
+                $range = getProp($register, 'range');
+                sort($range);
+                [$min, $max] = $range;
+                $startUnix = strtotime('-' . $max . ' ' . $unit);
+                $endUnix   = strtotime('-' . $min . ' ' . $unit);
+                if ($registerUnix >= $startUnix && $registerUnix <= $endUnix) {
+                    $registerFilter = true;
+                    break;
+                }
+            }
+        }
+
+        // 判断付费情况
+        $paidFilter = false;
+        if (isset($filterContent['isPaid'])) {
+            // 获取用户付费情况
+            $order = OrderService::getUserLastestOrder($uid);
+
+            // 有付费
+            if ($order && $filterContent['isPaid']) {
+                $paidFilter = true;
+            }
+
+            // 未付费
+            if (!$order && !$filterContent['isPaid']) {
+                $paidFilter = true;
+            }
+        }
+
+        // 两者如有一个不满足条件则不展示
+        if (!$registerFilter || !$paidFilter) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * 返回可显示数据
+     * @param $param
+     * @return array
+     */
+    private function buildPositionData($param): array
+    {
+        if (empty($param)) {
+            return [];
+        }
+
+        // 链接做特殊处理
+        $url    = getProp($param, 'link_url');
+        $urlArr = parse_url($url);
+        parse_str(getProp($urlArr, 'query'), $queryArr);
+
+        return [
+            'img'    => getProp($param, 'image_url'),
+            'url'    => getProp($urlArr, 'path'),
+            'title'  => getProp($param, 'title'),
+            'params' => $queryArr ?: (object)[]
+        ];
+    }
+}

+ 30 - 0
app/Modules/SendOrder/Models/QappSendOrder.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Modules\SendOrder\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class QappSendOrder extends Model
+{
+    protected $table = 'qapp_send_orders';
+    protected $fillable = [
+        'send_order_id',
+        'account',
+        'channel_user_id',
+        'distribution_channel_id',
+    ];
+
+    /**
+     * @param $sendOrderId
+     * @return array
+     */
+    public static function getSendOrderById($sendOrderId)
+    {
+        if (empty($sendOrderId)) {
+            return [];
+        }
+
+        $result = self::where('send_order_id', $sendOrderId)->first();
+        return $result ? $result->toArray() : [];
+    }
+}

+ 0 - 13
app/Modules/SendOrder/Models/QuickAppSendOrder.php

@@ -14,17 +14,4 @@ class QuickAppSendOrder extends Model
         'child_gzh_name',
     ];
 
-    /**
-     * @param $sendOrderId
-     * @return array
-     */
-    public static function getSendOrderById($sendOrderId)
-    {
-        if (empty($sendOrderId)) {
-            return [];
-        }
-        
-        $result = self::where('id', $sendOrderId)->first();
-        return $result ? $result->toArray() : [];
-    }
 }

+ 9 - 0
app/Modules/Trade/Models/Order.php

@@ -460,4 +460,13 @@ class Order extends Model
 
         return $result ? $result->toArray() : [];
     }
+
+    public static function getUserLastestOrder($uid)
+    {
+        if (empty($uid)) {
+            return [];
+        }
+
+        return self::where('uid', $uid)->where('status', 'PIAD')->orderBy('id')->limit(1)->get();
+    }
 }

+ 5 - 0
app/Modules/Trade/Services/OrderService.php

@@ -210,4 +210,9 @@ class OrderService
     {
         return Order::getOrdersByActivityId($channelId, $createdAt, $activityId);
     }
+
+    public static function getUserLastestOrder($uid)
+    {
+        return Order::getUserLastestOrder($uid);
+    }
 }

+ 13 - 0
app/Modules/User/Models/QappUser.php

@@ -18,4 +18,17 @@ class QappUser extends Model
         'phone',
         'channel_id',
     ];
+
+    /**
+     * @param $uid
+     * @return array
+     */
+    public static function getUserByUid($uid)
+    {
+        if (empty($uid)) {
+            return [];
+        }
+
+        return self::where('uid', $uid)->first();
+    }
 }