UserController.php 10 KB

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