浏览代码

获取满足用户分群的用户列表

liuzejian 1 年之前
父节点
当前提交
30392a9e64

+ 58 - 0
modules/Audience/Http/Controllers/UserGroupController.php

@@ -9,6 +9,8 @@ use Illuminate\Support\Facades\DB;
 use Modules\Audience\Models\GzhUgMapModel;
 use Modules\Audience\Models\UserGroupModel;
 use Modules\Audience\Services\UserGroupService;
+use Modules\Common\Errors\Errors;
+use Modules\Common\Exceptions\CommonBusinessException;
 
 class UserGroupController extends CatchController
 {
@@ -123,4 +125,60 @@ class UserGroupController extends CatchController
 
         return 'ok';
     }
+
+
+    /**
+     * 获取符合条件的用户分群列表
+     * @param Request $request
+     */
+    public function listUser(Request $request) {
+        $this->validate($request, [
+            'timestamp' => 'required',
+            'sign' => 'required'
+        ]);
+        $signKey = config('audience.ug.signKey');
+        if($request->input('sign') != md5(
+                $signKey.$request->input('timestamp')
+            )) {
+            CommonBusinessException::throwError(Errors::OPENPLATFORM_UG_SIGN_ERROR);
+        }
+
+        $tags = DB::table('user_groups')
+            ->where([
+                ['id' , '=', $request->input('ugId')],
+                ['is_enabled', '=', 1]
+            ])->value('tags');
+        if(!$tags) {
+            CommonBusinessException::throwError(Errors::OPENPLATFORM_UG_NOT_EXISTS);
+        }
+        $tagsArr = \json_decode($tags, true);
+        $authorizer_appid = DB::table('wechat_authorization_infos')
+            ->where([
+                ['id', '=', $request->input('gzhId')],
+                ['is_enabled', '=', 1]
+            ])->value('authorizer_appid');
+        if(!$authorizer_appid) {
+            CommonBusinessException::throwError(Errors::OPENPLATFORM_GZH_SHOUQUAN_ERROR);
+        }
+        $tagsArr['gzh_appids'] = [$authorizer_appid];
+        $sql = UserGroupService::getSQL($tagsArr);
+        $res = $sql->distinct()->select('b.uid', 'b.mp_openid')
+            ->get();
+        $uids = $res->pluck('uid')->toArray();
+        if(count($uids)) {
+            $uids = UserGroupService::dealHistoryReadBooks(UserGroupService::dealPayVideo($uids, $tags), $tags);
+        }
+
+        $openid = [];
+        foreach ($res as $item) {
+            if(in_array($item->uid, $uids)) {
+                $openid[] = $item->mp_openid;
+            }
+        }
+
+
+        return [
+            'openid' => $openid
+        ];
+    }
 }

+ 3 - 3
modules/Audience/Services/UserGroupService.php

@@ -9,8 +9,8 @@ class UserGroupService
     public static function getSQL($tags) {
         $now = time();
         $sql = DB::table('user_some_info as a');
-        if(strlen($tags['attention_hour'] ?? '') || (1 == $tags['in_48_hour'] ?? 1) ||
-        strlen($tags['interact_hour'] ?? '') || $tags['gzh_appids'] ?? []) {
+        if(strlen($tags['attention_hour'] ?? '') || (1 == ($tags['in_48_hour'] ?? 1)) ||
+        strlen($tags['interact_hour'] ?? '') || ($tags['gzh_appids'] ?? [])) {
             $sql->join('wechat_official_user_info as b', 'a.uid', '=', 'b.uid');
             if(strlen($tags['attention_hour'] ?? '')) {
                 $filter = self::filterTime($tags['attention_hour'], 'hour');
@@ -20,7 +20,7 @@ class UserGroupService
                     ]);
                 }
             }
-            if(1 == $tags['in_48_hour'] ?? 1) {
+            if(1 == ($tags['in_48_hour'] ?? 1)) {
                 $filter = self::filterTime('0-48', 'hour');
                 foreach ($filter as $f) {
                     $sql->where([

+ 6 - 0
modules/Audience/config/ug.php

@@ -0,0 +1,6 @@
+<?php
+
+return [
+    // 外部请求的signkey
+    'signKey' => 'lV9bOCooPTJ68G3a',
+];

+ 5 - 0
modules/Audience/routes/route.php

@@ -19,6 +19,11 @@ Route::prefix('audienceManage')->group(function () {
         Route::get('detail', [UserGroupController::class, 'detail']);
         Route::post('add', [UserGroupController::class, 'add']);
         Route::post('delete', [UserGroupController::class, 'delete']);
+
+    });
+
+    Route::prefix('outUserGroup')->group(function(){
+        Route::get('listUser', [UserGroupController::class, 'listUser'])->withoutMiddleware(config('catch.route.middlewares'));
     });
 });
 

+ 3 - 0
modules/Common/Errors/Errors.php

@@ -41,4 +41,7 @@ class Errors
     public const  CALLBACK_CHARGE_MONEY_MAP_ERROR = [500107, '金额项配置不合规'];
 
     public const OPENPLATFORM_UG_MAP_GZH_NOT_EXIST = [500603, '公众号没有配置对应用户分群'];
+    public const OPENPLATFORM_UG_SIGN_ERROR = [500604, '请求用户分群签名错误'];
+    public const OPENPLATFORM_UG_NOT_EXISTS = [500605, '用户分群不存在'];
+    public const OPENPLATFORM_GZH_SHOUQUAN_ERROR = [500606, '公众号没有授权'];
 }

+ 17 - 0
tests/Audience/Http/Controllers/UserGroupControllerTest.php

@@ -53,4 +53,21 @@ class UserGroupControllerTest extends UsedTestCase
         $this->dumpJson($res);
     }
 
+    public function testlistUser()
+    {
+        $timestamp = time();
+        $signKey = config('audience.ug.signKey');
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('get','http://localhost/api/audienceManage/outUserGroup/listUser', [
+            'timestamp' => $timestamp,
+            'sign' => md5($signKey. $timestamp),
+            'gzhId' => 8,
+            'ugId' => 2,
+            'nextUid' => 100,
+            'limit' => 10
+        ]);
+        $this->dumpJson($res);
+    }
+
 }