Selaa lähdekoodia

微信三方授权是投放平台公司的功能

liuzejian 1 vuosi sitten
vanhempi
commit
8b9a5efe15

+ 11 - 5
modules/Channel/Http/Controllers/WechatOpenPlatformController.php

@@ -4,6 +4,7 @@ namespace Modules\Channel\Http\Controllers;
 
 use Catch\Base\CatchController;
 use EasyWeChat\OpenPlatform\Application;
+use Illuminate\Foundation\Validation\ValidatesRequests;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Redis;
@@ -17,16 +18,21 @@ use Symfony\Component\Cache\Adapter\RedisAdapter;
 class WechatOpenPlatformController extends CatchController
 {
     use UserTrait;
+    use ValidatesRequests;
     // 授权跳转页
     public function preauth(Request $request) {
+        $this->validate($request, [
+            'user_id' => 'required'
+        ]);
         $currentUser = $this->getCurrentUser();
-        $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($currentUser->pid);
+        $componentInfo = WechatOpenPlatformService::getComponentInfoByCompanyUid($currentUser->id);
         $app = WechatOpenPlatformService::buildApplication($componentInfo);
-        $url = $app->createPreAuthorizationUrl(url('/api/channel/openPlatform/auth/'.$componentInfo->app_id), []);
+        $user_id = $request->input('user_id');
+        $url = $app->createPreAuthorizationUrl(url('/api/channel/openPlatform/auth/'.$componentInfo->app_id.'/'.$user_id), []);
         return $url;
     }
 
-    public function auth(Request $request, $component_appid) {
+    public function auth(Request $request, $component_appid, $user_id) {
         $auth_code = $request->input('auth_code');
         $componentInfo = WechatOpenPlatformService::getComponentInfoByAppid($component_appid);
         $app = WechatOpenPlatformService::buildApplication($componentInfo);
@@ -41,8 +47,8 @@ class WechatOpenPlatformController extends CatchController
         WechatAuthorizationInfo::updateOrCreate([
             'authorizer_appid' => $authorizer_appid,
             'component_appid' => $component_appid,
-            'user_id' => $currentUser->id,
-            'puser_id' => $currentUser->pid,
+            'user_id' => $user_id,
+            'puser_id' => $currentUser->id,
         ], [
             'authorizer_refresh_token' => $authorizer_refresh_token
         ]);

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

@@ -54,8 +54,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::get('auth/{component_appid}/{user_id}', [WechatOpenPlatformController::class, 'auth'])->middleware(['roleCheck:company']);
+        Route::get('preauth', [WechatOpenPlatformController::class, 'preauth'])->middleware(['roleCheck:company']);
         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'));;
     });