diff --git a/src/store/index.js b/src/store/index.js index de91a35..cab0bfe 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -6,6 +6,7 @@ export const useAdminStore = defineStore('admin', { adminData: null, // 用户信息 menuTree: [], // 菜单权限树 marketList: {}, // 市场列表 + flag: 0, //员工数据开关状态,0=不包含员工数据,1=包含员工数据 }), actions: { // 设置用户信息并同步到localStorage @@ -24,11 +25,18 @@ export const useAdminStore = defineStore('admin', { localStorage.setItem('marketList', JSON.stringify(list)) }, + // 设置员工数据开关状态(使用数字0和1) + setFlag(flag) { + this.flag = flag + localStorage.setItem('flag', JSON.stringify(flag)) + }, + // 从localStorage初始化数据 initFromLocalStorage() { const adminData = localStorage.getItem('adminData') const menuTree = localStorage.getItem('menuTree') const marketList = localStorage.getItem('marketList') + const flag = localStorage.getItem('flag') if (adminData) { this.adminData = JSON.parse(adminData) @@ -41,6 +49,10 @@ export const useAdminStore = defineStore('admin', { if (marketList) { this.marketList = JSON.parse(marketList) } + + if (flag) { + this.flag = JSON.parse(flag) + } }, // 清空状态并移除localStorage数据 @@ -48,9 +60,11 @@ export const useAdminStore = defineStore('admin', { this.adminData = null this.menuTree = [] this.marketList = {} + this.flag = 0 localStorage.removeItem('adminData') localStorage.removeItem('menuTree') localStorage.removeItem('marketList') + localStorage.removeItem('flag') // localStorage.removeItem('token') } } diff --git a/src/views/home.vue b/src/views/home.vue index b692dfe..5910196 100644 --- a/src/views/home.vue +++ b/src/views/home.vue @@ -79,7 +79,7 @@ const menuList = ref([]) // 获取仓库实例 const adminStore = useAdminStore() // 解构状态(保持响应式) 获得 adminData(用户信息) 和 menuTree(菜单树) -const { adminData, menuTree } = storeToRefs(adminStore) +const { adminData, menuTree, flag } = storeToRefs(adminStore) // 筛选权限菜单 ,menuTree 是组件通信拿的 menuList.value = filterMenu(menuTree.value) @@ -156,6 +156,13 @@ function logout() { ElMessage.success('退出成功') } +// 切换员工数据开关状态 +const toggleFlag = () => { + const newFlag = flag.value === 1 ? 0 : 1 + adminStore.setFlag(newFlag) + ElMessage.success(newFlag === 1 ? '员工数据已隐藏' : '员工数据已显示') + console.log('flag',newFlag) +} @@ -243,6 +250,10 @@ function logout() {