소스 검색

三方平台授权

liuzejian 1 년 전
부모
커밋
ab99811b87

+ 37 - 1
modules/Channel/Http/Controllers/WechatOpenPlatformController.php

@@ -5,6 +5,8 @@ namespace Modules\Channel\Http\Controllers;
 use Catch\Base\CatchController;
 use EasyWeChat\OpenPlatform\Application;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
+use Modules\Channel\Models\WechatAuthorizationInfo;
 use Modules\Channel\Services\WechatOpenPlatform\WechatOpenPlatformService;
 use Modules\Common\Errors\Errors;
 use Modules\Common\Exceptions\CommonBusinessException;
@@ -16,7 +18,7 @@ class WechatOpenPlatformController extends CatchController
     // 授权跳转页
     public function preauth(Request $request) {
         $currentUser = $this->getCurrentUser();
-        $componentInfo = WechatOpenPlatformService::getComponentInfo($currentUser->pid);
+        $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($currentUser->pid);
         if(!$componentInfo) {
             CommonBusinessException::throwError(Errors::OPENPLATFORM_COMPANY_INFO_NOT_EXISTS);
         }
@@ -37,6 +39,40 @@ class WechatOpenPlatformController extends CatchController
     }
 
     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);
+        $client = $app->getClient();
+        $response = $client->post('/cgi-bin/component/api_query_auth', [
+            'component_appid' => $component_appid,
+            'authorization_code' => $auth_code
+        ]);
+        $authorizer_appid = $response['authorization_info']['authorizer_appid'];
+        $authorizer_refresh_token = $response['authorization_info']['authorizer_refresh_token'];
+        $currentUser = $this->getCurrentUser();
+        WechatAuthorizationInfo::updateOrCreate([
+            'authorizer_appid' => $authorizer_appid,
+            'component_appid' => $component_appid,
+            'user_id' => $currentUser->id,
+            'puser_id' => $currentUser->pid,
+        ], [
+            'authorizer_refresh_token' => $authorizer_refresh_token
+        ]);
 
+        return view('wechat.openPlatform.authSuccess')->with('url', url('/'));
     }
 }

+ 12 - 0
modules/Channel/Models/WechatAuthorizationInfo.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace Modules\Channel\Models;
+
+
+
+class WechatAuthorizationInfo extends BaseModel
+{
+    protected $table = 'wechat_authorization_infos';
+
+    protected $guarded = [];
+}

+ 10 - 1
modules/Channel/Services/WechatOpenPlatform/WechatOpenPlatformService.php

@@ -6,7 +6,7 @@ use Illuminate\Support\Facades\DB;
 
 class WechatOpenPlatformService
 {
-    public static function getComponentInfo($companyUid) {
+    public static function getComponentInfoByCompanyUid($companyUid) {
         return DB::table('wechat_open_platform_infos')
             ->where([
                 'company_uid' => $companyUid,
@@ -14,4 +14,13 @@ class WechatOpenPlatformService
             ])->orderBy('id', 'desc')
             ->first();
     }
+
+    public static function getComponentInfoByAppid($componentAppid) {
+        return DB::table('wechat_open_platform_infos')
+            ->where([
+                'app_id' => $componentAppid,
+                'is_enabled' => 1,
+            ])->orderBy('id', 'desc')
+            ->first();
+    }
 }

+ 1 - 1
modules/Channel/routes/route.php

@@ -54,7 +54,7 @@ Route::prefix('channel')->group(function () {
      * 第三方开放平台
      */
     Route::prefix('openPlatform')->group(function(){
-        Route::get('auth/{component_appid}', [WechatOpenPlatformController::class, 'auth'])->withoutMiddleware(config('catch.route.middlewares'));
+        Route::get('auth/{component_appid}', [WechatOpenPlatformController::class, 'auth'])->middleware(['roleCheck:optimizer']);
         Route::get('preauth', [WechatOpenPlatformController::class, 'preauth']);
     });
 });

+ 8 - 0
resources/views/wechat/openPlatform/authSuccess.blade.php

@@ -0,0 +1,8 @@
+<h1>
+    授权成功, 正在跳转 : <a href="{{ $url }}">{{ $url }}</a>
+</h1>
+<script>
+    setTimeout(function () {
+        window.open("{{ $url }}")
+    }, 3000)
+</script>

+ 20 - 0
tests/Channel/Http/Controllers/WechatOpenPlatformControllerTest.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace Tests\Channel\Http\Controllers;
+
+use Illuminate\Support\Facades\DB;
+use Modules\Channel\Http\Controllers\WechatOpenPlatformController;
+use PHPUnit\Framework\TestCase;
+use Tests\UsedTestCase;
+
+class WechatOpenPlatformControllerTest extends UsedTestCase
+{
+
+    public function testAuth()
+    {
+        DB::table('banks')
+            ->upsert([
+                ['name' => '比利时联合银行股份有限公司', 'is_show' => 1, 'code' => 651],
+            ], ['name'], ['is_show']);
+    }
+}