From 71458e2c323b978b1fc423a024ab4a4026bd62d2 Mon Sep 17 00:00:00 2001 From: kangwenjing <1138819403@qq.com> Date: Thu, 19 Nov 2026 09:12:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E4=B8=BB=E9=A2=98?= =?UTF-8?q?=E8=89=B2=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF=E6=8C=81=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E8=87=AA=E5=AE=9A=E4=B9=89=E7=95=8C=E9=9D=A2=E4=B8=BB?= =?UTF-8?q?=E8=89=B2=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增主题色选择器组件,支持青、蓝、靛蓝、紫、玫红、橙六种颜色选项 前后端适配主题色存储与同步,用户偏好新增 color 字段 登录页与个人设置界面适配主题色展示,样式表按主题色拆分多套配色 完善相关国际化文案与测试用例,优化登录页面布局适配主题色切换 --- README.md | 33 ++- backend/app/api/auth.py | 9 +- backend/app/schemas.py | 14 +- backend/app/serializers.py | 4 +- backend/config.toml | 4 +- docs/frontend-design.md | 38 ++- docs/todo-inbox-unified-design.md | 8 +- frontend/index.html | 15 +- frontend/src/api/auth.ts | 4 +- .../common/AccentColorPicker.spec.ts | 58 ++++ .../components/common/AccentColorPicker.vue | 51 ++++ frontend/src/components/common/Input.vue | 4 +- frontend/src/components/common/index.ts | 1 + frontend/src/components/icons/Icon.vue | 4 + frontend/src/components/layout/AppHeader.vue | 9 +- frontend/src/components/layout/AuthLayout.vue | 147 +++++++--- frontend/src/components/portal/FilePanel.vue | 3 +- frontend/src/components/portal/NewsPanel.vue | 2 +- .../src/components/portal/SchedulePanel.vue | 9 +- frontend/src/composables/useAppearance.ts | 14 +- frontend/src/i18n/locales/en.ts | 22 +- frontend/src/i18n/locales/zh.ts | 22 +- frontend/src/style.css | 276 ++++++++++++++++++ frontend/src/styles/onboarding.css | 6 +- frontend/src/types/index.ts | 6 +- frontend/src/utils/appearance.ts | 25 +- frontend/src/utils/branding.spec.ts | 14 +- frontend/src/utils/themeColor.spec.ts | 216 ++++++++++++++ frontend/src/utils/themeColor.ts | 225 ++++++++++++++ frontend/src/views/auth/LoginView.vue | 162 +++++----- frontend/src/views/auth/SsoCallbackView.vue | 23 +- frontend/src/views/oa/ProfileView.vue | 14 +- frontend/src/views/oa/StartFormView.vue | 3 +- frontend/src/views/oa/StartProcessView.vue | 3 +- frontend/src/views/oa/StatisticsView.vue | 7 +- frontend/src/views/oa/WorkbenchView.vue | 3 +- .../src/views/oa/design/ProcessListView.vue | 9 +- frontend/src/views/portal/FileDriveView.vue | 3 +- frontend/src/views/portal/PortalHomeView.vue | 7 +- frontend/src/views/portal/ScheduleView.vue | 17 +- frontend/tailwind.config.js | 41 +-- 41 files changed, 1313 insertions(+), 222 deletions(-) create mode 100644 frontend/src/components/common/AccentColorPicker.spec.ts create mode 100644 frontend/src/components/common/AccentColorPicker.vue create mode 100644 frontend/src/utils/themeColor.spec.ts create mode 100644 frontend/src/utils/themeColor.ts diff --git a/README.md b/README.md index 777fb8f..fd29ae5 100644 --- a/README.md +++ b/README.md @@ -3274,7 +3274,9 @@ key 等于每换一张单整页重挂,那些 `watch` 全成了死代码,浏 - 复用层:`components/common/*`(`DataTable`、`TablePageLayout`、`BaseDialog`、 `ConfirmDialog`、`Pagination`、`Select`、`StatCard`、`DateRangePicker`、`SearchInput`)、 `style.css` 中的 `.btn/.input/.card/.badge/.tabs/.switch/.page-header/.stat-*`、 - teal 主色的 Tailwind 主题、暗色模式、`useTableLoader` 分页与请求取消、Pinia store 骨架 + 主色可换的 Tailwind 主题(默认 teal 青,色板是 `style.css` 里那几组 `--primary-*`, + 换档只换 ``,见「主题色」那一节)、暗色模式、 + `useTableLoader` 分页与请求取消、Pinia store 骨架 - OA 新增:`components/oa/FormRenderer.vue`(按表单定义渲染 12 种字段,`design` 模式 额外画出卡片化的字段、空位斜纹与「第 N 行 · 第 i/2 列」角标,审批页用的就是同一个组件)、 `utils/formLayout.ts`(字段落位算法:行/列/占几格、被挤到下一行留出的空位、字段名去重、设计校验)、 @@ -3355,7 +3357,12 @@ key 等于每换一张单整页重挂,那些 `watch` 全成了死代码,浏 一句人话 + 送回门户,等待态是 `index.html` 里那幅进站骨架,320ms 之内不出现); `views/auth/LoginView.vue`(Hub 开启时显示 Hub 下发的验证码、登录失败自动换一张;`hub.sso_only` 时不渲染账号密码,只留「前往统一门户」, - `?local=1` 保留应急本地口令入口);`utils/portal.ts` 是唯一的「整页跳去统一门户」出口, + `?local=1` 保留应急本地口令入口。版面抄统一门户那边 EDRS 的登录页:宽屏左边一整栏讲系统、 + 右边一块半透明磨砂只放登录,底下「其他登录方式」那一排圆钮就是回门户的那条第三方入口 + (点的还是原来那颗「前往统一门户」走的那条路:先让后端写 state,再整页跳), + 样式集中在 `style.css` 的 `.login-*` 那一组,底色跟着主题色走; + `components/layout/AuthLayout.vue` 是那层壳子,右上角两颗是语言与深浅); + `utils/portal.ts` 是唯一的「整页跳去统一门户」出口, `utils/sessionIdle.ts` 挂空闲心跳与本地超时计时(后端没配 `OA_SESSION_IDLE_MINUTES` 时什么都不做); `components/oa/HubPermissionPanel.vue`(权限角色页顶部:自己在 Hub 上的角色/权限码/数据范围、 与本地权限目录对账 + 一键登记、按账号代查;`hub_perm_enabled` 关着时整块不渲染) @@ -3376,6 +3383,27 @@ key 等于每换一张单整页重挂,那些 `watch` 全成了死代码,浏 `t('a.b')`,逐条走两棵树,任何一环取不到就报出「文件 + key」。它自己不发明写法, 动态拼出来的 key(``t(`${x}.title`)``)归各自那道闸门看住 +- 主题色(整体颜色):一整套界面的主色只有 `style.css` 里那十一组 `--primary-*`(RGB 三元组), + `tailwind.config.js` 把 `primary-*` 全部指到这些变量上并交出 ``, + 所以 `bg-primary-500/30` 这类带透明度的写法照旧生效,换一处就换整套。用哪一套由 + `` 决定,色板本身只住在 CSS 里(六套:青(默认)/ 蓝 / 靛蓝 / 紫 / 玫红 / 橙), + JS 只带一个 id:`utils/themeColor.ts` 认档、写属性、顺手把 `` 对上; + `index.html` 那段防闪脚本在画第一笔之前就把属性写上(认不出的值不写,落回默认青), + 因此不会出现"先青后蓝闪一下"。存法与深色档、字号同一班车——本地 `oa.accent` 是缓存, + `users.preferences.color` 才是权威,登录后由 `adoptPreferences` 接回来, + 换设备、清缓存、从企微换入口进来都还是自己挑的那一版;后端只认 + `schemas.ACCENT_COLORS` 那六个值,认不出的整档丢掉。入口三处共用 + `components/common/AccentColorPicker.vue`(顶栏「外观」、桌面「个人设置」、手机「我的」)。 + CSS 变量到不了的两处也问同一本账:统计页画在 canvas 上的线与柱子、页签上那枚内置 "OA" 图标, + 由 `accentColor(600)` / `accentRgba(600, .18)` 现问浏览器「当前这一档的 `--primary-600` + 算出来是哪三个数」,换档时上层那些 `computed` 跟着重算,图会当场改颜色;流程、日程、 + 未知后缀文件在**没配色**时的兜底同样走这条路(配过的颜色是数据,不跟着换), + 日程接口也不再替空颜色填上旧的 `#0d9488`。只有 `DEFAULT_ACCENT_RGB` 一份是抄下来的, + 用在样式表还没下载的那一瞬,`themeColor.spec.ts` 会把 `style.css` 抠出来逐档比对,抄漏了当场红。 + **加一档颜色要在两处对齐**:`style.css` 里补一份同名色板 + `utils/themeColor.ts` 的 + `ACCENT_OPTIONS` 加一条(含两份 locale 里那两个名字),只动 JS 那一头会静默退回默认青, + `utils/themeColor.spec.ts` 钉的就是这层对应关系 + ## 自检 仓库里不留测试代码:`backend/tests/` 那 42 套验收脚本、`backend/scripts/run_all.py`、 @@ -3425,6 +3453,7 @@ pnpm typecheck # vue-tsc --noEmit pnpm lint:check # eslint(不带 --fix) pnpm build # vue-tsc -b && vite build pnpm vitest run src/utils/browserCompatGuard.spec.ts # 浏览器口径闸门:带出值/展示这条链不许用回没有兜底的写法 +pnpm vitest run src/utils/themeColor.spec.ts src/components/common/AccentColorPicker.spec.ts # 动过主题色(style.css 那几组 --primary-* / utils/themeColor / 那一排圆点):六档与色板一一对应、点一档就换 并留在本地、认不出的值不换画面也不写盘 pnpm vitest run src/utils/branding.spec.ts # 动过主色 / 左上角那块 "OA" 磁贴 / index.html 里那两句图标链接:浏览器页签上那一枚必须还是同一张图(换主色要重跑 frontend/scripts/gen-icons.py) pnpm vitest run src/stores/tabs.spec.ts src/components/layout/AppLayout.tabs.spec.ts src/composables/useTableLoader.persist.spec.ts src/views/oa/MonitorView.persist.spec.ts src/views/oa/ReadListView.batch.spec.ts # 动过页签 / KeepAlive / 列表条件持久化 / 待阅批量:两张单子各占一格且写的是单据标题、列表页留活而详情页每回重新拉、关掉那张之后钥匙换号、条件与页码带得回来(窄屏只带条件、越界那页收回去)、批量只发看得见的这几条 pnpm vitest run src/views/oa/TodoListView.batchRead.spec.ts src/views/oa/TodoListView.batch.spec.ts src/views/oa/ReadListView.batch.spec.ts # 动过待办 / 待阅那两颗批量钮与「全选这一片」:批量已读只交「动哪几条」不发出口不发意见、按下去先确认一次、五档各说一句人话、整片那一档交的是条件不是几个 id、手机端那一横条上第二颗钮还在(少了 `secondary-label` 手机上就退回点十几下) diff --git a/backend/app/api/auth.py b/backend/app/api/auth.py index 11a227e..a1256b1 100644 --- a/backend/app/api/auth.py +++ b/backend/app/api/auth.py @@ -935,7 +935,7 @@ def update_my_preferences( user: CurrentUser = Depends(current_user), db: Session = Depends(get_db), ) -> dict: - """保存本人的界面偏好(深色档 / 字号):设一次,换设备、清缓存之后还是那一版。 + """保存本人的界面偏好(深色档 / 字号 / 主题色):设一次,换设备、清缓存之后还是那一版。 只收白名单里的取值(见 :func:`app.schemas.clean_preferences`),认不出的那几档整档丢掉; 没给的键保持原样,所以界面可以只提交刚改动的那一档。这是展示层的偏好, @@ -945,8 +945,11 @@ def update_my_preferences( if person is None: raise unauthorized() patch = schemas.clean_preferences( - {name: value for name, value in (("theme", body.theme), ("font", body.font)) - if name in body.model_fields_set} + {name: value for name, value in ( + ("theme", body.theme), + ("font", body.font), + ("color", body.color), + ) if name in body.model_fields_set} ) #: JSON 列原地改 SQLAlchemy 认不出来(比对的是同一个对象),必须整体换一个 dict 上去 merged = dict(person.preferences or {}) diff --git a/backend/app/schemas.py b/backend/app/schemas.py index aeefffb..b6e00fb 100644 --- a/backend/app/schemas.py +++ b/backend/app/schemas.py @@ -297,10 +297,19 @@ class ChangePasswordRequest(CamelModel): THEMES = ("light", "dark", "system") #: 字号档位与前端 ``useMobileShell.FontScale`` 一字不差:m 是默认(不写进库)。 FONT_SCALES = ("m", "l", "xl") +#: 主题色档位与前端 ``utils/themeColor.ACCENTS`` 一字不差(色板在 style.css 按 +#: ```` 分套):认不出的那一档画不出颜色,留着只会让人以为设坏了。 +ACCENT_COLORS = ("teal", "blue", "indigo", "violet", "rose", "amber") +#: teal 是默认那一档,存进库也画成同一个样子,但照样收着——它表达的是"我挑的", +#: 和"从没挑过"在界面上是同一张脸,没必要为这点差别另开一列。 #: 每一档认的取值;``None`` 单独认,意思是"这一档回到默认"。 -PREFERENCE_VALUES: dict[str, tuple[str, ...]] = {"theme": THEMES, "font": FONT_SCALES} +PREFERENCE_VALUES: dict[str, tuple[str, ...]] = { + "theme": THEMES, + "font": FONT_SCALES, + "color": ACCENT_COLORS, +} def clean_preferences(raw: object) -> dict: @@ -326,10 +335,11 @@ def clean_preferences(raw: object) -> dict: class PreferencesBody(CamelModel): - """``PUT /auth/me/preferences``:界面偏好(深色档 / 字号),没给的键保持原样。""" + """``PUT /auth/me/preferences``:界面偏好(深色档 / 字号 / 主题色),没给的键保持原样。""" theme: str | None = None font: str | None = None + color: str | None = None class BankAccountBody(CamelModel): diff --git a/backend/app/serializers.py b/backend/app/serializers.py index 4957d4f..de26f01 100644 --- a/backend/app/serializers.py +++ b/backend/app/serializers.py @@ -888,7 +888,9 @@ def schedule(row: models.Schedule) -> dict: "start_time": dt(row.start_time), "end_time": dt(row.end_time), "all_day": row.all_day, - "color": row.color or "#0d9488", + # 没配色就回空串,让前端拿当前那一档主题色兜底。 + # 早先这里写死 "#0d9488":界面换成别的档,没填过颜色的日程仍旧一片旧青。 + "color": row.color, "owner": row.owner, "owner_name": row.owner_name, "shared_identities": list(row.shared_identities or []), diff --git a/backend/config.toml b/backend/config.toml index fe2aa89..4e6a09b 100644 --- a/backend/config.toml +++ b/backend/config.toml @@ -49,7 +49,7 @@ # 永远读不到(文件路径本身就是它决定的),后端会在日志里提醒一句并忽略。 # ----------------------------------------------------------------------------- [env] -name = "dev" +name = "prod" # ----------------------------------------------------------------------------- # 哪些项必须写在 config.<环境名>.toml 里(本文件不放,放了也是被盖住的那一层): @@ -80,7 +80,7 @@ port = 8000 # 监听端口(OA_PORT);uvicorn 命令行的 --port # Logo、是否开放注册按内置默认,界面上改 # ----------------------------------------------------------------------------- [site] -name = "UnisOA" # 系统名称(OA_SITE_NAME) +name = "紫光汇智OA" # 系统名称(OA_SITE_NAME) description = "紫光汇智流程审批系统" # 一句话描述(OA_SITE_DESCRIPTION),显示在登录页 # ----------------------------------------------------------------------------- diff --git a/docs/frontend-design.md b/docs/frontend-design.md index 51c2894..cd3aa7d 100644 --- a/docs/frontend-design.md +++ b/docs/frontend-design.md @@ -73,14 +73,38 @@ PC 与手机两版的版式逐条写在 `docs/todo-inbox-unified-design.md` §4 ## 4. 色彩 -`tailwind.config.js` 定义了三个语义槽,注意 primary 实际是 Teal/Cyan 而不是蓝: +`tailwind.config.js` 定义了三个语义槽,注意 primary 默认是 Teal/Cyan 而不是蓝: | 槽 | 色相 | 用途 | | --- | --- | --- | -| `primary-500` = `#14b8a6` | 青 | 主按钮、当前导航、聚焦环、进度条 | +| `primary-500` = `#14b8a6`(默认档) | 青 | 主按钮、当前导航、聚焦环、进度条 | | `accent` / `dark` | 深蓝灰 slate | 中性层、边框、深色模式底色 | | 画布选中态 `indigo-600` | 靛蓝 | **仅**出现在设计器工具栏激活态,不与主色争夺注意力 | +primary 那一套不是写死的:`tailwind.config.js` 把 `primary-*` 指到 +`rgb(var(--primary-N) / )`,色板住在 `style.css` 的 `:root` 与各 +`html[data-accent=…]` 里(青 / 蓝 / 靛蓝 / 紫 / 玫红 / 橙六套,每套 50…950 共十一组 +RGB 三元组)。存三元组而不是整条颜色,为的是界面里那些 `primary-500/30` +的写法——只有把 RGB 拆开交给 ``,才能既换色又保住透明度层次。 +用户在「外观」里挑哪一套,`utils/themeColor.ts` 就往 `` 上写那个属性, +登录页 `.login-*` 那一组底色也读同一批变量,所以换色是整套的,不留死角。 +**新加一档要在 `style.css` 与 `ACCENT_OPTIONS` 两处对齐**,只对上一半会静默退回默认青。 + +CSS 变量到不了的地方只有两类,都收在 `utils/themeColor.ts` 里问同一本账: + +| 场景 | 用法 | +| --- | --- | +| 画在 canvas 上(统计页折线/柱状)、拼给 SVG、`` 里的图标 | `accentColor(600)` / `accentRgba(600, .18)`:现问浏览器「当前这一档的 `--primary-600` 算出来是哪三个数」,不在 JS 里抄第二份色板 | +| 数据没配颜色时的兜底(流程、日程、未知后缀的文件) | 同上,`process.color \|\| accentColor(600)`。管理员**配过**的颜色是数据,跟着主题换才叫串色 | + +`accentColor()` 读了 `accent` 这一档,所以套在 `computed` 与模板里会跟着换色实时重算, +已经画出来的图会当场改颜色。页签上那枚内置 "OA" 图标也由它重画(管理员在「站点信息」 +里配过自己的 logo 时让开)。唯一跟着不到的是 `public/manifest.webmanifest` 的 +`theme_color` 与装到桌面上的 PNG——那是静态文件,由 `scripts/gen-icons.py` 生成, +按默认档固定,属于已知边界。 +`DEFAULT_ACCENT_RGB` 是"样式表还没加载"时的那一份兜底副本, +`themeColor.spec.ts` 会把 `style.css` 抠出来逐档比,改 CSS 忘了改这里会红。 + 类型色板固定,流程节点、图例、轨迹图共用(`flowGraph.TYPE_COLORS`): ``` @@ -94,7 +118,15 @@ parallel/merge/split #7c3aed 紫 script/invoke/embed #2563eb 蓝 delay #0891b 圆点式状态用 `StatusBadge.vue`(`active/success` 绿、`warning` 黄、`danger` 红、其余灰)。 深色模式靠 `class="dark"` 切换(`main.ts` 的 `initThemeClass` 读 localStorage 与 -`prefers-color-scheme`),组件里成对写 `dark:`,不做第二套主题文件。 +`prefers-color-scheme`),组件里成对写 `dark:`,不做第二套主题文件。主题色同一道理: +组件里照旧写 `bg-primary-600`,不在组件里写死十六进制,否则换色时会露出一块没跟着走的旧色。 + +**故意不跟着主题色走的几处**(都是"分类"而不是"品牌",换色时它们必须保持不动, +否则图例和实际颜色会对不上):流程节点类型色 `flowGraph.TYPE_COLORS`、 +设计器里字段类型的底色与选中态 `indigo`(`FormSheetCanvas.vue`,选中态必须压得过任何一档主色)、 +文件后缀色板(`FilePanel.vue` / `FileDriveView.vue` 的 `TINTS`)、 +新建日程那八颗色片里除第一颗以外的七颗、以及库里存着的业务数据色 +(流程定义 `color`、门户分类色、门户封面图)。 ## 5. 排版 diff --git a/docs/todo-inbox-unified-design.md b/docs/todo-inbox-unified-design.md index 942297d..e4e30fb 100644 --- a/docs/todo-inbox-unified-design.md +++ b/docs/todo-inbox-unified-design.md @@ -219,11 +219,15 @@ ## 8. 外观偏好(顺带) -深色档与字号以前只活在 `localStorage`,换台设备回到解放前。现在 `users.preferences`(JSON) +深色档、字号与主题色(2026-11-18 起多这一档,`users.preferences.color`,六档: +青 / 蓝 / 靛蓝 / 紫 / 玫红 / 橙)以前只活在 `localStorage`,换台设备回到解放前。现在 `users.preferences`(JSON) 是权威,`PUT /auth/me/preferences` 写,`GET /auth/me` 回;`localStorage` 降级成缓存, 因为首屏那段防闪脚本只能在本地读(等接口回来再上色,人已经看见一次白闪)。 会话落地时 `stores/auth.ts` 调 `adoptPreferences` 把服务端的接过来。 -读写都在 `utils/appearance.ts`,入口两处:顶栏那颗外观钮(电脑端)与个人页那一格分段选择。 +深浅与字号读写在 `utils/appearance.ts`,主题色在 `utils/themeColor.ts`(色板本身在 +`style.css`,JS 只带一个 id;`index.html` 那段防闪脚本负责画第一笔),`adoptPreferences` +一次把三档都接过来。入口三处:顶栏那颗外观钮(电脑端)、个人页那一格分段选择、 +手机「我的」里那一栏,三处共用 `components/common/AccentColorPicker.vue`。 ## 9. 上线要动库 diff --git a/frontend/index.html b/frontend/index.html index e4e1b79..b80380f 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -37,6 +37,14 @@ // 不用 classList.toggle(name, force):IE 11 认不出第二个参数,深色档会变反。 if (dark) document.documentElement.classList.add('dark') else document.documentElement.classList.remove('dark') + // 主题色同理:整套色板在样式表里按 分套, + // 首屏这一笔只写那一个属性,色值一份都不抄(抄一份就会走样)。 + // 认不出的值干脆不写,落回 :root 那一套默认青。 + var accent = localStorage.getItem('oa.accent') + if (accent === 'teal' || accent === 'blue' || accent === 'indigo' || + accent === 'violet' || accent === 'rose' || accent === 'amber') { + document.documentElement.setAttribute('data-accent', accent) + } } catch (e) { /* 隐私模式里 localStorage 会抛:照着浅色画就是 */ } @@ -140,7 +148,12 @@ .dark .boot-tabs { border-color: #1e293b; } .dark .boot-card { background: #1e293b; } .boot-bar, .boot-card, .boot-tabs, .boot-block { - background-image: linear-gradient(90deg, transparent 0%, rgba(13, 148, 136, 0.08) 50%, transparent 100%); + background-image: linear-gradient( + 90deg, + transparent 0%, + rgb(var(--primary-500, 20 184 166) / 0.08) 50%, + transparent 100% + ); background-size: 200% 100%; background-repeat: no-repeat; animation: boot-shimmer 1.6s linear infinite; diff --git a/frontend/src/api/auth.ts b/frontend/src/api/auth.ts index 2be97f9..3fdfccf 100644 --- a/frontend/src/api/auth.ts +++ b/frontend/src/api/auth.ts @@ -1,5 +1,6 @@ import { http } from './client' import type { BankAccount, LoginResponse, PublicSettings, User } from '@/types' +import type { AccentId } from '@/utils/themeColor' export interface LoginPayload { account: string @@ -105,7 +106,7 @@ export async function updateBankAccount(payload: Pick { diff --git a/frontend/src/components/common/AccentColorPicker.spec.ts b/frontend/src/components/common/AccentColorPicker.spec.ts new file mode 100644 index 0000000..69810cf --- /dev/null +++ b/frontend/src/components/common/AccentColorPicker.spec.ts @@ -0,0 +1,58 @@ +// @vitest-environment jsdom +/** + * 主题色那一排圆点:点了要立刻换色,并且当前那一颗看得出来是选中了的。 + * + * 三个入口(顶栏「外观」、桌面个人设置、手机「我的」)共用这一个组件, + * 所以这里只钉组件本身该做的事——存库那条路归 useAppearance 管。 + */ +import { beforeEach, describe, expect, it } from 'vitest' +import { mount } from '@vue/test-utils' +import { createPinia, setActivePinia } from 'pinia' +import { createI18n } from 'vue-i18n' +import zh from '@/i18n/locales/zh' +import { useAuthStore } from '@/stores' +import { ACCENT_ATTR, ACCENT_OPTIONS, accent, DEFAULT_ACCENT, setAccentLocal } from '@/utils/themeColor' +import AccentColorPicker from './AccentColorPicker.vue' + +const i18n = createI18n({ legacy: false, locale: 'zh', messages: { zh } }) + +function open() { + const pinia = createPinia() + setActivePinia(pinia) + useAuthStore() + return mount(AccentColorPicker, { props: { testPrefix: 'oa' }, global: { plugins: [i18n, pinia] } }) +} + +beforeEach(() => { + // 这一档的状态是模块级的一份 ref,跨用例还留着:先把它按回默认再清本地, + // 否则「点当前这一档不写盘」那一条会被上一个用例选的色污染。 + setAccentLocal(DEFAULT_ACCENT) + localStorage.clear() + document.documentElement.removeAttribute(ACCENT_ATTR) +}) + +describe('主题色选择器', () => { + it('一档一颗圆点,标题写的是这一档的名字', async () => { + const wrapper = open() + const buttons = wrapper.findAll('button') + expect(buttons).toHaveLength(ACCENT_OPTIONS.length) + expect(buttons.map((button) => button.attributes('title'))).toEqual( + ACCENT_OPTIONS.map((option) => zh.mobile[option.nameKey.split('.')[1]]) + ) + }) + + it('点一颗就整套界面换色,并把这一档标成选中', async () => { + const wrapper = open() + await wrapper.get('[data-test="oa-color-blue"]').trigger('click') + expect(document.documentElement.getAttribute(ACCENT_ATTR)).toBe('blue') + expect(accent.value).toBe('blue') + expect(wrapper.get('[data-test="oa-color-blue"]').attributes('aria-pressed')).toBe('true') + expect(wrapper.get('[data-test="oa-color-teal"]').attributes('aria-pressed')).toBe('false') + }) + + it('点当前这一档不重复写本地(省一次没必要的写盘)', async () => { + const wrapper = open() + await wrapper.get('[data-test="oa-color-teal"]').trigger('click') + expect(localStorage.getItem('oa.accent')).toBeNull() + }) +}) diff --git a/frontend/src/components/common/AccentColorPicker.vue b/frontend/src/components/common/AccentColorPicker.vue new file mode 100644 index 0000000..88a5f80 --- /dev/null +++ b/frontend/src/components/common/AccentColorPicker.vue @@ -0,0 +1,51 @@ + + + diff --git a/frontend/src/components/common/Input.vue b/frontend/src/components/common/Input.vue index 3475740..823f045 100644 --- a/frontend/src/components/common/Input.vue +++ b/frontend/src/components/common/Input.vue @@ -1,6 +1,6 @@