Sfoglia il codice sorgente

用户分群返回openid

liuzejian 1 anno fa
parent
commit
7058d99102
2 ha cambiato i file con 37 aggiunte e 3 eliminazioni
  1. 31 3
      app/Jobs/WechatPlatform/GZHSendKFMessage.php
  2. 6 0
      config/wechat.php

+ 31 - 3
app/Jobs/WechatPlatform/GZHSendKFMessage.php

@@ -2,6 +2,7 @@
 
 namespace App\Jobs\WechatPlatform;
 
+use App\Service\Util\Support\Http\HttpRequestService;
 use App\Service\Util\Support\Trace\TraceContext;
 use App\Service\WechatPlatform\GZHSendKFMessageService;
 use App\Service\WechatPlatform\WechatPlatform;
@@ -89,12 +90,20 @@ class GZHSendKFMessage implements ShouldQueue
 //                        GZHSendKFMessageService::sendText($officialAccount, $opid, $messageStr, $this->traceContext);
                         dump($opid);
                     }
-                    $next_openid = $info['next_openid'];
+                    $next_openid = $info['next_openid'] ?? '';
                     if(!$next_openid) {
                         break;
                     }
                 } elseif (2 == $message->u_type) {
-                    // todo, 从人群包中获取.
+                    $info = $this->getUsersFromUG($gzh->id, $message->ug_id,  $next_openid);
+                    foreach (($info['data']['openid'] ?? []) as $opid) {
+                        //                        GZHSendKFMessageService::sendText($officialAccount, $opid, $messageStr, $this->traceContext);
+                        dump($opid);
+                    }
+                    $next_openid = $info['data']['next_uid'] ?? '';
+                    if(!$next_openid) {
+                        break;
+                    }
                 }
             }
         }
@@ -134,6 +143,24 @@ class GZHSendKFMessage implements ShouldQueue
         return $result;
     }
 
+    private function getUsersFromUG($gzhId, $ugId,  $nextUid) {
+        $url = config('wechat.ug.url.listUser');
+        $signKey = config('wechat.ug.signKey');
+        $now = time();
+        $res = HttpRequestService::simpleGet($url, [
+            'timestamp' => $now,
+            'sign' => md5($signKey.$now),
+            'gzhId' => $gzhId,
+            'ugId' => $ugId,
+            'nextUid' => $nextUid,
+            'limit' => 1000
+        ]);
+        if($res && (0 != ($res['code'] ?? 0))) {
+            return $res;
+        }
+        return false;
+    }
+
     /**
      * 获取公众号调用对象
      * @param $gzh
@@ -168,7 +195,8 @@ class GZHSendKFMessage implements ShouldQueue
                 ['a.id', '=', $this->info['gzhId']],
                 ['a.is_enabled', '=', 1],
                 ['b.is_enabled', '=', 1]
-            ])->select('a.authorizer_appid', 'a.authorizer_refresh_token', 'b.app_id', 'b.secret', 'b.token', 'b.aes_key')
+            ])->select('a.id', 'a.authorizer_appid', 'a.authorizer_refresh_token',
+                'b.app_id', 'b.secret', 'b.token', 'b.aes_key')
             ->first();
         if(!$gzh) {
             myLog('KFMessageSend')->error('公众号不可用', [

+ 6 - 0
config/wechat.php

@@ -4,5 +4,11 @@ return [
     'duanju' => [
         // 短剧主小程序
         'masterAppid' => env('WECHAT_DUANJU_MASTER_APPID', 'wx86822355ccd03a78'),
+    ],
+    'ug' => [
+        'url' => [
+            'listUser' => env('WECHAT_UG_URL_LIST_USER',  'http://m.test.duanju.dududus.com/api/audienceManage/userGroup/listUser'),
+        ],
+        'signKey' => 'lV9bOCooPTJ68G3a'
     ]
 ];