p1(语言泄漏):后端诊断串不再上屏
- presenter 新增 LoginFailure 原因码(denied / whitelist_pending / unreachable /
not_authlib_server / bad_credentials / no_profile / store_failed / other)
- 页面按码查 i18n 出标题+正文+「Code: …」,原文只进日志/详细信息
- 进行中(Requesting)也走 i18n,不再复读后端中文状态串
- presenter/state 不再发中文 toast(成败由页面按 i18n 画)
- 新增守门测试:注入带 AADSTS 的失败事件,断言英文视图无 CJK 且含 code
p2-1 复制:on_account 返回 Task,page_messages 把 iced::clipboard::write 交回 runtime
p2-2 可见失败态:新增 AccountNotice(added/removed/switched/名字非法/重名/落盘失败/
找不到),页面用 M3 内联 alert 画(不是 toast);离线名字先按 3–16 位规则校验,
失败不再清空输入
p2-3 重新登录按账户类型分派:微软→授权码流;第三方→预填服务器/登录名并提示补密码;
离线→明确说明没有可重登的服务器账户
p2-4 入口收敛:删 chip 行;空态=三卡片;有账号时三卡收进 Add an account(按钮不再绑死微软轨)
看图意见/tower 三条:
- 失败横幅改成 M3 alert(SurfaceContainerHigh + 描边,errorContainer 只作图标块,
不再通栏实心红);色值本身经复审像素采样确认是 90/30 档,未改角色
- 当前账户卡补齐与列表一致的「服务器/登录名」行;无皮肤时明说 No skin available
- 页面级 scrollable 加 8px 右 gutter,滚动条不再压卡片
- 倒计时改 mm:ss(15:00 left),数字与冒号语言无关
p3(顺带):AuthlibRequest / Form 手写 Debug 脱敏密码;HTTP 响应体与 base64 输出加上限;
离线提示文案与行为对齐;删除无人使用的 key;皮肤测试断言帽子层真的 blend
测试:app-iced lib 418 绿、presenter 151 / i18n 24 绿(workspace 全量另跑)
217 lines
9.0 KiB
Rust
217 lines
9.0 KiB
Rust
//! 账户页消息的落点(M114):页面只发意图,这里翻成 `AppCommand` 或本地的表单状态写入。
|
||
//!
|
||
//! 三条纪律(与 `update.rs` 同源):
|
||
//!
|
||
//! 1. **不做乐观回显**:切账户/删账户/登录都只派命令,等 `AccountsChanged` /
|
||
//! `DeviceLoginChanged` / `AuthlibLoginChanged` 回来才改列表;
|
||
//! 2. **密码只走一次**:`Submit` 把 `AuthlibRequest` 交给后端后立刻清掉表单里的密码
|
||
//! (成功失败都清——重试用新输入,不在内存里多留一份凭据);
|
||
//! 3. **表单态是纯 UI 状态**:输入框改动只写 `App::account_form` + 置脏,不派命令。
|
||
//!
|
||
//! ## 返回 `Task`(M114 r2 / 复审 P2-1)
|
||
//!
|
||
//! 复制用户码/链接必须把 `iced::clipboard::write` 的 `Task` **交回 runtime**——iced 0.14 的
|
||
//! `Task` 是惰性 stream,丢掉返回值等于永不执行(日志页的 Copy summary 就是正确写法)。
|
||
|
||
use iced::Task;
|
||
use unlucky_presenter::dto::{AccountKind, AppCommand, AuthlibRequest};
|
||
|
||
use crate::app::Message;
|
||
use crate::pages::account::{Hint, Message as Account, Panel, Track};
|
||
|
||
use super::super::state::App;
|
||
|
||
impl App {
|
||
/// 账户页消息。
|
||
pub(super) fn on_account(&mut self, message: Account) -> Task<Message> {
|
||
// 复制是唯一需要**交回 Task** 的分支:先把它的 Task 抓出来,其余分支照旧改状态
|
||
let clipboard = match &message {
|
||
Account::Copy(text) => Some(text.clone()),
|
||
_ => None,
|
||
};
|
||
|
||
match message {
|
||
Account::Open(track) => {
|
||
self.account_form.open(track);
|
||
self.account_form.hint = None;
|
||
self.view_dirty = true;
|
||
}
|
||
Account::ToggleAdd => {
|
||
self.account_form.section_open = !self.account_form.section_open;
|
||
self.account_form.hint = None;
|
||
self.view_dirty = true;
|
||
}
|
||
Account::Close => {
|
||
self.account_form.close();
|
||
// 关面板同时收掉登录任务(避免关掉面板后后台还在轮询、也不会凭空多一个账户)
|
||
if self.state.device_login.phase.busy() || self.state.authlib_login.phase.busy() {
|
||
self.dispatch(AppCommand::CancelDeviceLogin);
|
||
}
|
||
self.view_dirty = true;
|
||
}
|
||
Account::DismissNotice => self.dispatch(AppCommand::DismissAccountNotice),
|
||
Account::Server(value) => {
|
||
self.account_form.server = value;
|
||
self.view_dirty = true;
|
||
}
|
||
Account::Login(value) => {
|
||
self.account_form.login = value;
|
||
self.view_dirty = true;
|
||
}
|
||
Account::Password(value) => {
|
||
self.account_form.password = value;
|
||
self.view_dirty = true;
|
||
}
|
||
Account::OfflineName(value) => {
|
||
self.account_form.offline_name = value;
|
||
self.view_dirty = true;
|
||
}
|
||
Account::Probe => {
|
||
let server = self.account_form.server.clone();
|
||
if !server.trim().is_empty() {
|
||
self.dispatch(AppCommand::ProbeAuthlibServer(server));
|
||
}
|
||
}
|
||
Account::Submit => match self.account_form.panel {
|
||
Panel::Offline => {
|
||
// 名字**不清空**:失败时用户还要看见自己输入的(复审 r1 P2-2 的另一半)
|
||
let name = self.account_form.offline_name.trim().to_string();
|
||
if !name.is_empty() {
|
||
self.dispatch(AppCommand::AddOfflineAccount(name));
|
||
}
|
||
}
|
||
Panel::Authlib => {
|
||
if self.account_form.authlib_ready() {
|
||
let request = AuthlibRequest::new(
|
||
self.account_form.server.clone(),
|
||
self.account_form.login.clone(),
|
||
std::mem::take(&mut self.account_form.password),
|
||
);
|
||
self.dispatch(AppCommand::AddAuthlibAccount(request));
|
||
}
|
||
}
|
||
Panel::Microsoft => return self.on_account(Account::StartBrowser),
|
||
Panel::None => {}
|
||
},
|
||
Account::StartDevice => self.dispatch(AppCommand::StartDeviceLogin),
|
||
Account::StartBrowser => self.dispatch(AppCommand::StartBrowserLogin),
|
||
Account::Cancel => {
|
||
self.account_form.close();
|
||
self.dispatch(AppCommand::CancelDeviceLogin);
|
||
}
|
||
Account::Copy(_) => {}
|
||
Account::Switch(id) => self.dispatch(AppCommand::SwitchAccount(id)),
|
||
Account::Remove(id) => self.dispatch(AppCommand::RemoveAccount(id)),
|
||
Account::Relogin(id) => return self.relogin(&id),
|
||
}
|
||
self.refresh_view();
|
||
|
||
match clipboard {
|
||
Some(text) => iced::clipboard::write(text),
|
||
None => Task::none(),
|
||
}
|
||
}
|
||
|
||
/// 「重新登录」按**账户类型**分派(复审 r1 P2-3)。
|
||
///
|
||
/// - 微软轨:走授权码流(原路);
|
||
/// - 第三方轨:第三方站没有「一键重登」——预填服务器地址与登录名,让用户补密码;
|
||
/// - 本地档案:没有服务器账户可重登 ⇒ 明确说明,不假装能做。
|
||
fn relogin(&mut self, id: &str) -> Task<Message> {
|
||
let Some(account) = self
|
||
.state
|
||
.accounts
|
||
.iter()
|
||
.find(|account| account.id == id)
|
||
.cloned()
|
||
else {
|
||
return Task::none();
|
||
};
|
||
self.account_form.hint = None;
|
||
match account.kind {
|
||
AccountKind::Microsoft => {
|
||
self.account_form.panel = Panel::Microsoft;
|
||
self.dispatch(AppCommand::StartBrowserLogin);
|
||
}
|
||
AccountKind::Authlib => {
|
||
self.account_form.open(Track::Authlib);
|
||
self.account_form.server = account.server_url.clone();
|
||
self.account_form.login = account.login_name.clone();
|
||
self.account_form.password.clear();
|
||
self.account_form.hint = Some(Hint::AuthlibReauth);
|
||
self.view_dirty = true;
|
||
self.refresh_view();
|
||
}
|
||
AccountKind::Offline => {
|
||
self.account_form.hint = Some(Hint::OfflineNoRelogin);
|
||
self.view_dirty = true;
|
||
self.refresh_view();
|
||
}
|
||
}
|
||
Task::none()
|
||
}
|
||
}
|
||
|
||
/// 取证开关 `ULL_ACCOUNT_STATE` 的落地(**只在 `--screenshot` 下生效**)。
|
||
///
|
||
/// 为什么需要:设备码过期要等 15 分钟、表单要手点两下,截图通道等不到自然发生的状态。
|
||
pub(super) fn force_account_state(app: &mut App) {
|
||
if !app.has_screenshot() {
|
||
return;
|
||
}
|
||
let Ok(state) = std::env::var("ULL_ACCOUNT_STATE") else {
|
||
return;
|
||
};
|
||
match state.trim().to_lowercase().as_str() {
|
||
// 设备码已过期:起点往前挪一小时(倒计时自己算出来就是 0)
|
||
"expired" => {
|
||
app.state.device_login_started =
|
||
Some(std::time::Instant::now() - std::time::Duration::from_secs(3_600));
|
||
}
|
||
// 第三方站表单:直接把面板打开并预填地址
|
||
"authlib" | "authlib-form" => {
|
||
app.account_form.panel = crate::pages::account::Panel::Authlib;
|
||
if app.account_form.server.trim().is_empty() {
|
||
app.account_form.server = "https://littleskin.cn/api/yggdrasil/".to_string();
|
||
}
|
||
app.account_form.login = "steve@example.com".to_string();
|
||
app.account_form.password = "not-a-real-password".to_string();
|
||
}
|
||
// 有账号时「Add an account」展开态(P2-4 的收敛后形态)
|
||
"add" => {
|
||
app.account_form.section_open = true;
|
||
}
|
||
_ => {}
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use crate::pages::account::{Form, Panel, Track};
|
||
|
||
/// 表单态是纯 UI 状态:打开/关闭面板不派命令,但会把面板切对。
|
||
///
|
||
/// 抓得住:`Open` 忘了写 `panel`(点了轨道卡什么都不出现)、`close()` 不清密码。
|
||
/// 抓不住:真实 dispatch 的副作用(那要真后端)。
|
||
#[test]
|
||
fn the_form_state_follows_the_page_messages() {
|
||
let mut form = Form::new();
|
||
assert_eq!(form.panel, Panel::None);
|
||
form.open(Track::Authlib);
|
||
assert_eq!(form.panel, Panel::Authlib);
|
||
form.password = "hunter2".to_string();
|
||
form.close();
|
||
assert_eq!(form.panel, Panel::None);
|
||
assert!(form.password.is_empty(), "关闭面板必须把一次性密码清掉");
|
||
}
|
||
|
||
/// 三张轨道卡默认收起(有账号时),`ToggleAdd` 展开(复审 r1 P2-4)。
|
||
#[test]
|
||
fn the_add_section_starts_collapsed() {
|
||
let mut form = Form::new();
|
||
assert!(!form.section_open);
|
||
form.section_open = true;
|
||
assert!(form.section_open);
|
||
}
|
||
}
|