浏览代码

第三方授权事件处理

liuzejian 1 年之前
父节点
当前提交
b417d80c09

+ 31 - 31
modules/Channel/Http/Controllers/WechatOpenPlatformController.php

@@ -19,43 +19,15 @@ class WechatOpenPlatformController extends CatchController
     public function preauth(Request $request) {
         $currentUser = $this->getCurrentUser();
         $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($currentUser->pid);
-        if(!$componentInfo) {
-            CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
-        }
-        $config = [
-            'app_id' => $componentInfo->app_id, // 开放平台账号的 appid
-            '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 = WechatOpenPlatformService::buildApplication($componentInfo);
         $url = $app->createPreAuthorizationUrl(url('/api/channel/openPlatform/auth/'.$componentInfo->app_id), []);
         return $url;
     }
 
     public function auth(Request $request, $component_appid) {
         $auth_code = $request->input('auth_code');
-        $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($component_appid);
-        if(!$componentInfo) {
-            CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
-        }
-        $config = [
-            'app_id' => $componentInfo->app_id, // 开放平台账号的 appid
-            '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);
+        $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
+        $app = WechatOpenPlatformService::buildApplication($componentInfo);
         $client = $app->getClient();
         $response = $client->post('/cgi-bin/component/api_query_auth', [
             'component_appid' => $component_appid,
@@ -75,4 +47,32 @@ class WechatOpenPlatformController extends CatchController
 
         return view('wechat.openPlatform.authSuccess')->with('url', url('/'));
     }
+
+    /**
+     * 处理授权事件
+     * @param Request $request
+     * @param $component_appid
+     */
+    public function authorCommand(Request $request, $component_appid) {
+        $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
+        $app = WechatOpenPlatformService::buildApplication($componentInfo);
+        $server = $app->getServer();
+
+        $server->handleAuthorized(function($message, \Closure $next) {
+            myLog('authorCommand')->info('handleAuthorized', ['message' => $message]);
+            return $next($message);
+        });
+
+
+        return $server->serve();
+    }
+
+    public function infoCommand(Request $request, $authorizer_appid, $component_appid) {
+        $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
+        $app = WechatOpenPlatformService::buildApplication($componentInfo);
+
+        $server = $app->getServer();
+
+        return $server->serve();
+    }
 }

+ 37 - 2
modules/Channel/Services/WechatOpenPlatform/WechatOpenPlatformService.php

@@ -2,25 +2,60 @@
 
 namespace Modules\Channel\Services\WechatOpenPlatform;
 
+use EasyWeChat\OpenPlatform\Application;
 use Illuminate\Support\Facades\DB;
+use Modules\Common\Errors\Errors;
+use Modules\Common\Exceptions\CommonBusinessException;
 
 class WechatOpenPlatformService
 {
     public static function getComponentInfoByCompanyUid($companyUid) {
-        return DB::table('wechat_open_platform_infos')
+        $componentInfo = DB::table('wechat_open_platform_infos')
             ->where([
                 'company_uid' => $companyUid,
                 'is_enabled' => 1,
             ])->orderBy('id', 'desc')
             ->first();
+        if(!$componentInfo) {
+            CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
+        }
+        return $componentInfo;
     }
 
     public static function getComponentInfoByAppid($componentAppid) {
-        return DB::table('wechat_open_platform_infos')
+        $componentInfo =  DB::table('wechat_open_platform_infos')
             ->where([
                 'app_id' => $componentAppid,
                 'is_enabled' => 1,
             ])->orderBy('id', 'desc')
             ->first();
+        if(!$componentInfo) {
+            CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
+        }
+        return $componentInfo;
+    }
+
+    /**
+     * 构造app
+     * @param $componentInfo
+     * @return Application
+     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
+     */
+    public static function buildApplication($componentInfo) {
+        if(!$componentInfo) {
+            CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
+        }
+        $config = [
+            'app_id' => $componentInfo->app_id, // 开放平台账号的 appid
+            'secret' => $componentInfo->secret,   // 开放平台账号的 secret
+            'token' => $componentInfo->token,  // 开放平台账号的 token
+            'aes_key' => $componentInfo->aes_key,   // 明文模式请勿填写 EncodingAESKey
+            'http' => [
+                'throw'  => true, // 状态码非 200、300 时是否抛出异常,默认为开启
+                'timeout' => 5.0,
+                'retry' => true, // 使用默认重试配置
+            ],
+        ];
+        return new Application($config);
     }
 }

+ 2 - 0
modules/Channel/routes/route.php

@@ -56,6 +56,8 @@ Route::prefix('channel')->group(function () {
     Route::prefix('openPlatform')->group(function(){
         Route::get('auth/{component_appid}', [WechatOpenPlatformController::class, 'auth'])->middleware(['roleCheck:optimizer']);
         Route::get('preauth', [WechatOpenPlatformController::class, 'preauth']);
+        Route::post('authorCommand/{component_appid}', [WechatOpenPlatformController::class, 'authorCommand'])->withoutMiddleware(config('catch.route.middlewares'));;
+        Route::post('infoCommand/{authorizer_appid}/{component_appid}', [WechatOpenPlatformController::class, 'infoCommand'])->withoutMiddleware(config('catch.route.middlewares'));;
     });
 });