WechatController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Http\Controllers\Client;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Client\Wechat\GetAuthUrlRequest;
  5. use App\Http\Requests\Client\Wechat\HandleCallbackRequest;
  6. use App\Services\Client\WechatService;
  7. /**
  8. * @group 用户端
  9. *
  10. * 微信相关的API接口
  11. */
  12. class WechatController extends Controller
  13. {
  14. protected WechatService $wechatService;
  15. public function __construct(WechatService $wechatService)
  16. {
  17. $this->wechatService = $wechatService;
  18. }
  19. /**
  20. * [微信]获取微信授权URL
  21. *
  22. * @description 获取微信网页授权URL
  23. *
  24. * @queryParam redirect_url required 授权后的回调地址,必须是有效的URL Example: https://example.com/callback
  25. * @queryParam scope 授权范围,可选值:snsapi_base,snsapi_userinfo Example: snsapi_userinfo
  26. *
  27. * @response 200 scenario="成功" {
  28. * "code": 200,
  29. * "data": {
  30. * "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...",
  31. * "state": "7d8f69a..."
  32. * }
  33. * }
  34. * @response 422 scenario="参数验证失败" {
  35. * "code": 422,
  36. * "message": "回调地址不能为空|回调地址必须是一个有效的URL"
  37. * }
  38. * @response 400 scenario="参数错误" {
  39. * "code": 400,
  40. * "message": "生成授权链接失败,请稍后重试"
  41. * }
  42. *
  43. * @throws BusinessException
  44. */
  45. public function getAuthUrl(GetAuthUrlRequest $request)
  46. {
  47. $validated = $request->validated();
  48. $result = $this->wechatService->getAuthUrl(
  49. $validated['redirect_url'],
  50. $validated['scope'] ?? 'snsapi_userinfo'
  51. );
  52. return $this->success($result);
  53. }
  54. /**
  55. * [微信]处理微信授权回调
  56. *
  57. * @description 处理微信授权回调,完成登录。用于接收微信授权后的回调,处理用户登录注册流程。
  58. *
  59. * @bodyParam code string required 微信授权码 Example: 071Jh6ll2tQfM54Kjtol2pWDCE1Jh6lH
  60. * @bodyParam state string required 状态码,需要与获取授权URL时返回的state一致,长度必须为32位 Example: 7d8f69a...
  61. *
  62. * @response 200 scenario="成功" {
  63. * "code": 200,
  64. * "data": {
  65. * "token": "1|2uyxxxxxxxxxx",
  66. * "user": {
  67. * "id": 1,
  68. * "nickname": "张三",
  69. * "avatar": "https://thirdwx.qlogo.cn/mmopen/xxx/132",
  70. * "gender": "male",
  71. * "country": "中国",
  72. * "province": "广东",
  73. * "city": "深圳",
  74. * "created_at": "2024-01-01 00:00:00",
  75. * "updated_at": "2024-01-01 00:00:00"
  76. * }
  77. * }
  78. * }
  79. * @response 422 scenario="参数验证失败" {
  80. * "code": 422,
  81. * "message": "授权码不能为空|状态码长度必须为32位"
  82. * }
  83. * @response 400 scenario="无效的state" {
  84. * "code": 400,
  85. * "message": "无效的授权请求"
  86. * }
  87. * @response 400 scenario="授权失败" {
  88. * "code": 400,
  89. * "message": "微信授权失败,请稍后重试"
  90. * }
  91. *
  92. * @throws BusinessException 当授权验证失败或处理出错时抛出
  93. */
  94. public function handleCallback(HandleCallbackRequest $request)
  95. {
  96. $validated = $request->validated();
  97. $result = $this->wechatService->handleAuthCallback(
  98. $validated['code'],
  99. $validated['state'],
  100. $validated['invite_code'] ?? null
  101. );
  102. return $this->success($result);
  103. }
  104. }