AccountController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace App\Http\Controllers\Client;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Client\Account\BindMobileRequest;
  5. use App\Http\Requests\Client\Account\LoginRequest;
  6. use App\Http\Requests\Client\Account\SendVerifyCodeRequest;
  7. use App\Http\Requests\Client\Account\WxLoginRequest;
  8. use App\Services\Client\AccountService;
  9. use Illuminate\Support\Facades\Auth;
  10. /**
  11. * @group 用户端
  12. *
  13. * 包含登录、注册、账户管理等基础功能
  14. */
  15. class AccountController extends Controller
  16. {
  17. protected AccountService $service;
  18. public function __construct(AccountService $service)
  19. {
  20. $this->service = $service;
  21. }
  22. /**
  23. * [账户]发送验证码
  24. *
  25. * 向指定手机号发送验证码,用于登录或注册验证
  26. *
  27. *
  28. * @bodyParam mobile string required 手机号码,必须是11位有效的中国大陆手机号. Example: 13800138000
  29. *
  30. * @response 200 {
  31. * "code": 200,
  32. * "message": "验证码发送成功",
  33. * "data": {
  34. * "message": "验证码发送成功",
  35. * "code": 123456
  36. * }
  37. * }
  38. * @response 422 {
  39. * "code": 422,
  40. * "message": "手机号格式不正确",
  41. * "data": {
  42. * "mobile": ["手机号必须是11位有效的中国大陆手机号"]
  43. * }
  44. * }
  45. * @response 500 {
  46. * "code": 500,
  47. * "message": "验证码发送失败",
  48. * "data": null
  49. * }
  50. */
  51. public function sendVerifyCode(SendVerifyCodeRequest $request)
  52. {
  53. $validated = $request->validated();
  54. return $this->success(
  55. $this->service->sendVerifyCode($validated['mobile'])
  56. );
  57. }
  58. /**
  59. * [账户]手机号登录
  60. *
  61. * 使用手机号和验证码进行登录,支持新用户自动注册
  62. *
  63. *
  64. * @bodyParam mobile string required 手机号码,必须是11位有效的中国大陆手机号. Example: 13800138000
  65. * @bodyParam code string required 验证码,必须是6位数字. Example: 123456
  66. * @bodyParam invite_code string optional 邀请码,格式为:type_id,如 user_1 表示用户邀请,coach_1 表示技师邀请. Example: user_1
  67. *
  68. * @response 200 {
  69. * "code": 200,
  70. * "message": "登录成功",
  71. * "data": {
  72. * "token": "1|abcdefghijklmnopqrstuvwxyz",
  73. * "user": {
  74. * "id": 1,
  75. * "mobile": "13800138000",
  76. * "nickname": null,
  77. * "avatar": null,
  78. * "gender": null,
  79. * "state": "open",
  80. * "register_area": "330100",
  81. * "created_at": "2024-01-01 00:00:00",
  82. * "updated_at": "2024-01-01 00:00:00"
  83. * }
  84. * }
  85. * }
  86. * @response 422 {
  87. * "code": 422,
  88. * "message": "验证失败",
  89. * "data": {
  90. * "mobile": ["手机号格式不正确"],
  91. * "code": ["验证码必须是6位数字"]
  92. * }
  93. * }
  94. * @response 400 {
  95. * "code": 400,
  96. * "message": "验证码错误",
  97. * "data": null
  98. * }
  99. */
  100. public function login(LoginRequest $request)
  101. {
  102. $validated = $request->validated();
  103. return $this->success(
  104. $this->service->login(
  105. $validated['mobile'],
  106. $validated['code'],
  107. $validated['invite_code'] ?? null
  108. )
  109. );
  110. }
  111. /**
  112. * [账户]微信登录
  113. *
  114. * 使用微信openid进行登录,支持新用户自动注册,可选同步微信用户信息
  115. *
  116. *
  117. * @bodyParam openid string required 微信openid,必须是有效的微信用户标识. Example: wx_123456789
  118. * @bodyParam userInfo.nickname string optional 用户昵称. Example: 张三
  119. * @bodyParam userInfo.avatar string optional 头像URL,必须是有效的URL地址. Example: https://thirdwx.qlogo.cn/xxx.jpg
  120. * @bodyParam userInfo.gender int optional 性别(0未知1男2女). Example: 1
  121. * @bodyParam userInfo.invite_code string optional 邀请码,格式为:type_id. Example: user_1
  122. *
  123. * @response 200 {
  124. * "code": 200,
  125. * "message": "登录成功",
  126. * "data": {
  127. * "token": "1|abcdefghijklmnopqrstuvwxyz",
  128. * "user": {
  129. * "id": 1,
  130. * "nickname": "张三",
  131. * "avatar": "https://thirdwx.qlogo.cn/xxx.jpg",
  132. * "gender": 1,
  133. * "state": "open",
  134. * "register_area": "330100",
  135. * "created_at": "2024-01-01 00:00:00",
  136. * "updated_at": "2024-01-01 00:00:00"
  137. * }
  138. * }
  139. * }
  140. * @response 422 {
  141. * "code": 422,
  142. * "message": "验证失败",
  143. * "data": {
  144. * "openid": ["微信openid不能为空"],
  145. * "userInfo.avatar": ["头像必须是有效的URL地址"],
  146. * "userInfo.gender": ["性别值无效"]
  147. * }
  148. * }
  149. */
  150. public function wxLogin(WxLoginRequest $request)
  151. {
  152. $validated = $request->validated();
  153. return $this->success(
  154. $this->service->wxLogin($validated['openid'], $validated['userInfo'] ?? [])
  155. );
  156. }
  157. /**
  158. * [账户]退出登录
  159. *
  160. * 退出当前用户的登录状态,清除认证令牌
  161. *
  162. *
  163. * @authenticated
  164. *
  165. * @response 200 {
  166. * "code": 200,
  167. * "message": "退出成功",
  168. * "data": {
  169. * "message": "退出成功"
  170. * }
  171. * }
  172. * @response 401 {
  173. * "code": 401,
  174. * "message": "未登录或登录已过期",
  175. * "data": null
  176. * }
  177. */
  178. public function logout()
  179. {
  180. return $this->success(
  181. $this->service->logout(Auth::user()->id)
  182. );
  183. }
  184. /**
  185. * [账户]注销账号
  186. *
  187. * 永久注销当前用户账号,清除认证令牌,账号将无法恢复
  188. *
  189. *
  190. * @authenticated
  191. *
  192. * @response 200 {
  193. * "code": 200,
  194. * "message": "注销成功",
  195. * "data": {
  196. * "message": "账号已注销"
  197. * }
  198. * }
  199. * @response 401 {
  200. * "code": 401,
  201. * "message": "未登录或登录已过期",
  202. * "data": null
  203. * }
  204. * @response 400 {
  205. * "code": 400,
  206. * "message": "用户状态异常",
  207. * "data": null
  208. * }
  209. */
  210. public function destroy()
  211. {
  212. return $this->success(
  213. $this->service->deleteAccount()
  214. );
  215. }
  216. /**
  217. * [账户]绑定/修改手机号
  218. *
  219. * 为当前用户绑定新的手机号,如已绑定则更新为新手机号
  220. *
  221. *
  222. * @authenticated
  223. *
  224. * @bodyParam mobile string required 新手机号码,必须是11位有效的中国大陆手机号. Example: 13800138000
  225. * @bodyParam code string required 验证码,必须是6位数字. Example: 123456
  226. *
  227. * @response 200 {
  228. * "code": 200,
  229. * "message": "手机号绑定成功",
  230. * "data": {
  231. * "message": "手机号绑定成功",
  232. * "user": {
  233. * "id": 1,
  234. * "mobile": "13800138000",
  235. * "nickname": "张三",
  236. * "avatar": "https://example.com/avatar.jpg",
  237. * "gender": 1,
  238. * "state": "open",
  239. * "register_area": "330100",
  240. * "created_at": "2024-01-01 00:00:00",
  241. * "updated_at": "2024-01-01 00:00:00"
  242. * }
  243. * }
  244. * }
  245. * @response 422 {
  246. * "code": 422,
  247. * "message": "验证失败",
  248. * "data": {
  249. * "mobile": ["手机号格式不正确"],
  250. * "code": ["验证码必须是6位数字"]
  251. * }
  252. * }
  253. * @response 400 {
  254. * "code": 400,
  255. * "message": "验证码错误",
  256. * "data": null
  257. * }
  258. * @response 409 {
  259. * "code": 409,
  260. * "message": "手机号已被其他用户使用",
  261. * "data": null
  262. * }
  263. */
  264. public function bindMobile(BindMobileRequest $request)
  265. {
  266. $validated = $request->validated();
  267. return $this->success(
  268. $this->service->bindMobile(
  269. $validated['mobile'],
  270. $validated['code']
  271. )
  272. );
  273. }
  274. }