first(); if (!$user) { return [true, true]; // 成功但用户不存在,保护用户隐私 } $code = Helper::guid(); $key = CacheKey::get('TEMP_TOKEN', $code); Cache::put($key, $user->id, 300); Cache::put(CacheKey::get('LAST_SEND_LOGIN_WITH_MAIL_LINK_TIMESTAMP', $email), time(), 60); $redirectUrl = '/#/login?verify=' . $code . '&redirect=' . ($redirect ? $redirect : 'dashboard'); if (admin_setting('app_url')) { $link = admin_setting('app_url') . $redirectUrl; } else { $link = url($redirectUrl); } $this->sendMailLinkEmail($user, $link); return [true, $link]; } /** * 发送邮件链接登录邮件 * * @param User $user 用户对象 * @param string $link 登录链接 * @return void */ private function sendMailLinkEmail(User $user, string $link): void { SendEmailJob::dispatch([ 'email' => $user->email, 'subject' => __('Login to :name', [ 'name' => admin_setting('app_name', 'XBoard') ]), 'template_name' => 'login', 'template_value' => [ 'name' => admin_setting('app_name', 'XBoard'), 'link' => $link, 'url' => admin_setting('app_url') ] ]); } /** * 获取快速登录URL * * @param User $user 用户对象 * @param string|null $redirect 重定向地址 * @return string 登录URL */ public function getQuickLoginUrl(User $user, ?string $redirect = null): string { $code = Helper::guid(); $key = CacheKey::get('TEMP_TOKEN', $code); Cache::put($key, $user->id, 60); $redirectUrl = '/#/login?verify=' . $code . '&redirect=' . ($redirect ? $redirect : 'dashboard'); if (admin_setting('app_url')) { return admin_setting('app_url') . $redirectUrl; } else { return url($redirectUrl); } } /** * 处理Token登录 * * @param string $token 登录令牌 * @return int|null 用户ID或null */ public function handleTokenLogin(string $token): ?int { $key = CacheKey::get('TEMP_TOKEN', $token); $userId = Cache::get($key); if (!$userId) { return null; } $user = User::find($userId); if (!$user || $user->banned) { return null; } Cache::forget($key); return $userId; } }