From 199960276aa27eeded6ede4c88de25a417e5caf1 Mon Sep 17 00:00:00 2001 From: Nixevol Date: Wed, 18 Nov 2026 11:53:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=AE=A1=E7=90=86=E7=AB=AF=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=88=97=E8=A1=A8=E6=98=BE=E7=A4=BA=E5=A7=93=E5=90=8D?= =?UTF-8?q?=E5=92=8C=E5=85=AC=E5=8F=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/admin/users.py | 2 ++ .../alembic/versions/0012_account_profile.py | 21 ++++++++++++++ backend/euniv/models.py | 2 ++ backend/euniv/service.py | 29 +++++++++++++++++++ backend/tests/test_admin.py | 4 +++ backend/tests/test_api.py | 14 +++++++++ web/src/features/admin/UsersView.vue | 8 +++++ web/src/styles.css | 7 ++++- 8 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 backend/alembic/versions/0012_account_profile.py diff --git a/backend/admin/users.py b/backend/admin/users.py index 53c80dc..f087824 100644 --- a/backend/admin/users.py +++ b/backend/admin/users.py @@ -75,6 +75,8 @@ def list_users(db: Session) -> list[dict]: rows.append( { "employee_id": account.employee_id, + "user_name": account.user_name or "", + "company": account.company or "", "has_credential": bool(account.credential_cipher), "refresh_paused": bool(account.refresh_paused), "last_refresh_at": _iso_time(account.last_refresh_at), diff --git a/backend/alembic/versions/0012_account_profile.py b/backend/alembic/versions/0012_account_profile.py new file mode 100644 index 0000000..8fce5bc --- /dev/null +++ b/backend/alembic/versions/0012_account_profile.py @@ -0,0 +1,21 @@ +"""账号记下首页上的姓名和部门,供管理端用户列表显示。""" + +from alembic import op +import sqlalchemy as sa + +revision = "0012_account_profile" +down_revision = "0011_drop_proxy_list" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + with op.batch_alter_table("accounts") as batch: + batch.add_column(sa.Column("user_name", sa.String(length=64), server_default="", nullable=False)) + batch.add_column(sa.Column("company", sa.String(length=300), server_default="", nullable=False)) + + +def downgrade() -> None: + with op.batch_alter_table("accounts") as batch: + batch.drop_column("company") + batch.drop_column("user_name") diff --git a/backend/euniv/models.py b/backend/euniv/models.py index 3b475d1..860423d 100644 --- a/backend/euniv/models.py +++ b/backend/euniv/models.py @@ -56,6 +56,8 @@ class Account(Base): last_refresh_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) credential_valid: Mapped[bool | None] = mapped_column(Boolean, nullable=True) month_hours: Mapped[str] = mapped_column(String(32), nullable=False, default="", server_default=text("''")) + user_name: Mapped[str] = mapped_column(String(64), nullable=False, default="", server_default=text("''")) + company: Mapped[str] = mapped_column(String(300), nullable=False, default="", server_default=text("''")) hours_month: Mapped[str] = mapped_column(String(7), nullable=False, default="", server_default=text("''")) monthly_target: Mapped[int] = mapped_column(nullable=False, default=600, server_default="600") proxy_scheme: Mapped[str] = mapped_column(String(16), nullable=False, default="", server_default=text("''")) diff --git a/backend/euniv/service.py b/backend/euniv/service.py index a27fc54..76b664a 100644 --- a/backend/euniv/service.py +++ b/backend/euniv/service.py @@ -369,6 +369,7 @@ def poll_qr_login(db: Session, attempt_id: str) -> dict: def fetch_home_summary(db: Session, employee_id: str, client_factory=EunivClient) -> dict: data = _live(db, employee_id, client_factory, lambda client: _apply_hours_source(client, client.home_summary())) _remember_month_hours(db, employee_id, data) + _remember_profile(db, employee_id, data) return data @@ -470,6 +471,34 @@ def _remember_month_hours(db: Session, employee_id: str, payload: dict) -> None: db.commit() +def _profile_text(value: object, limit: int) -> str: + if not isinstance(value, str): + return "" + return value.strip()[:limit] + + +def _remember_profile(db: Session, employee_id: str, payload: dict) -> None: + """首页已经取到姓名和部门时,顺手写回用户列表。不记录职级。""" + if not isinstance(payload, dict): + return + name = _profile_text(payload.get("name"), 64) + company = _profile_text(payload.get("department"), 300) + if not name and not company: + return + account = db.get(Account, employee_id) + if account is None: + return + changed = False + if name and account.user_name != name: + account.user_name = name + changed = True + if company and account.company != company: + account.company = company + changed = True + if changed: + db.commit() + + def _hour_fields(data: dict) -> dict: fields = { "hours_source": data.get("hours_source") or "api", diff --git a/backend/tests/test_admin.py b/backend/tests/test_admin.py index 5f2459d..57cc87f 100644 --- a/backend/tests/test_admin.py +++ b/backend/tests/test_admin.py @@ -498,6 +498,8 @@ def test_check_skips_empty_credential_and_updates_saved_one(monkeypatch) -> None saved = next(item for item in body if item["employee_id"] == "saved-user") assert set(saved) == { "employee_id", + "user_name", + "company", "has_credential", "refresh_paused", "last_refresh_at", @@ -524,6 +526,8 @@ def test_check_skips_empty_credential_and_updates_saved_one(monkeypatch) -> None assert saved["refresh_paused"] is False assert saved["credential_valid"] is True assert saved["month_hours"] == "" + assert saved["user_name"] == "" + assert saved["company"] == "" assert isinstance(saved["last_refresh_at"], str) calls.clear() again = client.get("/api/admin/users") diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index 6cad719..9be9c7e 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -150,6 +150,8 @@ def test_login_stores_password_and_credential_only(monkeypatch) -> None: "last_refresh_at", "credential_valid", "month_hours", + "user_name", + "company", "hours_month", "monthly_target", "proxy_scheme", @@ -185,6 +187,18 @@ def test_login_stores_password_and_credential_only(monkeypatch) -> None: assert updated["yxt_token"] == "yxt-2" assert "测试用户" not in decrypt_secret(account.credential_cipher) assert "测试用户" not in decrypt_secret(account.password_cipher) + assert account.user_name == "测试用户" + assert account.company == "测试部门" + from euniv.service import _remember_profile + + _remember_profile(db, "6650000146", {"name": " ", "department": "", "level": "Scientist"}) + db.refresh(account) + assert account.user_name == "测试用户" + assert account.company == "测试部门" + _remember_profile(db, "6650000146", {"name": "新名字", "department": "广州瀚信广州处", "level": "Scientist"}) + db.refresh(account) + assert account.user_name == "新名字" + assert account.company == "广州瀚信广州处" def test_qr_mark_cannot_password_login_and_unicode_password_is_safe(monkeypatch) -> None: diff --git a/web/src/features/admin/UsersView.vue b/web/src/features/admin/UsersView.vue index 2414ce6..1dd022c 100644 --- a/web/src/features/admin/UsersView.vue +++ b/web/src/features/admin/UsersView.vue @@ -9,6 +9,8 @@ import { isRecord, textOf } from "../../shared/text"; interface UserRow { employee_id: string; + user_name: string; + company: string; has_credential: boolean; refresh_paused: boolean; last_refresh_at: string | null; @@ -164,6 +166,8 @@ function asUser(value: unknown): UserRow | null { if (!isRecord(value) || (typeof value.employee_id !== "string" && typeof value.employee_id !== "number")) return null; return { employee_id: String(value.employee_id), + user_name: textOf(value.user_name), + company: textOf(value.company), has_credential: value.has_credential === true, refresh_paused: value.refresh_paused === true, last_refresh_at: typeof value.last_refresh_at === "string" && value.last_refresh_at ? value.last_refresh_at : null, @@ -892,6 +896,8 @@ onMounted(() => { 工号 + 用户名称 + 公司 凭据 有效 本月学时 @@ -908,6 +914,8 @@ onMounted(() => { {{ row.employee_id }} + {{ row.user_name || "—" }} + {{ row.company || "—" }} {{ row.has_credential ? "已保存凭据" : "未保存凭据" }} {{ validityText(row) }} {{ monthHoursText(row) }} diff --git a/web/src/styles.css b/web/src/styles.css index b5fbf22..b5859d0 100644 --- a/web/src/styles.css +++ b/web/src/styles.css @@ -1569,7 +1569,7 @@ select { } .admin-table { width: 100%; - min-width: 820px; + min-width: 980px; border-collapse: collapse; } @@ -1597,11 +1597,16 @@ select { } .admin-table .employee, +.admin-table .name, .admin-table .when { font-variant-numeric: tabular-nums; white-space: nowrap; } +.admin-table .company { + max-width: 240px; +} + .admin-table .ok { color: var(--accent); }