Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ export default defineComponent({
const settingStore = useSettingStore()
const locales = userStore.getLocales()
const { locale, t } = useI18n()
function changeLanguage(item: { label: string, value: string }) {
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()
}
Comment on lines +20 to 29
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);保留成功路径不变。

return () => (

Expand Down
11 changes: 9 additions & 2 deletions web/src/store/modules/useUserStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,23 @@ const useUserStore = defineStore(

await nextTick()
useThemeColor().initThemeColor()
const locale = settings?.app?.useLocale ?? (language.value?.trim() || 'zh_CN')
const cacheLanguage = cache.get('language', '')?.trim?.() || ''
const settingsLanguage = settings?.app?.useLocale?.trim?.() || ''
const locale = cacheLanguage || settingsLanguage || 'zh_CN'
const appSettings = setting.getSettings('app')
if (appSettings) {
appSettings.useLocale = locale
}
setLanguage(locale)
}

function saveSettingToSever() {
const backend_setting = setting.getSettings()
useHttp().post('/admin/permission/update', { backend_setting }).then(() => {
return useHttp().post('/admin/permission/update', { backend_setting }).then(() => {
cache.set('sys_settings', backend_setting)
}).catch((error) => {
console.log(error)
return Promise.reject(error)
})
Comment on lines +226 to 231
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.

}

Expand Down
Loading