支持语言切换时保存用户设置至服务器,修复多语言切换后刷新页面无法正确使用当前语言问题。#719
Conversation
📝 Walkthrough工作流程总结此次变更升级了语言切换流程以支持异步操作,包括从服务器获取应用设置、更新区域设置,并在更改后将设置持久化保存到服务器。同时优化了用户店中区域初始化的优先级顺序。 变更概览
序列流程图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: 完成
预估代码审查工作量🎯 2 (简单) | ⏱️ ~10 分钟 可能相关的PR
建议标签
建议审查者
庆祝诗
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
web/src/layouts/components/bars/toolbar/components/translate.tsxweb/src/store/modules/useUserStore.ts
| 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() | ||
| } |
There was a problem hiding this comment.
建议补充保存失败兜底,避免“本地已切换但服务端未持久化”无提示。
当前先更新本地语言再请求保存;若请求失败,用户侧没有明确反馈,后续刷新可能回退语言,体验上会像“切换失效”。建议在这里捕获异常并提示失败(必要时可按产品策略回滚本地状态)。
💡 建议修改
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);保留成功路径不变。
| return useHttp().post('/admin/permission/update', { backend_setting }).then(() => { | ||
| cache.set('sys_settings', backend_setting) | ||
| }).catch((error) => { | ||
| console.log(error) | ||
| return Promise.reject(error) | ||
| }) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# 目的:定位所有 saveSettingToSever 调用点,核对是否有 await / .catch 处理
rg -nP --type=ts --type=tsx '\bsaveSettingToSever\s*\(' -C2Repository: mineadmin/MineAdmin
Length of output: 90
🏁 Script executed:
#!/bin/bash
# 搜索所有 saveSettingToSever 调用点(修正:不指定不支持的 tsx 类型)
rg -n 'saveSettingToSever\s*\(' --type=ts -C 3Repository: mineadmin/MineAdmin
Length of output: 2069
🏁 Script executed:
#!/bin/bash
# 如果上面失败,尝试不用类型过滤
rg -n 'saveSettingToSever\s*\(' -C 3Repository: mineadmin/MineAdmin
Length of output: 2069
🏁 Script executed:
#!/bin/bash
# 查看已知问题位置 settings.tsx 附近的代码
fd -e tsx -e ts | xargs grep -l 'saveSettingToSever' | head -5Repository: 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.
如题。修复多语言切换后 刷新 2 遍页面会恢复默认语言的问题。还有刷新页面后请求后端 api 接口时。语言不同步问题
Summary by CodeRabbit
发布说明
改进
Bug 修复