Browse Source

add: 图标 使用id 切换语言动态菜单更改

milestone-20251125-多语言
lihui 2 days ago
parent
commit
f968197bf8
  1. 92
      src/components/dialogs/LanguageSwitch.vue
  2. 22
      src/views/home.vue

92
src/components/dialogs/LanguageSwitch.vue

@ -1,24 +1,24 @@
<template> <template>
<el-dialog <el-dialog
v-model="dialogVisible"
title="语言切换"
width="300px"
:close-on-click-modal="false"
append-to-body
class="lang-switch-dialog"
v-model="dialogVisible"
title="语言切换"
width="300px"
:close-on-click-modal="false"
append-to-body
class="lang-switch-dialog"
> >
<el-form label-width="80px"> <el-form label-width="80px">
<el-form-item label="切换语言"> <el-form-item label="切换语言">
<el-select <el-select
v-model="tempLang"
placeholder="请选择语言"
style="width: 100%"
v-model="tempLang"
placeholder="请选择语言"
style="width: 100%"
> >
<el-option <el-option
v-for="item in langOptions"
:key="item.value"
:label="item.label"
:value="item.value"
v-for="item in langOptions"
:key="item.value"
:label="item.label"
:value="item.value"
/> />
</el-select> </el-select>
</el-form-item> </el-form-item>
@ -34,21 +34,15 @@
</template> </template>
<script setup> <script setup>
import { ref, computed } from 'vue'
import { useI18n } from 'vue-i18n'
import {computed, ref} from 'vue'
import {useI18n} from 'vue-i18n'
// === Element Plus === // === Element Plus ===
import {
ElDialog,
ElForm,
ElFormItem,
ElInput,
ElSelect,
ElOption,
ElButton,
ElMessage
} from 'element-plus'
const { locale } = useI18n()
import {ElButton, ElDialog, ElForm, ElFormItem, ElMessage, ElOption, ElSelect} from 'element-plus'
import request from "@/util/http.js";
import {useAdminStore} from '@/store/index.js';
import {storeToRefs} from "pinia";
const {locale} = useI18n()
// //
const dialogVisible = ref(false) const dialogVisible = ref(false)
@ -61,11 +55,11 @@ const tempLang = ref('')
// //
const langOptions = [ const langOptions = [
{ label: '中文(简体)', value: 'zh-CN' },
{ label: '中文(繁體)', value: 'zh-TW' },
{ label: '英语', value: 'en' },
{ label: 'ภาษาไทย', value: 'th' },
{ label: 'Tiếng Việt', value: 'vi' }
{label: '中文(简体)', value: 'zh-CN'},
{label: '中文(繁體)', value: 'zh-TW'},
{label: '英语', value: 'en'},
{label: 'ภาษาไทย', value: 'th'},
{label: 'Tiếng Việt', value: 'vi'}
] ]
// //
@ -80,13 +74,16 @@ const open = () => {
dialogVisible.value = true dialogVisible.value = true
} }
// //
const handleConfirm = () => {
const handleConfirm = async () => {
locale.value = tempLang.value locale.value = tempLang.value
localStorage.setItem('lang', tempLang.value) localStorage.setItem('lang', tempLang.value)
await getMenuTree()
ElMessage.success(`语言已切换为:${getLangLabel(tempLang.value)}`) ElMessage.success(`语言已切换为:${getLangLabel(tempLang.value)}`)
dialogVisible.value = false dialogVisible.value = false
// //
setTimeout(() => { setTimeout(() => {
window.location.reload() window.location.reload()
@ -96,6 +93,33 @@ const handleConfirm = () => {
defineExpose({ defineExpose({
open open
}) })
const adminStore = useAdminStore();
const {adminData} = storeToRefs(adminStore);
//
const getMenuTree = async function () {
//
try {
const result = await request({
url: '/menu/tree',
data: {
id: adminData.value.roleId,
}
})
if (result.code === 200){
adminStore.setMenuTree(result.data)
}
return result.data //
} catch (error) {
console.error('菜单数据请求失败:', error)
// return { code: 500, msg: '' }
ElMessage.error('网络异常')
adminStore.clearState()
}
}
</script> </script>

22
src/views/home.vue

@ -22,17 +22,17 @@ import {useMessageStore} from '@/store/index.js'
const icons = import.meta.glob('@/assets/SvgIcons/*.svg', {eager: true}) const icons = import.meta.glob('@/assets/SvgIcons/*.svg', {eager: true})
const menuNameMap = { const menuNameMap = {
'工作台': 'workbench',
'金币管理': 'gold-management',
'现金管理': 'cash-management',
'活动管理': 'activity-management',
'频道管理': 'channel-management',
'权限管理': 'permission-management',
'多语言配置': 'mutiple-language',
'2': 'workbench', //
'3': 'gold-management', //
'58': 'cash-management', //
'119': 'activity-management', //
'124': 'channel-management', //
'128': 'permission-management', //
'146': 'mutiple-language', //
} }
const getIconPath = (menuName) => {
const englishName = menuNameMap[menuName] || menuName;
const getIconPath = (menuNameId) => {
const englishName = menuNameMap[menuNameId] || menuNameId;
const possibleKeys = [ const possibleKeys = [
`@/assets/SvgIcons/${englishName}.svg`, `@/assets/SvgIcons/${englishName}.svg`,
@ -314,7 +314,7 @@ onMounted(() => getMessage())
<el-sub-menu v-if="menu.children && menu.children.length > 0" :index="menu.id.toString()"> <el-sub-menu v-if="menu.children && menu.children.length > 0" :index="menu.id.toString()">
<template #title> <template #title>
<img <img
:src="getIconPath(menu.menuName)"
:src="getIconPath(menu.id)"
:alt="`${menu.menuName}图标`" :alt="`${menu.menuName}图标`"
style="width: 4vh; height: 4vh; margin-right: 4px;" style="width: 4vh; height: 4vh; margin-right: 4px;"
> >
@ -355,7 +355,7 @@ onMounted(() => getMessage())
<!-- 无子菜单的一级菜单 --> <!-- 无子菜单的一级菜单 -->
<el-menu-item v-else :index="getRoutePath(menu)"> <el-menu-item v-else :index="getRoutePath(menu)">
<img <img
:src="getIconPath(menu.menuName)"
:src="getIconPath(menu.id)"
:alt="`${menu.menuName}图标`" :alt="`${menu.menuName}图标`"
style="width: 4vh; height: 4vh; margin-right: 4px;" style="width: 4vh; height: 4vh; margin-right: 4px;"
> >

Loading…
Cancel
Save