Skip to content

Fix(userStore): refactor setUserSetting to be async and ensure proper setting initialization#685

Merged
kanyxmo merged 4 commits into
mineadmin:masterfrom
people-sea:fix/issue/683
Sep 4, 2025
Merged

Fix(userStore): refactor setUserSetting to be async and ensure proper setting initialization#685
kanyxmo merged 4 commits into
mineadmin:masterfrom
people-sea:fix/issue/683

Conversation

@people-sea

@people-sea people-sea commented Sep 2, 2025

Copy link
Copy Markdown
Member

Fixes: #683

Summary by CodeRabbit

  • Bug Fixes

    • 修复用户设置未及时生效的问题:现在在应用设置后再刷新菜单与角色,避免界面不一致与短暂闪烁。
  • Improvements

    • 用户设置应用改为异步并在下一个渲染周期执行,主题与配色初始化顺序调整,语言设置随之稳定生效。
    • 增强语言输入校验(拒绝空/非字符串/仅空白值),并对来自后端的设置进行规范化处理,确保更可靠的配置应用与回退逻辑。

@coderabbitai

coderabbitai Bot commented Sep 2, 2025

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

web/src/store/modules/useUserStore.ts 中的用户设置加载与副作用执行顺序进行了调整:requestUserInfo 现在在规范化 backend_setting 后显式 await setUserSetting(...)setUserSetting 改为异步并在 nextTick() 后初始化主题与设置语言;setLanguage 增加输入校验并持久化语言。

Changes

Cohort / File(s) Summary
用户设置与时序调整
web/src/store/modules/useUserStore.ts
requestUserInfo 中对 data?.backend_setting 做了规范化(非数组则保留,否则为 null)并改为 await setUserSetting(normalized)setUserSetting 签名改为 async,内部在 await nextTick() 后先调用 initThemeColor(),再根据 `settings?.app?.useLocale ?? (language.value?.trim()
语言设置校验与持久化
web/src/store/modules/useUserStore.ts
setLanguage 新增输入校验:若 langName 为假值、非字符串或仅空白则返回 false;否则对 langNametrim() 后赋值并持久化到缓存。

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor U as 用户
  participant UI as 前端界面
  participant Store as useUserStore
  participant API as 后端

  U->>UI: 登录
  UI->>Store: requestUserInfo()
  Store->>API: 获取用户信息与 backend_setting
  API-->>Store: 返回 data (含 backend_setting)
  Store->>Store: 规范化 raw = data?.backend_setting -> normalized
  Store->>Store: await setUserSetting(normalized)
  Note over Store: setUserSetting 异步执行\n在 nextTick 后执行副作用
  rect rgb(235,245,235)
    Store->>Store: initThemeColor()  -- (先初始化主题)
    Store->>Store: setLanguage(locale) -- (后设置语言,含输入校验)
  end
  Store->>Store: refreshMenu()
  Store->>Store: refreshRole()
  Store-->>UI: 初始化完成
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Assessment against linked issues

Objective Addressed Explanation
修复重新登录后主题深浅色错乱问题(#683

我是小兔在代码里蹦,
把主题理直又整齐,
nextTick 一等风轻拂,
早晨登录色不惊。
菜单角色共欢喜,兔子跳个圈~ 🐇✨


📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 41c529b and d7bdb7a.

📒 Files selected for processing (1)
  • web/src/store/modules/useUserStore.ts (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
web/src/store/modules/useUserStore.ts (1)
web/src/hooks/useThemeColor.ts (1)
  • useThemeColor (12-83)
🔇 Additional comments (2)
web/src/store/modules/useUserStore.ts (2)

162-164: setLanguage 的输入校验与持久化处理合理,LGTM

避免写入空白语言值,符合预期。


208-216: 初始化顺序与 nextTick 用法正确,LGTM

initColorModeawait nextTick() 后再初始化主题与语言,能减少 UI 闪烁。

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or PR title anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@people-sea
people-sea requested a review from zds-s September 2, 2025 15:31
@people-sea
people-sea marked this pull request as ready for review September 2, 2025 15:31
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug labels Sep 2, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between f8f2e88 and 153a9aa.

📒 Files selected for processing (1)
  • web/src/store/modules/useUserStore.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
web/src/store/modules/useUserStore.ts (1)
web/src/hooks/useThemeColor.ts (1)
  • useThemeColor (12-83)
🔇 Additional comments (1)
web/src/store/modules/useUserStore.ts (1)

131-132: 在 requestUserInfo 中等待 setUserSetting:顺序收敛合理,能消除竞态

确保菜单/路由刷新发生在主题与语言初始化之后,有助于修复 #683 提到的首次进入时的主题/本地化错乱问题。

建议手动验证场景:

  • 步骤:前端自动登出过夜 → 次日登录 → 检查各页签/新开页签的深浅色与分页文案语言是否一致,无需强刷即可生效。

Comment thread web/src/store/modules/useUserStore.ts
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Sep 2, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

♻️ Duplicate comments (1)
web/src/store/modules/useUserStore.ts (1)

205-212: setUserSetting 异步化 + nextTick 顺序优化:LGTM

按先设置配置/色阶模式 → await nextTick → 初始化主题色 → 设置语言的顺序,有助于消除首屏/切页闪烁与时序竞态。与此前建议一致,改动到位。

🧹 Nitpick comments (2)
web/src/store/modules/useUserStore.ts (2)

227-234: 登出时不建议将 language.value 置为 '';否则回退逻辑可能被“空字符串”短路

即便清除了缓存,仍把 language.value 写成空字符串,会与上方回退一起保留 ''。如果产品预期是“保留用户语言偏好”,建议保留内存值;若需重置,直接设为默认值而非空字符串。

-      cache.remove('language')
+      // 若需重置为默认语言再清缓存;否则保留用户偏好
+      // cache.remove('language')
 ...
-      language.value = ''
+      // 保留内存中的语言;如需重置,设为默认而非空字符串
+      // language.value = 'zh_CN'

如确认必须清缓存,也请结合上方对 setLanguage 的加固,避免写入空值。


214-221: 命名拼写:saveSettingToSever → saveSettingToServer

小拼写问题,建议统一为 “Server”。若对外导出/被外部调用,请同步更新导出对象与调用点。

-    function saveSettingToSever() {
+    function saveSettingToServer() {
       const backend_setting = setting.getSettings()
       useHttp().post('/admin/permission/update', { backend_setting }).then(() => {
         cache.set('sys_settings', backend_setting)
       }).catch((error) => {
         console.log(error)
       })
     }
@@
     return {
@@
-      saveSettingToSever,
+      saveSettingToServer,
@@
     }

可随后全局检索调用点以确保无遗漏:

rg -nP --type=ts '\bsaveSettingToSever\b' -C2

Also applies to: 239-259

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 153a9aa and 24e5ec3.

📒 Files selected for processing (1)
  • web/src/store/modules/useUserStore.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
web/src/store/modules/useUserStore.ts (1)
web/src/hooks/useThemeColor.ts (1)
  • useThemeColor (12-83)

Comment thread web/src/store/modules/useUserStore.ts Outdated
Comment thread web/src/store/modules/useUserStore.ts Outdated
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Sep 2, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
web/src/store/modules/useUserStore.ts (1)

161-166: 语言写入已加固,避免空串污染缓存:赞

现在会忽略空/空白语言值并返回布尔值,符合预期。请保持该约束。

🧹 Nitpick comments (1)
web/src/store/modules/useUserStore.ts (1)

208-216: 初始化顺序与 nextTick 使用正确;微调 locale 计算与幂等设置以减少无效写入

整体顺序 LGTM。为避免写入仅含空白的 locale,并减少重复写入缓存的小抖动,可小幅收敛:

@@
-      useThemeColor().initThemeColor()
-      const locale = settings?.app?.useLocale ?? (language.value?.trim() || 'zh_CN')
-      setLanguage(locale)
+      useThemeColor().initThemeColor()
+      const rawLocale = typeof settings?.app?.useLocale === 'string' ? settings!.app!.useLocale : undefined
+      const locale = (rawLocale?.trim()) || (language.value?.trim() || 'zh_CN')
+      if (locale !== language.value) {
+        setLanguage(locale)
+      }
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 24e5ec3 and 41c529b.

📒 Files selected for processing (1)
  • web/src/store/modules/useUserStore.ts (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
web/src/store/modules/useUserStore.ts (1)
web/src/hooks/useThemeColor.ts (1)
  • useThemeColor (12-83)
🔇 Additional comments (1)
web/src/store/modules/useUserStore.ts (1)

208-216: 确认完毕:setUserSetting 的唯一调用(133 行)已使用 await,无遗漏

Comment thread web/src/store/modules/useUserStore.ts
coderabbitai[bot]
coderabbitai Bot previously approved these changes Sep 2, 2025
@zds-s
zds-s requested review from kanyxmo and removed request for kanyxmo and zds-s September 3, 2025 01:08
Changed the parameter type of setUserSetting function to 'any'.
@kanyxmo
kanyxmo merged commit 09c8d92 into mineadmin:master Sep 4, 2025
4 checks passed
@lait233

lait233 commented Sep 10, 2025

Copy link
Copy Markdown
Contributor

@people-sea 大佬 部门分支能同步修复合并一下吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Frontend size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] 前端页面系统自动退出登录后 第二天重新登录系统会出现主题 深色/浅色 颜色错乱问题

3 participants