123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace App\Http\Controllers\Client;
- 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 用户端
- *
- * 微信相关的API接口
- */
- class WechatController extends Controller
- {
- protected WechatService $wechatService;
- public function __construct(WechatService $wechatService)
- {
- $this->wechatService = $wechatService;
- }
- /**
- * [微信]获取微信授权URL
- *
- * @description 获取微信网页授权URL
- *
- * @queryParam redirect_url required 授权后的回调地址,必须是有效的URL Example: https://example.com/callback
- * @queryParam scope 授权范围,可选值:snsapi_base,snsapi_userinfo Example: snsapi_userinfo
- *
- * @response 200 scenario="成功" {
- * "code": 200,
- * "data": {
- * "auth_url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx123456&redirect_uri=https%3A%2F%2Fexample.com%2Fcallback&response_type=code&scope=snsapi_userinfo&state=7d8f69a...",
- * "state": "7d8f69a..."
- * }
- * }
- * @response 422 scenario="参数验证失败" {
- * "code": 422,
- * "message": "回调地址不能为空|回调地址必须是一个有效的URL"
- * }
- * @response 400 scenario="参数错误" {
- * "code": 400,
- * "message": "生成授权链接失败,请稍后重试"
- * }
- *
- * @throws BusinessException
- */
- public function getAuthUrl(GetAuthUrlRequest $request)
- {
- $validated = $request->validated();
- $result = $this->wechatService->getAuthUrl(
- $validated['redirect_uri'],
- $validated['scope'] ?? 'snsapi_userinfo'
- );
- return $this->success($result);
- }
- /**
- * [微信]处理微信授权回调
- *
- * @description 处理微信授权回调,完成登录。用于接收微信授权后的回调,处理用户登录注册流程。
- *
- * @bodyParam code string required 微信授权码 Example: 071Jh6ll2tQfM54Kjtol2pWDCE1Jh6lH
- * @bodyParam state string required 状态码,需要与获取授权URL时返回的state一致,长度必须为32位 Example: 7d8f69a...
- *
- * @response 200 scenario="成功" {
- * "code": 200,
- * "data": {
- * "token": "1|2uyxxxxxxxxxx",
- * "user": {
- * "id": 1,
- * "nickname": "张三",
- * "avatar": "https://thirdwx.qlogo.cn/mmopen/xxx/132",
- * "gender": "male",
- * "country": "中国",
- * "province": "广东",
- * "city": "深圳",
- * "created_at": "2024-01-01 00:00:00",
- * "updated_at": "2024-01-01 00:00:00"
- * }
- * }
- * }
- * @response 422 scenario="参数验证失败" {
- * "code": 422,
- * "message": "授权码不能为空|状���码长度必须为32位"
- * }
- * @response 400 scenario="无效的state" {
- * "code": 400,
- * "message": "无效的授权请求"
- * }
- * @response 400 scenario="授权失败" {
- * "code": 400,
- * "message": "微信授权失败,请稍后重试"
- * }
- *
- * @throws BusinessException 当授权验证失败或处理出错时抛出
- */
- public function handleCallback(HandleCallbackRequest $request)
- {
- $validated = $request->validated();
- $result = $this->wechatService->handleAuthCallback(
- $validated['code'],
- $validated['state'],
- $validated['invite_code'] ?? null
- );
- 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);
- }
- }
|