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.
45 lines
1.3 KiB
45 lines
1.3 KiB
// src/store/index.js
|
|
import { defineStore } from 'pinia'
|
|
|
|
export const useAdminStore = defineStore('admin', {
|
|
state: () => ({
|
|
adminData: null, // 用户信息
|
|
menuTree: null, // 菜单权限树
|
|
}),
|
|
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))
|
|
},
|
|
|
|
// 从localStorage初始化数据
|
|
initFromLocalStorage() {
|
|
const adminData = localStorage.getItem('adminData')
|
|
const menuTree = localStorage.getItem('menuTree')
|
|
|
|
if (adminData) {
|
|
this.adminData = JSON.parse(adminData)
|
|
}
|
|
|
|
if (menuTree) {
|
|
this.menuTree = JSON.parse(menuTree)
|
|
}
|
|
},
|
|
|
|
// 清空状态并移除localStorage数据
|
|
clearState() {
|
|
this.adminData = null
|
|
this.menuTree = null
|
|
localStorage.removeItem('adminData')
|
|
localStorage.removeItem('menuTree')
|
|
// localStorage.removeItem('token')
|
|
}
|
|
}
|
|
})
|