You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

71 lines
2.2 KiB

// src/store/index.js
import {defineStore} from 'pinia'
export const useAdminStore = defineStore('admin', {
state: () => ({
adminData: null, // 用户信息
menuTree: [], // 菜单权限树
marketList: {}, // 市场列表
flag: 0, //员工数据开关状态,0=不包含员工数据,1=包含员工数据
}),
actions: {
// 设置用户信息并同步到localStorage
setAdminData(info) {
this.adminData = info
localStorage.setItem('adminData', JSON.stringify(info))
},
// 设置菜单树并同步到localStorage
setMenuTree(tree) {
this.menuTree = tree
localStorage.setItem('menuTree', JSON.stringify(tree))
},
setMarketList(list) {
this.marketList = list
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)
}
if (menuTree) {
this.menuTree = JSON.parse(menuTree)
}
if (marketList) {
this.marketList = JSON.parse(marketList)
}
if (flag) {
this.flag = JSON.parse(flag)
}
},
// 清空状态并移除localStorage数据
clearState() {
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')
}
}
})