diff --git a/src/TelegramPanel.Web/Components/Pages/AccountLogin.razor b/src/TelegramPanel.Web/Components/Pages/AccountLogin.razor index 5eba8ab..f3ce6e6 100644 --- a/src/TelegramPanel.Web/Components/Pages/AccountLogin.razor +++ b/src/TelegramPanel.Web/Components/Pages/AccountLogin.razor @@ -12,15 +12,18 @@ - + - - - + + @StepTitle + + + @if (currentStep == LoginStep.Phone) + { @if (!IsApiConfigured) { - - 未配置全局 Telegram API(ApiId/ApiHash),无法登录。 + + 未配置全局 Telegram API(ApiId/ApiHash),无法登录。 去系统设置配置 @@ -28,102 +31,89 @@ } else { - - 使用全局 Telegram API:ApiId=@(Configuration["Telegram:ApiId"] ?? "(未配置)") + + 使用全局 Telegram API:ApiId=@(Configuration["Telegram:ApiId"] ?? "(未配置)") } + - + Placeholder="+86xxxxxxxxxx" Class="mt-2" /> + * 手机号需包含国家代码,例如:+8613800138000 - - - - + } + else if (currentStep == LoginStep.Code) + { + 验证码已发送到 @phoneNumber - + Placeholder="12345" Class="mt-2" /> + 说明:验证码通常优先发送到已登录设备的 Telegram「系统通知(777000)」;没有可用设备时才可能发送短信/电话。 - @if (accountId == 0) - { - - 当前并未成功发送验证码(可能是页面刚打开尚未连接、或你跳过了“发送验证码”步骤)。请回到上一步点击“发送验证码”。 - - } - 重新发送验证码(尝试切换通道) - - - - + } + else if (currentStep == LoginStep.Password) + { + 此账号启用了两步验证,请输入密码 - - - - - - - 登录成功并保存到数据库! - 手机号:@phoneNumber + + } + else if (currentStep == LoginStep.Done) + { + + 登录成功并保存到数据库! + + 手机号:@phoneNumber 用户名:@(username ?? "无") 用户ID:@userId - + 查看账号列表 - - - - - - - 重置 - - - - @if ((stepper?.ActiveIndex ?? 0) > 0 && !IsFinished) - { - - 上一步 - - } - @if (!IsFinished) - { - - @if (logging) - { - - 处理中... - } - else - { - @NextButtonText - } - - } - - - - + } + + + + 重置 + + + @if (currentStep != LoginStep.Phone && currentStep != LoginStep.Done) + { + + 上一步 + + } + @if (currentStep != LoginStep.Done) + { + + @if (logging) + { + + 处理中... + } + else + { + @PrimaryButtonText + } + + } + @code { - private MudStepper? stepper; - private bool stepperReady; private bool logging = false; private int telegramApiId; @@ -136,6 +126,7 @@ private string? username = ""; private long userId = 0; private int accountId = 0; + private LoginStep currentStep = LoginStep.Phone; private bool TryGetTelegramApi(out int apiId, out string apiHash, out string configuredSessionsPath) { @@ -148,59 +139,53 @@ private bool IsApiConfigured => TryGetTelegramApi(out _, out _, out _); - private bool IsFinished => - stepper != null && stepper.ActiveIndex >= stepper.Steps.Count - 1; - - private bool IsLoginActionStep => - stepper != null && stepper.ActiveIndex == stepper.Steps.Count - 2; - - protected override Task OnAfterRenderAsync(bool firstRender) + private string StepTitle => currentStep switch { - // @ref 在首次渲染后才会赋值;需要触发一次刷新,否则按钮会一直保持“Disabled”,看起来像“点了没反应” - if (firstRender && !stepperReady && stepper != null) - { - stepperReady = true; - return InvokeAsync(StateHasChanged); - } + LoginStep.Phone => "步骤 1/3:输入手机号并发送验证码", + LoginStep.Code => "步骤 2/3:输入验证码", + LoginStep.Password => "步骤 3/3:输入两步验证密码", + LoginStep.Done => "完成", + _ => "手机号登录" + }; - return Task.CompletedTask; - } - - private string NextButtonText => stepper?.ActiveIndex switch + private string PrimaryButtonText => currentStep switch { - 0 => "发送验证码", - 1 => "验证验证码", - 2 => "验证密码", + LoginStep.Phone => "发送验证码", + LoginStep.Code => "验证验证码", + LoginStep.Password => "验证密码", _ => "下一步" }; - private async Task ResetStepperAsync() + private Task ResetAsync() { - if (stepper == null) return; accountId = 0; + phoneNumber = ""; verificationCode = ""; password = ""; - await stepper.ResetAsync(true); + username = ""; + userId = 0; + currentStep = LoginStep.Phone; + return Task.CompletedTask; } - private async Task PreviousStepAsync() + private Task PreviousAsync() { - if (stepper == null) return; - await stepper.PreviousStepAsync(); + currentStep = currentStep switch + { + LoginStep.Code => LoginStep.Phone, + LoginStep.Password => LoginStep.Code, + _ => currentStep + }; + return Task.CompletedTask; } - private async Task NextStep() + private async Task NextAsync() { - if (stepper == null) return; - - var currentStep = stepper.ActiveIndex; logging = true; - try { - if (currentStep == 0) + if (currentStep == LoginStep.Phone) { - // 步骤1:检查全局配置并发送验证码 if (!TryGetTelegramApi(out telegramApiId, out telegramApiHash, out sessionsPath)) { Snackbar.Add("请先在【系统设置】中配置全局 Telegram API(ApiId/ApiHash)", Severity.Error); @@ -209,39 +194,46 @@ if (string.IsNullOrWhiteSpace(phoneNumber)) { - Snackbar.Add("请填写完整信息", Severity.Error); + Snackbar.Add("请输入手机号(包含国家代码)", Severity.Error); return; } - // 生成临时账号ID(用于跟踪登录过程) accountId = Random.Shared.Next(1, int.MaxValue); - var result = await AccountService.StartLoginAsync(accountId, phoneNumber); + if (result.Success) + { + await SaveAccount(result.Account!); + return; + } + if (result.NextStep == "code") { Snackbar.Add($"验证码已发送到 {phoneNumber}", Severity.Info); - await stepper.NextStepAsync(); + currentStep = LoginStep.Code; + return; } - else if (result.NextStep == "signup") + + if (result.NextStep == "password") { - Snackbar.Add("此号码未注册Telegram,请先注册", Severity.Error); - } - else if (result.NextStep == "password") - { - // 极少数情况下可能直接进入 2FA(例如会话已可恢复) Snackbar.Add("需要两步验证密码", Severity.Info); - await stepper.NextStepAsync(); - await stepper.NextStepAsync(); - } - else - { - Snackbar.Add(result.Message ?? "发送验证码失败", Severity.Error); + currentStep = LoginStep.Password; + return; } + + Snackbar.Add(result.Message ?? "发送验证码失败", Severity.Error); + return; } - else if (currentStep == 1) + + if (currentStep == LoginStep.Code) { - // 步骤2:验证验证码 + if (accountId == 0) + { + Snackbar.Add("请先发送验证码", Severity.Warning); + currentStep = LoginStep.Phone; + return; + } + if (string.IsNullOrWhiteSpace(verificationCode)) { Snackbar.Add("请输入验证码", Severity.Error); @@ -249,41 +241,47 @@ } var result = await AccountService.SubmitCodeAsync(accountId, verificationCode); - if (result.Success) { - // 登录成功,保存到数据库 await SaveAccount(result.Account!); + return; } - else if (result.NextStep == "password") + + if (result.NextStep == "password") { Snackbar.Add("需要两步验证密码", Severity.Info); - await stepper.NextStepAsync(); - } - else - { - Snackbar.Add(result.Message ?? "验证码错误", Severity.Error); + currentStep = LoginStep.Password; + return; } + + Snackbar.Add(result.Message ?? "验证码错误或已过期", Severity.Error); + return; } - else if (currentStep == 2) + + if (currentStep == LoginStep.Password) { - // 步骤3:验证密码 + if (accountId == 0) + { + Snackbar.Add("请先发送验证码", Severity.Warning); + currentStep = LoginStep.Phone; + return; + } + if (string.IsNullOrWhiteSpace(password)) { - Snackbar.Add("请输入密码", Severity.Error); + Snackbar.Add("请输入两步验证密码", Severity.Error); return; } var result = await AccountService.SubmitPasswordAsync(accountId, password); - if (result.Success) { await SaveAccount(result.Account!); + return; } - else - { - Snackbar.Add(result.Message ?? "密码错误", Severity.Error); - } + + Snackbar.Add(result.Message ?? "密码错误", Severity.Error); + return; } } catch (Exception ex) @@ -321,6 +319,14 @@ } } + private enum LoginStep + { + Phone = 0, + Code = 1, + Password = 2, + Done = 3 + } + private async Task SaveAccount(TelegramPanel.Core.Models.AccountInfo accountInfo) { try @@ -367,10 +373,7 @@ userId = accountInfo.TelegramUserId; // 跳转到完成步骤 - if (stepper != null) - { - await stepper.NextStepAsync(); - } + currentStep = LoginStep.Done; } catch (Exception ex) {