123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- namespace App\Services\Client;
- use App\Models\CoachApplication;
- use App\Models\Feedback;
- use App\Models\MemberUser;
- use App\Models\User;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use SimpleSoftwareIO\QrCode\Facades\QrCode;
- class UserService
- {
- protected $marketDistTeamService;
- public function __construct(MarketDistTeamService $marketDistTeamService)
- {
- $this->marketDistTeamService = $marketDistTeamService;
- }
- /**
- * 获取当前用户信息
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function getUserInfo()
- {
- try {
- // 获取当前登录用户
- $user = Auth::user();
- return response()->json([
- 'code' => 200,
- 'message' => '获取成功',
- 'data' => $user,
- ]);
- } catch (\Exception $e) {
- Log::error('获取用户信息失败: '.$e->getMessage());
- throw $e;
- }
- }
- /**
- * 用户注册
- *
- * @param string $mobile 手机号
- * @param string $code 验证码
- * @param string|null $invite_code 邀请码(选填)
- * @param int|null $invite_id 邀请人ID(选填)
- * @param string|null $invite_role 邀请人角色(选填)
- * @return \Illuminate\Http\JsonResponse
- */
- public function register(string $mobile, string $code, ?string $invite_code = null, ?int $invite_id = null, ?string $invite_role = null)
- {
- try {
- // 开启事务
- DB::beginTransaction();
- // 检查手机号是否已注册
- if (MemberUser::where('mobile', $mobile)->exists()) {
- return response()->json([
- 'code' => 422,
- 'message' => '该手机号已注册',
- 'data' => null,
- ]);
- }
- // 验证手机验证码
- if (! $this->verifySmsCode($mobile, $code)) {
- return response()->json([
- 'code' => 422,
- 'message' => '验证码错误或已过期',
- 'data' => null,
- ]);
- }
- // 创建用户数据
- $userData = [
- 'mobile' => $mobile,
- 'password' => bcrypt(substr($mobile, -6)), // 默认密码为手机号后6位
- 'nickname' => substr_replace($mobile, '****', 3, 4), // 默认昵称为手机号(中间4位隐藏)
- ];
- // 创建用户
- $user = MemberUser::create($userData);
- // 处理邀请关系
- if ($invite_code && $invite_role && $invite_id) {
- $this->marketDistTeamService->createInviteRelation(
- $user,
- $invite_code,
- $invite_id,
- $invite_role
- );
- }
- DB::commit();
- return response()->json([
- 'code' => 200,
- 'message' => '注册成功',
- 'data' => [
- 'user_id' => $user->id,
- 'mobile' => $mobile,
- 'invite_code' => $user->invite_code,
- ],
- ]);
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('用户注册失败: '.$e->getMessage());
- throw $e;
- }
- }
- /**
- * 验证短信验证码
- */
- private function verifySmsCode(string $mobile, string $code): bool
- {
- try {
- // TODO: 实现验证码验证逻辑
- // 可以通过Redis验证,示例:
- // $cacheKey = "sms_code:{$mobile}";
- // $cacheCode = Redis::get($cacheKey);
- // if (!$cacheCode || $cacheCode !== $code) {
- // return false;
- // }
- // Redis::del($cacheKey); // 验证成功后删除验证码
- return true;
- } catch (\Exception $e) {
- Log::error('验证码验证失败: '.$e->getMessage());
- return false;
- }
- }
- /**
- * 更新当前用户信息
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function updateUserInfo(array $data)
- {
- try {
- // 更新用户信息
- $user = Auth::user();
- $user->update($data);
- return response()->json([
- 'code' => 200,
- 'message' => '修改成功',
- 'data' => null,
- ]);
- } catch (\Exception $e) {
- Log::error('更新用户信息失败: '.$e->getMessage());
- throw $e;
- }
- }
- /**
- * 提交用户反馈
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function feedback(string $content)
- {
- try {
- // 保存用户反馈
- // Feedback::create([
- // 'user_id' => Auth::id(),
- // 'content' => $content,
- // ]);
- // return response()->json([
- // 'code' => 200,
- // 'message' => '提交成功',
- // 'data' => null,
- // ]);
- } catch (\Exception $e) {
- Log::error('提交反馈失败: '.$e->getMessage());
- throw $e;
- }
- }
- /**
- * 申请成为技师
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function applyCoach(string $mobile, string $gender, string $work_years, string $intention_city)
- {
- try {
- // 创建技师申请记录
- // CoachApplication::create([
- // 'user_id' => Auth::id(),
- // 'mobile' => $mobile,
- // 'gender' => $gender,
- // 'work_years' => $work_years,
- // 'intention_city' => $intention_city,
- // ]);
- return response()->json([
- 'code' => 200,
- 'message' => '申请成功',
- 'data' => null,
- ]);
- } catch (\Exception $e) {
- Log::error('申请成为技师失败: '.$e->getMessage());
- throw $e;
- }
- }
- /**
- * 生成用户邀请码
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function generateInviteCode()
- {
- try {
- // 获取当前用户
- $user = Auth::user();
- // 生成邀请码
- $inviteCode = strtoupper(substr(md5($user->id), 0, 6));
- // 生成带参数的网页链接
- $qrContent = config('app.url').'/invite?'.http_build_query([
- 'invite_id' => $user->id,
- 'invite_role' => 'user',
- 'invite_code' => $inviteCode,
- ]);
- // 使用QrCode库生成SVG格式的二维码
- $qrImage = QrCode::format('svg')
- ->size(200)
- ->margin(2)
- ->encoding('UTF-8')
- ->generate($qrContent);
- // 将SVG转为base64
- $qrBase64 = base64_encode($qrImage);
- // 记录生成日志
- Log::info('用户生成邀请码:', [
- 'invite_id' => $user->id,
- 'invite_role' => 'user',
- 'invite_code' => $inviteCode,
- 'invite_url' => $qrContent, // 记录生成的邀请链接
- ]);
- return response()->json([
- 'code' => 200,
- 'message' => '生成成功',
- 'data' => [
- 'invite_code' => $inviteCode,
- 'invite_url' => $qrContent, // 返回邀请链接
- 'qr_code' => 'data:image/svg+xml;base64,'.$qrBase64,
- ],
- ]);
- } catch (\Exception $e) {
- Log::error('生成邀请码失败: '.$e->getMessage());
- return response()->json([
- 'code' => 500,
- 'msg' => '生成二维码失败,请稍后再试。',
- ]);
- }
- }
- }
|