UserController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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: 0
  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": "male",
  115. * "updated_at": "2024-03-20 10:00:00"
  116. * }
  117. * }
  118. * @response 401 {
  119. * "code": 401,
  120. * "message": "请先登录",
  121. * "data": null
  122. * }
  123. */
  124. public function update(UpdateRequest $request)
  125. {
  126. $validated = $request->validated();
  127. $result = $this->service->updateUserInfo($validated);
  128. return $this->success($result, '修改成功');
  129. }
  130. /**
  131. * [用户]用户反馈
  132. *
  133. * @description 提交用户���馈信息
  134. *
  135. * @authenticated
  136. *
  137. * @bodyParam content string required 反馈内容 Example: 系统使用体验很好,建议增加更多功能
  138. * @bodyParam images array optional 图片数组 Example: ["https://example.com/image1.jpg", "https://example.com/image2.jpg"]
  139. * @bodyParam contact string optional 联系方式 Example: 13800138000
  140. *
  141. * @response 200 {
  142. * "code": 200,
  143. * "message": "反馈提交成功",
  144. * "data": {
  145. * "id": 1,
  146. * "content": "系统使用体验很好,建议增加更多功能",
  147. * "images": ["https://example.com/image1.jpg"],
  148. * "created_at": "2024-03-20 10:00:00"
  149. * }
  150. * }
  151. * @response 422 {
  152. * "code": 422,
  153. * "message": "内容不能为空",
  154. * "data": null
  155. * }
  156. */
  157. public function feedback(FeedbackRequest $request)
  158. {
  159. $validated = $request->validated();
  160. $result = $this->service->feedback(
  161. $validated['content'],
  162. $validated['images'] ?? [],
  163. $validated['contact'] ?? null
  164. );
  165. return $this->success($result, '反馈提交成功');
  166. }
  167. /**
  168. * [用户]申请成为技师
  169. *
  170. * @description 普通用户申请成为平台技师,提交技师申��信息
  171. *
  172. * @authenticated
  173. *
  174. * @bodyParam age integer required 年龄(18-60岁) Example: 25
  175. * @bodyParam mobile string required 联系电话 Example: 13800138000
  176. * @bodyParam gender integer required 性别(1:男/2:女) Example: 1
  177. * @bodyParam work_years integer required 工作年限(0-50年) Example: 5
  178. * @bodyParam intention_city string required 意向城市 Example: 杭州
  179. * @bodyParam portrait_images array required 形象照片(最多6张) Example: ["https://example.com/portrait1.jpg"]
  180. * @bodyParam introduction string optional 个人简介(最多1000字) Example: 专业按摩师,有多年经验
  181. *
  182. * @response 200 {
  183. * "code": 200,
  184. * "message": "申请提交成功",
  185. * "data": {
  186. * "id": 1,
  187. * "coach_id": 100,
  188. * "age": 25,
  189. * "mobile": "13800138000",
  190. * "gender": 1,
  191. * "work_years": 5,
  192. * "intention_city": "杭州",
  193. * "portrait_images": ["https://example.com/portrait1.jpg"],
  194. * "introduction": "专业按摩师,有多年经验",
  195. * "state": "auditing",
  196. * "created_at": "2024-03-20 10:00:00",
  197. * "updated_at": "2024-03-20 10:00:00"
  198. * }
  199. * }
  200. * @response 401 {
  201. * "code": 401,
  202. * "message": "请先登录",
  203. * "data": null
  204. * }
  205. * @response 422 {
  206. * "code": 422,
  207. * "message": "验证失败",
  208. * "errors": {
  209. * "mobile": ["手机号码格式不正确"],
  210. * "gender": ["性别只能是1(男)或2(女)"],
  211. * "work_years": ["工作年限必须是0-50之间的整数"],
  212. * "intention_city": ["意向城市不能为空"],
  213. * "portrait_images": ["形象照片不能为空"],
  214. * "portrait_images.*": ["图片必须是有效的URL地址"]
  215. * }
  216. * }
  217. * @response 422 {
  218. * "code": 422,
  219. * "message": "您已是技师,无需重复申请",
  220. * "data": null
  221. * }
  222. * @response 422 {
  223. * "code": 422,
  224. * "message": "您有正在审核的申请,请耐心等待",
  225. * "data": null
  226. * }
  227. */
  228. public function applyCoach(ApplyCoachRequest $request)
  229. {
  230. $validated = $request->validated();
  231. $result = $this->service->applyCoach(
  232. $validated['age'],
  233. $validated['mobile'],
  234. $validated['gender'],
  235. $validated['work_years'],
  236. $validated['intention_city'],
  237. $validated['portrait_images'],
  238. $validated['introduction'] ?? null
  239. );
  240. return $this->success($result, '申请提交成功');
  241. }
  242. /**
  243. * [用户]生成邀请二维码
  244. *
  245. * @description 生成当前用户专属的邀请码和邀请二维码,支持普通用户和技师两种身份
  246. *
  247. * @authenticated
  248. *
  249. * @queryParam type string required 邀请码类型(user:普通用户/coach:技师) Example: user
  250. *
  251. * @response 200 {
  252. * "code": 200,
  253. * "message": "生成成功",
  254. * "data": {
  255. * "invite_code": "user_1",
  256. * "invite_url": "https://example.com/invite?invite_id=1&invite_role=user&invite_code=user_1",
  257. * "qr_code": "data:image/svg+xml;base64,..."
  258. * }
  259. * }
  260. * @response 401 {
  261. * "code": 401,
  262. * "message": "请先登录",
  263. * "data": null
  264. * }
  265. * @response 422 {
  266. * "code": 422,
  267. * "message": "无法生成邀请码,用户类型不匹配",
  268. * "data": null
  269. * }
  270. */
  271. public function generateInviteCode()
  272. {
  273. $result = $this->service->generateInviteCode();
  274. return $this->success($result, '生成成功');
  275. }
  276. /**
  277. * [用户]查看技师申请记录
  278. *
  279. * @description 获取当前用户的技师申请记录,如果用户不是技师或没有申请记录则返回 null
  280. *
  281. * @authenticated
  282. *
  283. * @response 200 {
  284. * "code": 200,
  285. * "message": "获取成功",
  286. * "data": {
  287. * "id": 1,
  288. * "coach_id": 100,
  289. * "age": 25,
  290. * "mobile": "13800138000",
  291. * "gender": 1,
  292. * "work_years": 5,
  293. * "intention_city": "杭州",
  294. * "portrait_images": ["https://example.com/portrait1.jpg"],
  295. * "introduction": "专业按摩师,有多年经验",
  296. * "state": 1,
  297. * "state_text": "待审核",
  298. * "created_at": "2024-03-20 10:00:00",
  299. * "updated_at": "2024-03-20 10:00:00"
  300. * }
  301. * }
  302. * @response 200 {
  303. * "code": 200,
  304. * "message": "您还不是技师",
  305. * "data": null
  306. * }
  307. * @response 200 {
  308. * "code": 200,
  309. * "message": "暂无申请记录",
  310. * "data": null
  311. * }
  312. * @response 401 {
  313. * "code": 401,
  314. * "message": "请先登录",
  315. * "data": null
  316. * }
  317. */
  318. public function getCoachApplication()
  319. {
  320. $result = $this->service->getCoachApplication();
  321. // 如果用户不是技师
  322. if ($result === null && ! Auth::user()->coach) {
  323. return $this->success(null, '您还不是技师');
  324. }
  325. // 如果是技师但没有申请记录
  326. if ($result === null) {
  327. return $this->success(null, '暂无申请记录');
  328. }
  329. return $this->success($result, '获取成功');
  330. }
  331. }