|
@@ -4,12 +4,22 @@ 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;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取当前用户信息
|
|
|
*
|
|
@@ -32,6 +42,101 @@ class UserService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 用户注册
|
|
|
+ *
|
|
|
+ * @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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 更新当前用户信息
|
|
|
*
|
|
@@ -124,9 +229,9 @@ class UserService
|
|
|
|
|
|
// 生成带参数的网页链接
|
|
|
$qrContent = config('app.url').'/invite?'.http_build_query([
|
|
|
- 'user_id' => $user->id,
|
|
|
- 'role' => 'user',
|
|
|
- 'code' => $inviteCode,
|
|
|
+ 'invite_id' => $user->id,
|
|
|
+ 'invite_role' => 'user',
|
|
|
+ 'invite_code' => $inviteCode,
|
|
|
]);
|
|
|
|
|
|
// 使用QrCode库生成SVG格式的二维码
|
|
@@ -141,7 +246,8 @@ class UserService
|
|
|
|
|
|
// 记录生成日志
|
|
|
Log::info('用户生成邀请码:', [
|
|
|
- 'user_id' => $user->id,
|
|
|
+ 'invite_id' => $user->id,
|
|
|
+ 'invite_role' => 'user',
|
|
|
'invite_code' => $inviteCode,
|
|
|
'invite_url' => $qrContent, // 记录生成的邀请链接
|
|
|
]);
|