Skip to content

支持语言切换时保存用户设置至服务器,修复多语言切换后刷新页面无法正确使用当前语言问题。#719

Merged
zds-s merged 1 commit intomineadmin:masterfrom
lait233:pr
Mar 31, 2026
Merged

支持语言切换时保存用户设置至服务器,修复多语言切换后刷新页面无法正确使用当前语言问题。#719
zds-s merged 1 commit intomineadmin:masterfrom
lait233:pr

Conversation

@lait233
Copy link
Copy Markdown
Contributor

@lait233 lait233 commented Mar 30, 2026

如题。修复多语言切换后 刷新 2 遍页面会恢复默认语言的问题。还有刷新页面后请求后端 api 接口时。语言不同步问题

Summary by CodeRabbit

发布说明

  • 改进

    • 语言设置现已异步保存至服务器,确保用户偏好设置得以持久化。
  • Bug 修复

    • 优化了语言区域设置的初始化逻辑,改进了相关的错误处理机制。

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 30, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 30, 2026

📝 Walkthrough

工作流程总结

此次变更升级了语言切换流程以支持异步操作,包括从服务器获取应用设置、更新区域设置,并在更改后将设置持久化保存到服务器。同时优化了用户店中区域初始化的优先级顺序。

变更概览

涵盖范围 / 文件 变更摘要
语言切换异步化
web/src/layouts/components/bars/toolbar/components/translate.tsx
changeLanguage 函数改为异步,新增获取应用设置、更新 appSettings.useLocale,并在结尾调用 userStore.saveSettingToSever() 实现服务器端持久化。
用户店区域初始化优化
web/src/store/modules/useUserStore.ts
调整 setUserSetting 中区域初始化的优先级(优先使用缓存的 language,其次使用 settings.app.useLocale),更新应用设置对象,改进 saveSettingToSever 的错误处理和返回值(现返回 Promise 链)。

序列流程图

sequenceDiagram
    participant User as 用户
    participant Component as 语言切换组件
    participant UserStore as 用户存储
    participant SettingStore as 设置存储
    participant Server as 服务器

    User->>Component: 点击切换语言
    Component->>UserStore: setLanguage(新语言值)
    UserStore->>SettingStore: getSettings('app')
    SettingStore-->>UserStore: 返回应用设置
    UserStore->>UserStore: 更新 appSettings.useLocale
    Component->>Component: 更新i18n区域和标题
    Component->>UserStore: saveSettingToSever()
    UserStore->>Server: POST /admin/permission/update
    Server-->>UserStore: 返回结果
    UserStore-->>Component: 返回Promise
    Component-->>User: 完成
Loading

预估代码审查工作量

🎯 2 (简单) | ⏱️ ~10 分钟

可能相关的PR

建议标签

bug

建议审查者

  • kanyxmo

庆祝诗

🐰 语言在变,异步也在变,
设置存储到服务,不再是幻想,
Promise 来辅助,流程更优雅,
兔子跳跳跳,为这改进欢呼雀跃! 🌍✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题清晰准确地总结了主要变更:支持语言切换时保存用户设置至服务器,修复多语言切换后刷新页面的语言问题,与代码更改内容完全一致。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@dosubot dosubot bot added the enhancement label Mar 30, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@web/src/layouts/components/bars/toolbar/components/translate.tsx`:
- Around line 20-29: 在 changeLanguage 方法中,当调用 userStore.saveSettingToSever()
失败时需要捕获异常并给用户明确反馈并按产品策略处理回滚;修改:在 changeLanguage 中用 try/catch 包裹 await
userStore.saveSettingToSever(),catch 内调用项目的通知/提示方法显示“保存语言设置失败”的信息(并可携带错误详情),必要时在
catch 中回退之前的本地更新(例如通过 userStore.setLanguage(oldValue)、locale.value = oldValue、恢复
appSettings.useLocale 并重新调用 settingStore.setTitle);保留成功路径不变。

In `@web/src/store/modules/useUserStore.ts`:
- Around line 226-231: The caller in settings.tsx currently calls
useUserStore().saveSettingToSever() without awaiting or handling rejection,
causing false success notifications; update the onClick handler in settings.tsx
(the code that triggers saveSettingToSever) to either: make the handler async
and await useUserStore().saveSettingToSever() inside a try/catch and show
success only after await succeeds and show an error notification in catch, or
keep the handler synchronous but chain .then(() => show success).catch(err =>
show failure) so rejected Promises from saveSettingToSever are properly handled
and do not display a success message erroneously.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6d450629-1a6e-40d9-a0d2-2cd70e6b2283

📥 Commits

Reviewing files that changed from the base of the PR and between 31134bd and 85d10fa.

📒 Files selected for processing (2)
  • web/src/layouts/components/bars/toolbar/components/translate.tsx
  • web/src/store/modules/useUserStore.ts

Comment on lines +20 to 29
async function changeLanguage(item: { label: string, value: string }) {
userStore.setLanguage(item.value)
const appSettings = settingStore.getSettings('app')
if (appSettings) {
appSettings.useLocale = item.value
}
locale.value = item.value
settingStore.setTitle(route.meta?.i18n ? t(route.meta?.i18n as string) : route.meta?.title as string)
await userStore.saveSettingToSever()
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

建议补充保存失败兜底,避免“本地已切换但服务端未持久化”无提示。

当前先更新本地语言再请求保存;若请求失败,用户侧没有明确反馈,后续刷新可能回退语言,体验上会像“切换失效”。建议在这里捕获异常并提示失败(必要时可按产品策略回滚本地状态)。

💡 建议修改
 async function changeLanguage(item: { label: string, value: string }) {
   userStore.setLanguage(item.value)
   const appSettings = settingStore.getSettings('app')
   if (appSettings) {
     appSettings.useLocale = item.value
   }
   locale.value = item.value
   settingStore.setTitle(route.meta?.i18n ? t(route.meta?.i18n as string) : route.meta?.title as string)
-  await userStore.saveSettingToSever()
+  try {
+    await userStore.saveSettingToSever()
+  }
+  catch (error) {
+    // 按项目现有消息组件替换为对应提示
+    console.error(error)
+  }
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@web/src/layouts/components/bars/toolbar/components/translate.tsx` around
lines 20 - 29, 在 changeLanguage 方法中,当调用 userStore.saveSettingToSever()
失败时需要捕获异常并给用户明确反馈并按产品策略处理回滚;修改:在 changeLanguage 中用 try/catch 包裹 await
userStore.saveSettingToSever(),catch 内调用项目的通知/提示方法显示“保存语言设置失败”的信息(并可携带错误详情),必要时在
catch 中回退之前的本地更新(例如通过 userStore.setLanguage(oldValue)、locale.value = oldValue、恢复
appSettings.useLocale 并重新调用 settingStore.setTitle);保留成功路径不变。

Comment on lines +226 to 231
return useHttp().post('/admin/permission/update', { backend_setting }).then(() => {
cache.set('sys_settings', backend_setting)
}).catch((error) => {
console.log(error)
return Promise.reject(error)
})
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# 目的:定位所有 saveSettingToSever 调用点,核对是否有 await / .catch 处理
rg -nP --type=ts --type=tsx '\bsaveSettingToSever\s*\(' -C2

Repository: mineadmin/MineAdmin

Length of output: 90


🏁 Script executed:

#!/bin/bash
# 搜索所有 saveSettingToSever 调用点(修正:不指定不支持的 tsx 类型)
rg -n 'saveSettingToSever\s*\(' --type=ts -C 3

Repository: mineadmin/MineAdmin

Length of output: 2069


🏁 Script executed:

#!/bin/bash
# 如果上面失败,尝试不用类型过滤
rg -n 'saveSettingToSever\s*\(' -C 3

Repository: mineadmin/MineAdmin

Length of output: 2069


🏁 Script executed:

#!/bin/bash
# 查看已知问题位置 settings.tsx 附近的代码
fd -e tsx -e ts | xargs grep -l 'saveSettingToSever' | head -5

Repository: mineadmin/MineAdmin

Length of output: 229


saveSettingToSever 的 Promise 拒绝未被正确处理,settings.tsx 中的调用方会导致虚假成功提示

settings.tsx:336 中,调用 useUserStore().saveSettingToSever() 后直接显示成功消息,但未 await 或 catch。当 Promise reject 时,错误会被忽略,用户会看到成功提示但实际保存失败。

对比 translate.tsx:28 正确使用了 await 的做法,需要统一调用方的处理:

  • settings.tsx 应在 onClick 中使用 async/await,并在 catch 分支中提示失败
  • 或使用 .catch() 处理错误,仅在成功时显示提示

建议修改 settings.tsx:336 的逻辑为先等待结果再根据成功/失败分别提示。

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@web/src/store/modules/useUserStore.ts` around lines 226 - 231, The caller in
settings.tsx currently calls useUserStore().saveSettingToSever() without
awaiting or handling rejection, causing false success notifications; update the
onClick handler in settings.tsx (the code that triggers saveSettingToSever) to
either: make the handler async and await useUserStore().saveSettingToSever()
inside a try/catch and show success only after await succeeds and show an error
notification in catch, or keep the handler synchronous but chain .then(() =>
show success).catch(err => show failure) so rejected Promises from
saveSettingToSever are properly handled and do not display a success message
erroneously.

@zds-s zds-s enabled auto-merge (squash) March 31, 2026 05:33
@zds-s zds-s disabled auto-merge March 31, 2026 05:33
@zds-s zds-s merged commit 06a8155 into mineadmin:master Mar 31, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement 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.

2 participants