feat: Element Plus 语言随 i18n 切换;用户侧登录/禁用提示接入 i18n

This commit is contained in:
Wangnov
2025-09-10 18:04:59 +08:00
parent 482eb7c8f7
commit 022724336b
2 changed files with 24 additions and 7 deletions

View File

@@ -1,25 +1,39 @@
<template>
<div id="app">
<router-view />
<el-config-provider :locale="elLocale">
<router-view />
<!-- 全局组件 -->
<ToastNotification ref="toastRef" />
<ConfirmDialog ref="confirmRef" />
<!-- 全局组件 -->
<ToastNotification ref="toastRef" />
<ConfirmDialog ref="confirmRef" />
</el-config-provider>
</div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import { onMounted, ref, computed } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { useThemeStore } from '@/stores/theme'
import ToastNotification from '@/components/common/ToastNotification.vue'
import ConfirmDialog from '@/components/common/ConfirmDialog.vue'
import i18n from '@/i18n'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import zhTw from 'element-plus/dist/locale/zh-tw.mjs'
import en from 'element-plus/dist/locale/en.mjs'
const authStore = useAuthStore()
const themeStore = useThemeStore()
const toastRef = ref()
const confirmRef = ref()
// Element Plus 语言随 i18n 切换
const elLocale = computed(() => {
const l = i18n.global.locale.value
if (l === 'zh-tw') return zhTw
if (l === 'en') return en
return zhCn
})
onMounted(() => {
// 初始化主题
themeStore.initTheme()

View File

@@ -1,5 +1,6 @@
import { defineStore } from 'pinia'
import axios from 'axios'
import i18n from '@/i18n'
import { showToast } from '@/utils/toast'
const API_BASE = '/users'
@@ -40,7 +41,7 @@ export const useUserStore = defineStore('user', {
return response.data
} else {
throw new Error(response.data.message || 'Login failed')
throw new Error(response.data.message || i18n.global.t('user.login.loginFailed'))
}
} catch (error) {
this.clearAuth()
@@ -115,7 +116,9 @@ export const useUserStore = defineStore('user', {
this.clearAuth()
// If it's a disabled account error, throw a specific error
if (error.response?.status === 403) {
throw new Error(error.response.data?.message || 'Your account has been disabled')
throw new Error(
error.response.data?.message || i18n.global.t('user.login.accountDisabled')
)
}
}
throw error