UserController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. namespace App\Http\Controllers\Client;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Client\User\ApplyCoachRequest;
  5. use App\Http\Requests\Client\User\FeedbackRequest;
  6. use App\Http\Requests\Client\User\RegisterRequest;
  7. use App\Http\Requests\Client\User\UpdateRequest;
  8. use App\Http\Resources\Client\UserResource;
  9. use App\Services\Client\UserService;
  10. use Illuminate\Support\Facades\Auth;
  11. /**
  12. * @group 用户端
  13. *
  14. * 用户相关的API接口
  15. */
  16. class UserController extends Controller
  17. {
  18. protected UserService $service;
  19. public function __construct(UserService $service)
  20. {
  21. $this->service = $service;
  22. }
  23. /**
  24. * [用户]获取用户信息
  25. *
  26. * @description 获取当前登录用户的详细信息
  27. *
  28. * @authenticated
  29. *
  30. * @response 200 {
  31. * "code": 200,
  32. * "message": "获取成功",
  33. * "data": {
  34. * "id": 1,
  35. * "mobile": "13800138000",
  36. * "nickname": "张三",
  37. * "avatar": "https://example.com/avatar.jpg",
  38. * "gender": "male",
  39. * "created_at": "2024-03-20 10:00:00",
  40. * "updated_at": "2024-03-20 10:00:00"
  41. * }
  42. * }
  43. * @response 401 {
  44. * "code": 401,
  45. * "message": "请先登录",
  46. * "data": null
  47. * }
  48. */
  49. public function show()
  50. {
  51. $data = $this->service->getUserInfo();
  52. return $this->success(new UserResource($data));
  53. }
  54. /**
  55. * [用户]用户注册
  56. *
  57. * @description 新用户注册接口,支持邀请注册功能
  58. *
  59. * @bodyParam mobile string required 手机号码 Example: 13800138000
  60. * @bodyParam code string required 短信验证码 Example: 123456
  61. * @bodyParam invite_code string optional 邀请码 Example: ABC123
  62. * @bodyParam invite_id integer optional 邀请人ID Example: 1
  63. * @bodyParam invite_role string optional 邀请人角色(user/coach) Example: user
  64. *
  65. * @response 200 {
  66. * "code": 200,
  67. * "message": "注册成功",
  68. * "data": {
  69. * "user_id": 1,
  70. * "mobile": "13800138000",
  71. * "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
  72. * "invite_code": "DEF456"
  73. * }
  74. * }
  75. * @response 422 {
  76. * "code": 422,
  77. * "message": "验证失败",
  78. * "errors": {
  79. * "mobile": ["手机号���格式不正确"],
  80. * "code": ["验证码错误"]
  81. * }
  82. * }
  83. */
  84. public function register(RegisterRequest $request)
  85. {
  86. $validated = $request->validated();
  87. $data = $this->service->register(
  88. $validated['mobile'],
  89. $validated['code'],
  90. $validated['invite_code'] ?? null,
  91. $validated['invite_id'] ?? null,
  92. $validated['invite_role'] ?? null
  93. );
  94. return $this->success($data, '注册成功');
  95. }
  96. /**
  97. * [用户]修改用户信息
  98. *
  99. * @description 修改当前登录用户的基本信息,包括昵称、头像和性别
  100. *
  101. * @authenticated
  102. *
  103. * @bodyParam nickname string optional 用户昵称 Example: 张三
  104. * @bodyParam avatar string optional 头像URL Example: https://example.com/avatar.jpg
  105. * @bodyParam gender integer optional 性别(0:未知/1:男/2:女) Example: 1
  106. *
  107. * @response 200 {
  108. * "code": 200,
  109. * "message": "修改成功",
  110. * "data": {
  111. * "id": 1,
  112. * "nickname": "张三",
  113. * "avatar": "https://example.com/avatar.jpg",
  114. * "gender": 1,
  115. * "gender_text": "男",
  116. * "updated_at": "2024-03-20 10:00:00"
  117. * }
  118. * }
  119. * @response 422 {
  120. * "code": 422,
  121. * "message": "验证失败",
  122. * "errors": {
  123. * "avatar": ["头像必须是有效的URL地址"]
  124. * }
  125. * }
  126. */
  127. public function update(UpdateRequest $request)
  128. {
  129. $validated = $request->validated();
  130. $result = $this->service->updateUserInfo($validated);
  131. return $this->success($result, '修改成功');
  132. }
  133. /**
  134. * [用户]用户反馈
  135. *
  136. * @description 提交用户反馈信息
  137. *
  138. * @authenticated
  139. *
  140. * @bodyParam content string required 反馈内容 Example: 系统使用体验很好,建议增加更多功能
  141. * @bodyParam images array optional 图片数组 Example: ["https://example.com/image1.jpg", "https://example.com/image2.jpg"]
  142. * @bodyParam contact string optional 联系方式 Example: 13800138000
  143. *
  144. * @response 200 {
  145. * "code": 200,
  146. * "message": "反馈提交成功",
  147. * "data": {
  148. * "id": 1,
  149. * "content": "系统使用体验很好,建议增加更多功能",
  150. * "images": ["https://example.com/image1.jpg"],
  151. * "created_at": "2024-03-20 10:00:00"
  152. * }
  153. * }
  154. * @response 422 {
  155. * "code": 422,
  156. * "message": "内容不能为空",
  157. * "data": null
  158. * }
  159. */
  160. public function feedback(FeedbackRequest $request)
  161. {
  162. $validated = $request->validated();
  163. $result = $this->service->feedback(
  164. $validated['content'],
  165. $validated['images'] ?? [],
  166. $validated['contact'] ?? null
  167. );
  168. return $this->success($result, '反馈提交成功');
  169. }
  170. /**
  171. * [用户]申请成为技师
  172. *
  173. * @description 普通用户申请成为平台技师,提交技师申请信息
  174. *
  175. * @authenticated
  176. *
  177. * @bodyParam age integer required 年龄(18-60岁) Example: 25
  178. * @bodyParam mobile string required 联系电话 Example: 13800138000
  179. * @bodyParam gender integer required 性别(1:男/2:女) Example: 1
  180. * @bodyParam work_years integer required 工作年限(0-50年) Example: 5
  181. * @bodyParam intention_city string required 意向城市 Example: 杭州
  182. * @bodyParam portrait_images array required 形象照片(最多6张) Example: ["https://example.com/portrait1.jpg"]
  183. * @bodyParam introduction string optional 个人简介(最多1000字) Example: 专业按摩师,有多年经验
  184. *
  185. * @response 200 {
  186. * "code": 200,
  187. * "message": "申请提交成功",
  188. * "data": {
  189. * "id": 1,
  190. * "coach_id": 100,
  191. * "age": 25,
  192. * "mobile": "13800138000",
  193. * "gender": 1,
  194. * "work_years": 5,
  195. * "intention_city": "杭州",
  196. * "portrait_images": ["https://example.com/portrait1.jpg"],
  197. * "introduction": "专业按摩师,有多年经验",
  198. * "state": "auditing",
  199. * "created_at": "2024-03-20 10:00:00",
  200. * "updated_at": "2024-03-20 10:00:00"
  201. * }
  202. * }
  203. * @response 401 {
  204. * "code": 401,
  205. * "message": "请先登录",
  206. * "data": null
  207. * }
  208. * @response 422 {
  209. * "code": 422,
  210. * "message": "验证失败",
  211. * "errors": {
  212. * "mobile": ["手机号码格式不正确"],
  213. * "gender": ["性别只能是1(男)或2(女)"],
  214. * "work_years": ["工作年限必须是0-50之间的整数"],
  215. * "intention_city": ["意向城市不能为空"],
  216. * "portrait_images": ["形象照片不能为空"],
  217. * "portrait_images.*": ["图片必须是有效的URL地址"]
  218. * }
  219. * }
  220. * @response 422 {
  221. * "code": 422,
  222. * "message": "您已是技师,无需重复申请",
  223. * "data": null
  224. * }
  225. * @response 422 {
  226. * "code": 422,
  227. * "message": "您有正在审核的申请,请耐心等待",
  228. * "data": null
  229. * }
  230. */
  231. public function applyCoach(ApplyCoachRequest $request)
  232. {
  233. $validated = $request->validated();
  234. $result = $this->service->applyCoach(
  235. $validated['age'],
  236. $validated['mobile'],
  237. $validated['gender'],
  238. $validated['work_years'],
  239. $validated['intention_city'],
  240. $validated['portrait_images'],
  241. $validated['introduction'] ?? null
  242. );
  243. return $this->success($result, '申请提交成功');
  244. }
  245. /**
  246. * [用户]生成邀请二维码
  247. *
  248. * @description 生成当前用户专属的邀请码和邀请二维码,支持普通用户和技师两种身份
  249. *
  250. * @authenticated
  251. *
  252. * @queryParam type string required 邀请码类型(user:普通用户/coach:技师) Example: user
  253. *
  254. * @response 200 {
  255. * "code": 200,
  256. * "message": "生成成功",
  257. * "data": {
  258. * "invite_code": "user_1",
  259. * "invite_url": "https://example.com/invite?invite_id=1&invite_role=user&invite_code=user_1",
  260. * "qr_code": "data:image/svg+xml;base64,..."
  261. * }
  262. * }
  263. * @response 401 {
  264. * "code": 401,
  265. * "message": "请先登录",
  266. * "data": null
  267. * }
  268. * @response 422 {
  269. * "code": 422,
  270. * "message": "无法生成邀请码,用户类型不匹配",
  271. * "data": null
  272. * }
  273. */
  274. public function generateInviteCode()
  275. {
  276. $result = $this->service->generateInviteCode();
  277. return $this->success($result, '生成成功');
  278. }
  279. /**
  280. * [用户]查看技师申请记录
  281. *
  282. * @description 获取当前用户的技师申请记录,如果用户不是技师或没有申请记录则返回 null
  283. *
  284. * @authenticated
  285. *
  286. * @response 200 {
  287. * "code": 200,
  288. * "message": "获取成功",
  289. * "data": {
  290. * "id": 1,
  291. * "coach_id": 100,
  292. * "age": 25,
  293. * "mobile": "13800138000",
  294. * "gender": 1,
  295. * "work_years": 5,
  296. * "intention_city": "杭州",
  297. * "portrait_images": ["https://example.com/portrait1.jpg"],
  298. * "introduction": "专业按摩师,有多年经验",
  299. * "state": 1,
  300. * "state_text": "待审核",
  301. * "created_at": "2024-03-20 10:00:00",
  302. * "updated_at": "2024-03-20 10:00:00"
  303. * }
  304. * }
  305. * @response 200 {
  306. * "code": 200,
  307. * "message": "您还不是技师",
  308. * "data": null
  309. * }
  310. * @response 200 {
  311. * "code": 200,
  312. * "message": "暂无申请记录",
  313. * "data": null
  314. * }
  315. * @response 401 {
  316. * "code": 401,
  317. * "message": "请先登录",
  318. * "data": null
  319. * }
  320. */
  321. public function getCoachApplication()
  322. {
  323. $result = $this->service->getCoachApplication();
  324. // 如果用户不是技师
  325. if ($result === null && ! Auth::user()->coach) {
  326. return $this->success(null, '您还不是技师');
  327. }
  328. // 如果是技师但没有申请记录
  329. if ($result === null) {
  330. return $this->success(null, '暂无申请记录');
  331. }
  332. return $this->success($result, '获取成功');
  333. }
  334. }