فهرست منبع

Merge branch 'liuzj-1001016-dev' into test

liuzejian 1 سال پیش
والد
کامیت
e5755c276e

+ 8 - 17
app/Console/Commands/WechatPlatform/Test.php

@@ -6,7 +6,9 @@ use App\Jobs\WechatPlatform\GZHSendKFMessage;
 use App\Service\Util\Support\Trace\TraceContext;
 use App\Service\WechatPlatform\WechatPlatformConstService;
 use EasyWeChat\Factory;
+use EasyWeChat\Kernel\Messages\Text;
 use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\DB;
 
 
@@ -31,24 +33,13 @@ class Test extends Command
      */
     public function handle()
     {
-        $config = [
-            'app_id' => 'wxf313b3506bac2647',
-            'secret' => 'aaf348571cbc5d554e4de1bcd3cb0342',
-            'token' => 'jXP3Cdl6GofhWVsQTtgwLv2tMZG9nHlPgErZm6ixaSl',
-            'aes_key' => 'GtG7s1v7QV8kRfpWeGmDnwhwdPPKSrA35rIhmrwenpq',
-            'http' => [
-                'max_retries' => 1,
-                'retry_delay' => 500,
-                'timeout' => 5.0,
-            ],
+        $info = [
+            'gzhId' => 8,
+            'messageId' => 6,
+            'traceInfo' => (new TraceContext())->getTraceInfo()
         ];
-        $openPlatform = Factory::openPlatform($config);
-        $officialAccount = $openPlatform->officialAccount('wxebcb86ec4b80eaca',
-            'refreshtoken@@@pOdN-RBZ1f475kZxXM66IcG2keHkv8V0mDs97JYa2TQ');
-
-        $officialAccount->customer_service->message('hello, '. date('Y-m-d H:i:s'))
-            ->to('oHhd20dzCcfVjWt4OcGzFWISp6A4')
-            ->send();
+        $skf = new GZHSendKFMessage($info);
 
+        $skf->handle();
     }
 }

+ 5 - 2
app/Jobs/WechatPlatform/GZHSendKFMessage.php

@@ -86,7 +86,8 @@ class GZHSendKFMessage implements ShouldQueue
                 if(1 == $message->u_type) {
                     $info = $this->getUserOpenids($officialAccount, $next_openid);
                     foreach ($info['openid'] as $opid){
-                        GZHSendKFMessageService::sendText($officialAccount, $opid, $messageStr, $this->traceContext);
+//                        GZHSendKFMessageService::sendText($officialAccount, $opid, $messageStr, $this->traceContext);
+                        dump($opid);
                     }
                     $next_openid = $info['next_openid'];
                     if(!$next_openid) {
@@ -131,6 +132,8 @@ class GZHSendKFMessage implements ShouldQueue
                     'next_openid' => $next_openid,
                 ]
             ])->toArray();
+        $result = $officialAccount->user->list($next_openid);
+
         if(0 != ($result['errcode'] ?? 0)) {
             return false;
         }
@@ -154,7 +157,7 @@ class GZHSendKFMessage implements ShouldQueue
     private function getOfficialAccount($gzh) {
         try{
             return WechatPlatform::buildApplication($gzh)
-                ->getOfficialAccountWithRefreshToken($gzh->authorizer_appid, $gzh->authorizer_refresh_token);
+                ->officialAccount($gzh->authorizer_appid, $gzh->authorizer_refresh_token);
         } catch (\Throwable $exception) {
             myLog('KFMessageSend')->error('获取公众号调用对象失败', [
                 'exceptionMessage' => $exception->getMessage(),

+ 2 - 8
app/Service/WechatPlatform/GZHSendKFMessageService.php

@@ -19,14 +19,8 @@ class GZHSendKFMessageService
      */
     public static function sendText($officialAccount, $openid, $content, $traceContext) {
         try {
-            $officialAccount->getClient()
-                ->postJson('cgi-bin/message/custom/send',  [
-                    'touser' => $openid,
-                    'msgtype' => 'text',
-                    'text' => [
-                        'content' => $content
-                    ]
-                ])->toArray();
+            $officialAccount->customer_service->message($content)
+                ->to($openid)->send();
             return true;
         } catch (\Throwable $exception) {
             myLog('KFMessageSend')->error('发送客服消息失败', [

+ 5 - 9
app/Service/WechatPlatform/WechatPlatform.php

@@ -2,6 +2,7 @@
 
 namespace App\Service\WechatPlatform;
 
+use EasyWeChat\Factory;
 use EasyWeChat\OpenPlatform\Application;
 use Illuminate\Support\Facades\Cache;
 
@@ -13,14 +14,10 @@ class WechatPlatform
             'secret' => $componentInfo->secret,   // 开放平台账号的 secret
             'token' => $componentInfo->token,  // 开放平台账号的 token
             'aes_key' => $componentInfo->aes_key,   // 明文模式请勿填写 EncodingAESKey
-            'http' => [
-                'throw'  => true, // 状态码非 200、300 时是否抛出异常,默认为开启
-                'timeout' => 5.0,
-                'retry' => true, // 使用默认重试配置
-            ],
         ];
-        $app = new Application($config);
-        $app->setCache(Cache::store('redis'));
+
+        $app = Factory::openPlatform(array_merge($config, config('easywechat')));
+        $app->rebind('cache',Cache::store('redis'));
         return $app;
     }
 
@@ -31,7 +28,6 @@ class WechatPlatform
      * @return \EasyWeChat\OfficialAccount\Application
      */
     public static function officialAccount($app, $appid, $refreshToken) {
-
-        return $app->getOfficialAccountWithRefreshToken($appid, $refreshToken);
+        return $app->officialAccount($appid, $refreshToken);
     }
 }

+ 23 - 0
config/easywechat.php

@@ -0,0 +1,23 @@
+<?php
+
+/**
+ * easywechat 5.0 公共配置
+ */
+return [
+    'response_type' => 'array',
+    'http' => [
+        'throw'  => true, // 状态码非 200、300 时是否抛出异常,默认为开启
+        'timeout' => 5.0,
+        'retry' => true, // 使用默认重试配置
+    ],
+    'log' => [
+        'default' => 'prod',
+        'channels' => [
+            'prod' => [
+                'driver' => 'daily',
+                'path' => storage_path('logs/easyWechat.log'),
+                'level' => env('EASY_WECHAT_LOG_LEVEL', 'debug'),
+            ]
+        ]
+    ]
+];

+ 23 - 0
tests/Jobs/WechatPlatform/GZHSendKFMessageTest.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace Tests\Jobs\WechatPlatform;
+
+use App\Jobs\WechatPlatform\GZHSendKFMessage;
+use App\Service\Util\Support\Trace\TraceContext;
+use PHPUnit\Framework\TestCase;
+
+class GZHSendKFMessageTest extends \Tests\TestCase
+{
+
+    public function testHandle()
+    {
+        $info = [
+            'gzhId' => 8,
+            'messageId' => 6,
+            'traceInfo' => (new TraceContext())->getTraceInfo()
+        ];
+        $skf = new GZHSendKFMessage($info);
+
+        $skf->handle();
+    }
+}