initializeMobile($request); if ($response !== null) { return $response; } $account = trim((string) $request->post('account', '')); $accountType = trim((string) $request->post('account_type', '')); $password = (string) $request->post('password', ''); $inviteCode = trim((string) $request->post('invite_code', '')); if ($account === '' || $accountType === '' || $password === '') { return $this->mobileError(1001, 'Missing parameters'); } if ($accountType !== 'phone' && $accountType !== 'email') { return $this->mobileError(1003, 'Invalid parameter value'); } $username = $account; $mobile = ''; $email = ''; if ($accountType === 'phone') { $mobile = $account; } if ($accountType === 'email') { $email = $account; } $extend = []; if ($inviteCode !== '') { $inviterAdmin = Db::name('admin')->field(['id', 'channel_id'])->where('invite_code', $inviteCode)->find(); if (!$inviterAdmin) { return $this->mobileError(2002, 'Invite code does not exist'); } $extend['register_invite_code'] = $inviteCode; $extend['admin_id'] = $inviterAdmin['id']; $extend['channel_id'] = $inviterAdmin['channel_id'] ?? null; } $registered = $this->auth->register($username, $password, $mobile, $email, 1, $extend); if (!$registered) { return $this->mobileError(2000, (string) $this->auth->getError()); } $loggedIn = $this->auth->login($username, $password, true); if (!$loggedIn) { return $this->mobileError(2000, 'Registered successfully but login failed'); } $userInfo = $this->auth->getUserInfo(); return $this->mobileSuccess([ 'user_id' => $userInfo['id'] ?? null, 'access_token' => $userInfo['token'] ?? '', 'expires_in' => config('buildadmin.user_token_keep_time', 259200), 'profile' => [ 'username' => $userInfo['username'] ?? '', 'coin' => $userInfo['coin'] ?? '0.0000', 'channel_id' => $userInfo['channel_id'] ?? null, ], ]); } public function userLogin(Request $request): Response { $response = $this->initializeMobile($request); if ($response !== null) { return $response; } $account = trim((string) $request->post('account', '')); $password = (string) $request->post('password', ''); if ($account === '' || $password === '') { return $this->mobileError(1001, 'Missing parameters'); } $ok = $this->auth->login($account, $password, true); if (!$ok) { return $this->mobileError(1101, 'Incorrect account or password'); } $userInfo = $this->auth->getUserInfo(); return $this->mobileSuccess([ 'access_token' => $userInfo['token'] ?? '', 'refresh_token' => $userInfo['refresh_token'] ?? '', 'expires_in' => config('buildadmin.user_token_keep_time', 259200), 'user' => [ 'id' => $userInfo['id'] ?? null, 'username' => $userInfo['username'] ?? '', 'coin' => $userInfo['coin'] ?? '0.0000', 'risk_flags' => $userInfo['risk_flags'] ?? 0, ], ]); } public function tokenRefresh(Request $request): Response { $response = $this->initializeMobile($request); if ($response !== null) { return $response; } $refreshToken = trim((string) $request->post('refresh_token', '')); if ($refreshToken === '') { return $this->mobileError(1001, 'Missing parameters'); } $tokenData = Token::get($refreshToken); if (!$tokenData || $tokenData['type'] !== UserAuth::TOKEN_TYPE . '-refresh' || $tokenData['expire_time'] < time()) { return $this->mobileError(1101, 'Login status has expired'); } $newToken = Random::uuid(); Token::set($newToken, UserAuth::TOKEN_TYPE, $tokenData['user_id'], config('buildadmin.user_token_keep_time', 259200)); return $this->mobileSuccess([ 'access_token' => $newToken, 'expires_in' => config('buildadmin.user_token_keep_time', 259200), ]); } }