UserController.php 11 KB

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