Browse Source

feat:用户端-微信-获取JSAPI配置

刘学玺 4 months ago
parent
commit
e83a7001b4

+ 46 - 0
app/Http/Controllers/Client/CoachGroupController.php

@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
 use App\Http\Requests\Client\Wechat\GetAuthUrlRequest;
 use App\Http\Requests\Client\Wechat\HandleCallbackRequest;
 use App\Services\Client\WechatService;
+use Illuminate\Http\Request;
 
 /**
  * @group 用户端
@@ -111,4 +112,49 @@ class WechatController extends Controller
 
         return $this->success($result);
     }
+
+    /**
+     * [微信]获取JSAPI配置
+     *
+     * @description 获取微信JSAPI配置信息,用于前端调用微信JSAPI
+     *
+     * @queryParam url required 当前网页的URL,不包含#及其后面部分 Example: https://example.com/page
+     *
+     * @response 200 scenario="成功" {
+     *   "code": 200,
+     *   "data": {
+     *     "appId": "wx123456789",
+     *     "timestamp": 1577808000,
+     *     "nonceStr": "random_string",
+     *     "signature": "sha1_signature_string",
+     *     "jsApiList": ["updateAppMessageShareData", "updateTimelineShareData"]
+     *   }
+     * }
+     * @response 422 scenario="参数验证失败" {
+     *   "code": 422,
+     *   "message": "URL不能为空|URL必须是一个有效的网址"
+     * }
+     * @response 400 scenario="获取配置失败" {
+     *   "code": 400,
+     *   "message": "获取JSAPI配置失败,请稍后重试"
+     * }
+     *
+     * @throws BusinessException
+     */
+    public function getJsConfig(Request $request)
+    {
+        $url = $request->input('url');
+
+        if (empty($url)) {
+            return $this->error('URL不能为空');
+        }
+
+        if (!filter_var($url, FILTER_VALIDATE_URL)) {
+            return $this->error('URL必须是一个有效的网址');
+        }
+
+        $config = $this->wechatService->getJsConfig($url);
+
+        return $this->success($config);
+    }
 }

+ 33 - 0
app/Http/Controllers/Coach/AuthController.php

@@ -184,4 +184,37 @@ class WechatService
     {
         return config('wechat.auth.cache_prefix').$state;
     }
+
+    /**
+     * 获取JSAPI配置
+     *
+     * @param  string  $url  当前网页的URL,不包含#及其后面部分
+     * @return array JSAPI配置信息
+     *
+     * @throws BusinessException
+     */
+    public function getJsConfig(string $url): array
+    {
+        try {
+            $apis = [
+                'updateAppMessageShareData',
+                'updateTimelineShareData',
+                'chooseImage',
+                'previewImage',
+                'uploadImage',
+                'downloadImage',
+                'getLocation',
+                'openLocation',
+                'scanQRCode'
+            ];
+
+            return $this->app->getUtils()->buildJsSdkConfig($url, $apis);
+        } catch (\Throwable $e) {
+            \Log::error('获取JSAPI配置失败', [
+                'url' => $url,
+                'error' => $e->getMessage()
+            ]);
+            throw new BusinessException('获取JSAPI配置失败,请稍后重试');
+        }
+    }
 }

+ 1 - 0
app/Services/Coach/AuthService.php

@@ -145,6 +145,7 @@ Route::prefix('client')->group(function () {
         Route::prefix('wechat')->group(function () {
             Route::get('auth-url', [WechatController::class, 'getAuthUrl']);
             Route::post('callback', [WechatController::class, 'handleCallback']);
+            Route::get('js-config', [WechatController::class, 'getJsConfig']);
         });
 
         // 评价管理