Ver Fonte

Merge branch 'liuzj-1001016-dev' into test

liuzejian há 1 ano atrás
pai
commit
2a416804ef
1 ficheiros alterados com 32 adições e 29 exclusões
  1. 32 29
      app/Jobs/WechatPlatform/GZHSendKFMessage.php

+ 32 - 29
app/Jobs/WechatPlatform/GZHSendKFMessage.php

@@ -8,7 +8,6 @@ use App\Service\WechatPlatform\GZHSendKFMessageService;
 use App\Service\WechatPlatform\WechatPlatform;
 use EasyWeChat\OfficialAccount\Application;
 use Illuminate\Bus\Queueable;
-use Illuminate\Contracts\Queue\ShouldBeUnique;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
 use Illuminate\Queue\InteractsWithQueue;
@@ -37,6 +36,7 @@ class GZHSendKFMessage implements ShouldQueue
      * @var TraceContext
      */
     private $traceContext;
+
     /**
      * Create a new job instance.
      */
@@ -57,21 +57,21 @@ class GZHSendKFMessage implements ShouldQueue
         ]);
 
         $gzh = $this->getGZH();
-        if(!$gzh) return;
+        if (!$gzh) return;
 
         $message = $this->getMessage();
-        if(!$message) return;
+        if (!$message) return;
 
 
         $messageContent = collect(\json_decode($message->message_content, true));
         $messageStr = $messageContent->pluck('text')->join("\n\n");
 
         $officialAccount = $this->getOfficialAccount($gzh);
-        if(false === $officialAccount) return;
+        if (false === $officialAccount) return;
 
-        if($this->info['isTest'] ?? false) {
+        if ($this->info['isTest'] ?? false) {
             $openid = $this->info['openid'] ?? '';
-            if(!$openid) {
+            if (!$openid) {
                 myLog('KFMessageSend')->error('测试回传没有openid', [
                     'info' => $this->info
                 ]);
@@ -81,27 +81,25 @@ class GZHSendKFMessage implements ShouldQueue
             $next_openid = '';
             $loop = 1;
             while (true) {
-                if($loop++ > 10000) {
+                if ($loop++ > 10000) {
                     break;
                 }
-                if(1 == $message->u_type) {
+                if (1 == $message->u_type) {
                     $info = $this->getUserOpenids($officialAccount, $next_openid);
-                    foreach (($info['data']['openid'] ?? []) as $opid){
-//                        GZHSendKFMessageService::sendText($officialAccount, $opid, $messageStr, $this->traceContext);
-                        dump($opid);
+                    foreach (($info['data']['openid'] ?? []) as $opid) {
+                        GZHSendKFMessageService::sendText($officialAccount, $opid, $messageStr, $this->traceContext);
                     }
                     $next_openid = $info['next_openid'] ?? '';
-                    if(!$next_openid) {
+                    if (!$next_openid) {
                         break;
                     }
                 } elseif (2 == $message->u_type) {
-                    $info = $this->getUsersFromUG($gzh->id, $message->ug_id,  $next_openid);
+                    $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);
+                        GZHSendKFMessageService::sendText($officialAccount, $opid, $messageStr, $this->traceContext);
                     }
                     $next_openid = $info['data']['next_uid'] ?? '';
-                    if(!$next_openid) {
+                    if (!$next_openid) {
                         break;
                     }
                 }
@@ -109,18 +107,19 @@ class GZHSendKFMessage implements ShouldQueue
         }
     }
 
-    private function getMessage() {
+    private function getMessage()
+    {
         $message = DB::table('wechat_kf_messages')
             ->where('id', $this->info['messageId'])
             ->first();
-        if(!$message) {
+        if (!$message) {
             myLog('KFMessageSend')->error('消息不存在', [
                 'info' => $this->info,
                 'traceInfo' => $this->traceContext->getTraceInfo(),
             ]);
             return false;
         }
-        if(1 != $message->message_type) {
+        if (1 != $message->message_type) {
             myLog('KFMessageSend')->error('不支持的消息类型', [
                 'info' => $this->info,
                 'traceInfo' => $this->traceContext->getTraceInfo(),
@@ -134,10 +133,11 @@ class GZHSendKFMessage implements ShouldQueue
      * 拉取公众号粉丝
      * @param $officialAccount Application
      */
-    private function getUserOpenids($officialAccount, $next_openid) {
+    private function getUserOpenids($officialAccount, $next_openid)
+    {
         $result = $officialAccount->user->list($next_openid);
 
-        if(0 != ($result['errcode'] ?? 0)) {
+        if (0 != ($result['errcode'] ?? 0)) {
             return false;
         }
         return $result;
@@ -150,25 +150,26 @@ class GZHSendKFMessage implements ShouldQueue
      * @param $nextUid
      * @return false|mixed
      */
-    private function getUsersFromUG($gzhId, $ugId,  $nextUid) {
+    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),
+            'sign' => md5($signKey . $now),
             'gzhId' => $gzhId,
             'ugId' => $ugId,
             'nextUid' => $nextUid,
             'limit' => 1000
         ]);
-        if($res) {
+        if ($res) {
             myLog('KFMessageSend')->debug('通过用户分群获取用户列表', [
                 'gzhId' => $gzhId,
                 'ugId' => $ugId,
                 'res' => $res
             ]);
-            if(10000 == ($res['code'] ?? 10000)) {
+            if (10000 == ($res['code'] ?? 10000)) {
                 return $res;
             }
         }
@@ -189,8 +190,9 @@ class GZHSendKFMessage implements ShouldQueue
      * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
      * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
      */
-    private function getOfficialAccount($gzh) {
-        try{
+    private function getOfficialAccount($gzh)
+    {
+        try {
             return WechatPlatform::buildApplication($gzh)
                 ->officialAccount($gzh->authorizer_appid, $gzh->authorizer_refresh_token);
         } catch (\Throwable $exception) {
@@ -202,7 +204,8 @@ class GZHSendKFMessage implements ShouldQueue
         }
     }
 
-    private function getGZH() {
+    private function getGZH()
+    {
         $gzh = DB::table('wechat_authorization_infos as a')
             ->join('wechat_open_platform_infos as b', 'a.component_appid', 'b.app_id')
             ->where([
@@ -212,7 +215,7 @@ class GZHSendKFMessage implements ShouldQueue
             ])->select('a.id', 'a.authorizer_appid', 'a.authorizer_refresh_token',
                 'b.app_id', 'b.secret', 'b.token', 'b.aes_key')
             ->first();
-        if(!$gzh) {
+        if (!$gzh) {
             myLog('KFMessageSend')->error('公众号不可用', [
                 'traceInfo' => $this->traceContext->getTraceInfo(),
             ]);