WechatController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. use Illuminate\Http\Request;
  8. /**
  9. * @group 用户端
  10. *
  11. * 微信相关的API接口
  12. */
  13. class WechatController extends Controller
  14. {
  15. protected WechatService $wechatService;
  16. public function __construct(WechatService $wechatService)
  17. {
  18. $this->wechatService = $wechatService;
  19. }
  20. /**
  21. * [微信]获取微信授权URL
  22. *
  23. * @description 获取微信网页授权URL
  24. *
  25. * @queryParam redirect_url required 授权后的回调地址,必须是有效的URL Example: https://example.com/callback
  26. * @queryParam scope 授权范围,可选值:snsapi_base,snsapi_userinfo Example: snsapi_userinfo
  27. *
  28. * @response 200 scenario="成功" {
  29. * "code": 200,
  30. * "data": {
  31. * "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...",
  32. * "state": "7d8f69a..."
  33. * }
  34. * }
  35. * @response 422 scenario="参数验证失败" {
  36. * "code": 422,
  37. * "message": "回调地址不能为空|回调地址必须是一个有效的URL"
  38. * }
  39. * @response 400 scenario="参数错误" {
  40. * "code": 400,
  41. * "message": "生成授权链接失败,请稍后重试"
  42. * }
  43. *
  44. * @throws BusinessException
  45. */
  46. public function getAuthUrl(GetAuthUrlRequest $request)
  47. {
  48. $validated = $request->validated();
  49. $result = $this->wechatService->getAuthUrl(
  50. $validated['redirect_uri'],
  51. $validated['scope'] ?? 'snsapi_userinfo'
  52. );
  53. return $this->success($result);
  54. }
  55. /**
  56. * [微信]处理微信授权回调
  57. *
  58. * @description 处理微信授权回调,完成登录。用于接收微信授权后的回调,处理用户登录注册流程。
  59. *
  60. * @bodyParam code string required 微信授权码 Example: 071Jh6ll2tQfM54Kjtol2pWDCE1Jh6lH
  61. * @bodyParam state string required 状态码,需要与获取授权URL时返回的state一致,长度必须为32位 Example: 7d8f69a...
  62. *
  63. * @response 200 scenario="成功" {
  64. * "code": 200,
  65. * "data": {
  66. * "token": "1|2uyxxxxxxxxxx",
  67. * "user": {
  68. * "id": 1,
  69. * "nickname": "张三",
  70. * "avatar": "https://thirdwx.qlogo.cn/mmopen/xxx/132",
  71. * "gender": "male",
  72. * "country": "中国",
  73. * "province": "广东",
  74. * "city": "深圳",
  75. * "created_at": "2024-01-01 00:00:00",
  76. * "updated_at": "2024-01-01 00:00:00"
  77. * }
  78. * }
  79. * }
  80. * @response 422 scenario="参数验证失败" {
  81. * "code": 422,
  82. * "message": "授权码不能为空|状���码长度必须为32位"
  83. * }
  84. * @response 400 scenario="无效的state" {
  85. * "code": 400,
  86. * "message": "无效的授权请求"
  87. * }
  88. * @response 400 scenario="授权失败" {
  89. * "code": 400,
  90. * "message": "微信授权失败,请稍后重试"
  91. * }
  92. *
  93. * @throws BusinessException 当授权验证失败或处理出错时抛出
  94. */
  95. public function handleCallback(HandleCallbackRequest $request)
  96. {
  97. $validated = $request->validated();
  98. $result = $this->wechatService->handleAuthCallback(
  99. $validated['code'],
  100. $validated['state'],
  101. $validated['invite_code'] ?? null
  102. );
  103. return $this->success($result);
  104. }
  105. /**
  106. * [微信]获取JSAPI配置
  107. *
  108. * @description 获取微信JSAPI配置信息,用于前端调用微信JSAPI
  109. *
  110. * @queryParam url required 当前网页的URL,不包含#及其后面部分 Example: https://example.com/page
  111. *
  112. * @response 200 scenario="成功" {
  113. * "code": 200,
  114. * "data": {
  115. * "appId": "wx123456789",
  116. * "timestamp": 1577808000,
  117. * "nonceStr": "random_string",
  118. * "signature": "sha1_signature_string",
  119. * "jsApiList": ["updateAppMessageShareData", "updateTimelineShareData"]
  120. * }
  121. * }
  122. * @response 422 scenario="参数验证失败" {
  123. * "code": 422,
  124. * "message": "URL不能为空|URL必须是一个有效的网址"
  125. * }
  126. * @response 400 scenario="获取配置失败" {
  127. * "code": 400,
  128. * "message": "获取JSAPI配置失败,请稍后重试"
  129. * }
  130. *
  131. * @throws BusinessException
  132. */
  133. public function getJsConfig(Request $request)
  134. {
  135. $url = $request->input('url');
  136. if (empty($url)) {
  137. return $this->error('URL不能为空');
  138. }
  139. if (!filter_var($url, FILTER_VALIDATE_URL)) {
  140. return $this->error('URL必须是一个有效的网址');
  141. }
  142. $config = $this->wechatService->getJsConfig($url);
  143. return $this->success($config);
  144. }
  145. }