UserService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Models\CoachApplication;
  4. use App\Models\Feedback;
  5. use Illuminate\Support\Facades\Auth;
  6. use Illuminate\Support\Facades\Log;
  7. use SimpleSoftwareIO\QrCode\Facades\QrCode;
  8. class UserService
  9. {
  10. /**
  11. * 获取当前用户信息
  12. *
  13. * @return \Illuminate\Http\JsonResponse
  14. */
  15. public function getUserInfo()
  16. {
  17. try {
  18. // 获取当前登录用户
  19. $user = Auth::user();
  20. return response()->json([
  21. 'code' => 200,
  22. 'message' => '获取成功',
  23. 'data' => $user,
  24. ]);
  25. } catch (\Exception $e) {
  26. Log::error('获取用户信息失败: '.$e->getMessage());
  27. throw $e;
  28. }
  29. }
  30. /**
  31. * 更新当前用户信息
  32. *
  33. * @return \Illuminate\Http\JsonResponse
  34. */
  35. public function updateUserInfo(array $data)
  36. {
  37. try {
  38. // 更新用户信息
  39. $user = Auth::user();
  40. $user->update($data);
  41. return response()->json([
  42. 'code' => 200,
  43. 'message' => '修改成功',
  44. 'data' => null,
  45. ]);
  46. } catch (\Exception $e) {
  47. Log::error('更新用户信息失败: '.$e->getMessage());
  48. throw $e;
  49. }
  50. }
  51. /**
  52. * 提交用户反馈
  53. *
  54. * @return \Illuminate\Http\JsonResponse
  55. */
  56. public function feedback(string $content)
  57. {
  58. try {
  59. // 保存用户反馈
  60. // Feedback::create([
  61. // 'user_id' => Auth::id(),
  62. // 'content' => $content,
  63. // ]);
  64. // return response()->json([
  65. // 'code' => 200,
  66. // 'message' => '提交成功',
  67. // 'data' => null,
  68. // ]);
  69. } catch (\Exception $e) {
  70. Log::error('提交反馈失败: '.$e->getMessage());
  71. throw $e;
  72. }
  73. }
  74. /**
  75. * 申请成为技师
  76. *
  77. * @return \Illuminate\Http\JsonResponse
  78. */
  79. public function applyCoach(string $mobile, string $gender, string $work_years, string $intention_city)
  80. {
  81. try {
  82. // 创建技师申请记录
  83. // CoachApplication::create([
  84. // 'user_id' => Auth::id(),
  85. // 'mobile' => $mobile,
  86. // 'gender' => $gender,
  87. // 'work_years' => $work_years,
  88. // 'intention_city' => $intention_city,
  89. // ]);
  90. return response()->json([
  91. 'code' => 200,
  92. 'message' => '申请成功',
  93. 'data' => null,
  94. ]);
  95. } catch (\Exception $e) {
  96. Log::error('申请成为技师失败: '.$e->getMessage());
  97. throw $e;
  98. }
  99. }
  100. /**
  101. * 生成用户邀请码
  102. *
  103. * @return \Illuminate\Http\JsonResponse
  104. */
  105. public function generateInviteCode()
  106. {
  107. try {
  108. // 获取当前用户
  109. $user = Auth::user();
  110. // 生成邀请码
  111. $inviteCode = strtoupper(substr(md5($user->id), 0, 6));
  112. // 生成带参数的网页链接
  113. $qrContent = config('app.url').'/invite?'.http_build_query([
  114. 'user_id' => $user->id,
  115. 'role' => 'user',
  116. 'code' => $inviteCode,
  117. ]);
  118. // 使用QrCode库生成SVG格式的二维码
  119. $qrImage = QrCode::format('svg')
  120. ->size(200)
  121. ->margin(2)
  122. ->encoding('UTF-8')
  123. ->generate($qrContent);
  124. // 将SVG转为base64
  125. $qrBase64 = base64_encode($qrImage);
  126. // 记录生成日志
  127. Log::info('用户生成邀请码:', [
  128. 'user_id' => $user->id,
  129. 'invite_code' => $inviteCode,
  130. 'invite_url' => $qrContent, // 记录生成的邀请链接
  131. ]);
  132. return response()->json([
  133. 'code' => 200,
  134. 'message' => '生成成功',
  135. 'data' => [
  136. 'invite_code' => $inviteCode,
  137. 'invite_url' => $qrContent, // 返回邀请链接
  138. 'qr_code' => 'data:image/svg+xml;base64,'.$qrBase64,
  139. ],
  140. ]);
  141. } catch (\Exception $e) {
  142. Log::error('生成邀请码失败: '.$e->getMessage());
  143. return response()->json([
  144. 'code' => 500,
  145. 'msg' => '生成二维码失败,请稍后再试。',
  146. ]);
  147. }
  148. }
  149. }