AccountController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Http\Controllers\Client;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Client\Account\LoginRequest;
  5. use App\Http\Requests\Client\Account\SendVerifyCodeRequest;
  6. use App\Http\Requests\Client\Account\WxLoginRequest;
  7. use App\Services\Client\AccountService;
  8. use Illuminate\Support\Facades\Auth;
  9. /**
  10. * @group 用户端
  11. *
  12. * 包含登录、注册、账户管理等基础功能
  13. */
  14. class AccountController extends Controller
  15. {
  16. protected AccountService $service;
  17. public function __construct(AccountService $service)
  18. {
  19. $this->service = $service;
  20. }
  21. /**
  22. * 发送验证码
  23. *
  24. * 向指定手机号发送验证码
  25. *
  26. * @queryParam mobile string required 手机号码. Example: 13800138000
  27. *
  28. * @response {
  29. * "code": 200,
  30. * "message": "验证码发送成功",
  31. * "data": null
  32. * }
  33. */
  34. public function sendVerifyCode(SendVerifyCodeRequest $request)
  35. {
  36. $validated = $request->validated();
  37. return $this->success(
  38. $this->service->sendVerifyCode($validated['mobile'])
  39. );
  40. }
  41. /**
  42. * 用户登录
  43. *
  44. * 使用手机号和验证码登录账户
  45. *
  46. * @bodyParam mobile string required 手机号码. Example: 13800138000
  47. * @bodyParam code string required 验证码. Example: 123456
  48. * @bodyParam invite_code string optional 邀请码. Example: user_1
  49. *
  50. * @response {
  51. * "code": 200,
  52. * "message": "登录成功",
  53. * "data": {
  54. * "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
  55. * "user": {
  56. * "id": 1,
  57. * "mobile": "13800138000",
  58. * "nickname": "用户昵称"
  59. * }
  60. * }
  61. * }
  62. */
  63. public function login(LoginRequest $request)
  64. {
  65. $validated = $request->validated();
  66. return $this->success(
  67. $this->service->login(
  68. $validated['mobile'],
  69. $validated['code'],
  70. $validated['invite_code'] ?? null
  71. )
  72. );
  73. }
  74. /**
  75. * 微信登录
  76. *
  77. * 使用微信openid登录账户
  78. *
  79. * @bodyParam openid string required 微信openid. Example: wx_123456789
  80. * @bodyParam userInfo object optional 微信用户信息.
  81. * @bodyParam userInfo.nickname string optional 用户昵称. Example: 张三
  82. * @bodyParam userInfo.avatar string optional 头像URL. Example: https://xxx.com/avatar.jpg
  83. * @bodyParam userInfo.gender int optional 性别(1男2女0未知). Example: 1
  84. * @bodyParam userInfo.invite_code string optional 邀请码. Example: user_1
  85. *
  86. * @response {
  87. * "code": 200,
  88. * "message": "登录成功",
  89. * "data": {
  90. * "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
  91. * "user": {
  92. * "id": 1,
  93. * "openid": "wx_123456789",
  94. * "nickname": "微信昵称"
  95. * }
  96. * }
  97. * }
  98. */
  99. public function wxLogin(WxLoginRequest $request)
  100. {
  101. $validated = $request->validated();
  102. return $this->success(
  103. $this->service->wxLogin($validated['openid'], $validated['userInfo'] ?? [])
  104. );
  105. }
  106. /**
  107. * 用户退出
  108. *
  109. * 退出当前账户登录状态
  110. *
  111. * @authenticated
  112. *
  113. * @response {
  114. * "code": 200,
  115. * "message": "退出成功",
  116. * "data": null
  117. * }
  118. */
  119. public function logout()
  120. {
  121. return $this->success(
  122. $this->service->logout(Auth::user()->id)
  123. );
  124. }
  125. /**
  126. * 用户注销
  127. *
  128. * 永久注销当前账户
  129. *
  130. * @authenticated
  131. *
  132. * @response {
  133. * "code": 200,
  134. * "message": "注销成功",
  135. * "data": null
  136. * }
  137. */
  138. public function destroy()
  139. {
  140. return $this->success(
  141. $this->service->deleteAccount()
  142. );
  143. }
  144. }