UserService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Models\CoachApplication;
  4. use App\Models\Feedback;
  5. use App\Models\MemberUser;
  6. use App\Models\User;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use SimpleSoftwareIO\QrCode\Facades\QrCode;
  11. class UserService
  12. {
  13. protected $marketDistTeamService;
  14. public function __construct(MarketDistTeamService $marketDistTeamService)
  15. {
  16. $this->marketDistTeamService = $marketDistTeamService;
  17. }
  18. /**
  19. * 获取当前用户信息
  20. *
  21. * @return \Illuminate\Http\JsonResponse
  22. */
  23. public function getUserInfo()
  24. {
  25. try {
  26. // 获取当前登录用户
  27. $user = Auth::user();
  28. return response()->json([
  29. 'code' => 200,
  30. 'message' => '获取成功',
  31. 'data' => $user,
  32. ]);
  33. } catch (\Exception $e) {
  34. Log::error('获取用户信息失败: '.$e->getMessage());
  35. throw $e;
  36. }
  37. }
  38. /**
  39. * 用户注册
  40. *
  41. * @param string $mobile 手机号
  42. * @param string $code 验证码
  43. * @param string|null $invite_code 邀请码(选填)
  44. * @param int|null $invite_id 邀请人ID(选填)
  45. * @param string|null $invite_role 邀请人角色(选填)
  46. * @return \Illuminate\Http\JsonResponse
  47. */
  48. public function register(string $mobile, string $code, ?string $invite_code = null, ?int $invite_id = null, ?string $invite_role = null)
  49. {
  50. try {
  51. // 开启事务
  52. DB::beginTransaction();
  53. // 检查手机号是否已注册
  54. if (MemberUser::where('mobile', $mobile)->exists()) {
  55. return response()->json([
  56. 'code' => 422,
  57. 'message' => '该手机号已注册',
  58. 'data' => null,
  59. ]);
  60. }
  61. // 验证手机验证码
  62. if (! $this->verifySmsCode($mobile, $code)) {
  63. return response()->json([
  64. 'code' => 422,
  65. 'message' => '验证码错误或已过期',
  66. 'data' => null,
  67. ]);
  68. }
  69. // 创建用户数据
  70. $userData = [
  71. 'mobile' => $mobile,
  72. 'password' => bcrypt(substr($mobile, -6)), // 默认密码为手机号后6位
  73. 'nickname' => substr_replace($mobile, '****', 3, 4), // 默认昵称为手机号(中间4位隐藏)
  74. ];
  75. // 创建用户
  76. $user = MemberUser::create($userData);
  77. // 处理邀请关系
  78. if ($invite_code && $invite_role && $invite_id) {
  79. $this->marketDistTeamService->createInviteRelation(
  80. $user,
  81. $invite_code,
  82. $invite_id,
  83. $invite_role
  84. );
  85. }
  86. DB::commit();
  87. return response()->json([
  88. 'code' => 200,
  89. 'message' => '注册成功',
  90. 'data' => [
  91. 'user_id' => $user->id,
  92. 'mobile' => $mobile,
  93. 'invite_code' => $user->invite_code,
  94. ],
  95. ]);
  96. } catch (\Exception $e) {
  97. DB::rollBack();
  98. Log::error('用户注册失败: '.$e->getMessage());
  99. throw $e;
  100. }
  101. }
  102. /**
  103. * 验证短信验证码
  104. */
  105. private function verifySmsCode(string $mobile, string $code): bool
  106. {
  107. try {
  108. // TODO: 实现验证码验证逻辑
  109. // 可以通过Redis验证,示例:
  110. // $cacheKey = "sms_code:{$mobile}";
  111. // $cacheCode = Redis::get($cacheKey);
  112. // if (!$cacheCode || $cacheCode !== $code) {
  113. // return false;
  114. // }
  115. // Redis::del($cacheKey); // 验证成功后删除验证码
  116. return true;
  117. } catch (\Exception $e) {
  118. Log::error('验证码验证失败: '.$e->getMessage());
  119. return false;
  120. }
  121. }
  122. /**
  123. * 更新当前用户信息
  124. *
  125. * @return \Illuminate\Http\JsonResponse
  126. */
  127. public function updateUserInfo(array $data)
  128. {
  129. try {
  130. // 更新用户信息
  131. $user = Auth::user();
  132. $user->update($data);
  133. return response()->json([
  134. 'code' => 200,
  135. 'message' => '修改成功',
  136. 'data' => null,
  137. ]);
  138. } catch (\Exception $e) {
  139. Log::error('更新用户信息失败: '.$e->getMessage());
  140. throw $e;
  141. }
  142. }
  143. /**
  144. * 提交用户反馈
  145. *
  146. * @return \Illuminate\Http\JsonResponse
  147. */
  148. public function feedback(string $content)
  149. {
  150. try {
  151. // 保存用户反馈
  152. // Feedback::create([
  153. // 'user_id' => Auth::id(),
  154. // 'content' => $content,
  155. // ]);
  156. // return response()->json([
  157. // 'code' => 200,
  158. // 'message' => '提交成功',
  159. // 'data' => null,
  160. // ]);
  161. } catch (\Exception $e) {
  162. Log::error('提交反馈失败: '.$e->getMessage());
  163. throw $e;
  164. }
  165. }
  166. /**
  167. * 申请成为技师
  168. *
  169. * @return \Illuminate\Http\JsonResponse
  170. */
  171. public function applyCoach(string $mobile, string $gender, string $work_years, string $intention_city)
  172. {
  173. try {
  174. // 创建技师申请记录
  175. // CoachApplication::create([
  176. // 'user_id' => Auth::id(),
  177. // 'mobile' => $mobile,
  178. // 'gender' => $gender,
  179. // 'work_years' => $work_years,
  180. // 'intention_city' => $intention_city,
  181. // ]);
  182. return response()->json([
  183. 'code' => 200,
  184. 'message' => '申请成功',
  185. 'data' => null,
  186. ]);
  187. } catch (\Exception $e) {
  188. Log::error('申请成为技师失败: '.$e->getMessage());
  189. throw $e;
  190. }
  191. }
  192. /**
  193. * 生成用户邀请码
  194. *
  195. * @return \Illuminate\Http\JsonResponse
  196. */
  197. public function generateInviteCode()
  198. {
  199. try {
  200. // 获取当前用户
  201. $user = Auth::user();
  202. // 生成邀请码
  203. $inviteCode = strtoupper(substr(md5($user->id), 0, 6));
  204. // 生成带参数的网页链接
  205. $qrContent = config('app.url').'/invite?'.http_build_query([
  206. 'invite_id' => $user->id,
  207. 'invite_role' => 'user',
  208. 'invite_code' => $inviteCode,
  209. ]);
  210. // 使用QrCode库生成SVG格式的二维码
  211. $qrImage = QrCode::format('svg')
  212. ->size(200)
  213. ->margin(2)
  214. ->encoding('UTF-8')
  215. ->generate($qrContent);
  216. // 将SVG转为base64
  217. $qrBase64 = base64_encode($qrImage);
  218. // 记录生成日志
  219. Log::info('用户生成邀请码:', [
  220. 'invite_id' => $user->id,
  221. 'invite_role' => 'user',
  222. 'invite_code' => $inviteCode,
  223. 'invite_url' => $qrContent, // 记录生成的邀请链接
  224. ]);
  225. return response()->json([
  226. 'code' => 200,
  227. 'message' => '生成成功',
  228. 'data' => [
  229. 'invite_code' => $inviteCode,
  230. 'invite_url' => $qrContent, // 返回邀请链接
  231. 'qr_code' => 'data:image/svg+xml;base64,'.$qrBase64,
  232. ],
  233. ]);
  234. } catch (\Exception $e) {
  235. Log::error('生成邀请码失败: '.$e->getMessage());
  236. return response()->json([
  237. 'code' => 500,
  238. 'msg' => '生成二维码失败,请稍后再试。',
  239. ]);
  240. }
  241. }
  242. }