Browse Source

更新到easyWechat5.0
1,更新授权和取消授权

liuzejian 1 year ago
parent
commit
75471b5d37

+ 14 - 21
modules/Channel/Http/Controllers/WechatOpenPlatformController.php

@@ -42,7 +42,7 @@ class WechatOpenPlatformController extends CatchController
         $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($currentUser->id);
         $app = WechatOpenPlatformService::buildApplication($componentInfo);
         $user_id = $request->input('user_id');
-        $url = $app->createPreAuthorizationUrl(sprintf('%s/api/channel/openPlatform/auth/%s/%s',config('app.url'), $componentInfo->app_id, $user_id), []);
+        $url = $app->getPreAuthorizationUrl(sprintf('%s/api/channel/openPlatform/auth/%s/%s',config('app.url'), $componentInfo->app_id, $user_id), []);
         return $url;
     }
 
@@ -71,29 +71,26 @@ class WechatOpenPlatformController extends CatchController
         }
         $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
         $app = WechatOpenPlatformService::buildApplication($componentInfo);
-        $client = $app->getClient();
-        $response = $client->postJson('/cgi-bin/component/api_query_auth', [
-            'component_appid' => $component_appid,
-            'authorization_code' => $auth_code
-        ]);
-        $parsedContent = \json_decode($response->getContent(), true);
-        $authorizer_appid = $parsedContent['authorization_info']['authorizer_appid'];
-        $authorizer_refresh_token = $parsedContent['authorization_info']['authorizer_refresh_token'];
+        $authorize = $app->handleAuthorize($auth_code);
+        $authorizer_appid = $authorize['authorization_info']['authorizer_appid'];
+        $authorizer_refresh_token = $authorize['authorization_info']['authorizer_refresh_token'];
+        $gzhBaseInfo = $app->getAuthorizer($authorizer_appid);
 
-        $response = $client->postJson('/cgi-bin/component/api_get_authorizer_info', [
+        WechatAuthorizationInfo::where([
+            'authorizer_appid' => $authorizer_appid,
             'component_appid' => $component_appid,
-            'authorizer_appid' => $authorizer_appid
+            'puser_id' => $auth_user->pid,
+            'is_enabled' => 1,
+        ])->update([
+            'is_enabled' => 0,
         ]);
-        $parsedContent = \json_decode($response->getContent(), true);
-
-        WechatAuthorizationInfo::updateOrCreate([
+        WechatAuthorizationInfo::create([
             'authorizer_appid' => $authorizer_appid,
             'component_appid' => $component_appid,
             'user_id' => $user_id,
             'puser_id' => $auth_user->pid,
-        ], [
             'authorizer_refresh_token' => $authorizer_refresh_token,
-            'nick_name' => $parsedContent['authorizer_info']['nick_name'],
+            'nick_name' => $gzhBaseInfo['authorizer_info']['nick_name'],
         ]);
 
         return view('wechat.openPlatform.authSuccess')->with('url', sprintf('%s/#/user/advertiser',config('app.url')));
@@ -108,12 +105,8 @@ class WechatOpenPlatformController extends CatchController
         $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
         $app = WechatOpenPlatformService::buildApplication($componentInfo);
 
-        $server = $app->getServer();
+        $server = $app->server;
 
-        $server->handleAuthorized(function($message, \Closure $next) {
-            myLog('authorCommand')->info('handleAuthorized', ['message' => $message]);
-            return $next($message);
-        });
         $server->handleUnauthorized(function($message, \Closure $next) {
             WechatOpenPlatformService::handleUnauthorized($message);
             return $next($message);

+ 9 - 5
modules/Channel/Services/WechatOpenPlatform/WechatOpenPlatformService.php

@@ -2,10 +2,12 @@
 
 namespace Modules\Channel\Services\WechatOpenPlatform;
 
+use EasyWeChat\Factory;
 use EasyWeChat\OpenPlatform\Application;
 use EasyWeChat\OpenPlatform\Message;
 use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\DB;
+use Modules\Channel\Models\WechatAuthorizationInfo;
 use Modules\Common\Errors\Errors;
 use Modules\Common\Exceptions\CommonBusinessException;
 
@@ -49,14 +51,15 @@ class WechatOpenPlatformService
             'secret' => $componentInfo->secret,   // 开放平台账号的 secret
             'token' => $componentInfo->token,  // 开放平台账号的 token
             'aes_key' => $componentInfo->aes_key,   // 明文模式请勿填写 EncodingAESKey
+            'response_type' => 'array',
             'http' => [
                 'throw'  => true, // 状态码非 200、300 时是否抛出异常,默认为开启
                 'timeout' => 5.0,
                 'retry' => true, // 使用默认重试配置
             ],
         ];
-        $app = new Application($config);
-        $app->setCache(Cache::store('redis'));
+        $app = Factory::openPlatform($config);
+        $app->rebind('cache', Cache::store('redis'));
         return $app;
     }
 
@@ -67,11 +70,12 @@ class WechatOpenPlatformService
     public static function handleUnauthorized($message) {
         $component_appid = $message['AppId'];
         $authorizer_appid = $message['AuthorizerAppid'];
-        DB::table('wechat_authorization_infos')
-            ->where([
+        WechatAuthorizationInfo::where([
                 'component_appid' => $component_appid,
                 'authorizer_appid' => $authorizer_appid,
-            ])->delete();
+            ])->update([
+                'is_enabled' => 0
+            ]);
     }
 
     /**