import { defineStore, acceptHMRUpdate } from "pinia"; export const useStore = defineStore< string, SotreState, SoreGetters, SotreActions >("storeInfo", { state: () => ({ userInfo: {}, // 当前的用户信息 routers: [], // 返回的所有路由 asyncRoutesMark: false, // 是否添加过路由 personnelList: [], // 人员列表 }), getters: { getRoutersList() { return this.routers; }, getToken() { return this.userInfo?.id || ""; }, getPersonnelListItems(val) { let value = val; if(Array.isArray(val)) { value = val[0] } return this.personnelList.find(item => item.value == value) }, }, actions: { // 方法 setRouters(arr) { this.routers = arr; }, setAsyncRoutesMark(val) { this.asyncRoutesMark = val; }, setValue(val, key) { this[key] = val; }, getRouterConfig(path) { return this.routers.find((item: any) => item.path === path); }, getFunctionList(path) { const config = this.getRouterConfig(path); if (!config) { return []; } return config.functionList || []; }, getUserInfoVal(val) { return this.userInfo[val]; }, clearStore() { localStorage.clear(); sessionStorage.clear(); this.userInfo = {}; this.routers = []; this.personnelList = []; this.asyncRoutesMark = false; }, }, persist: { storage: sessionStorage, // 存储在 sessionStorage 中 }, // 是否持久化 }); if (import.meta.hot) { import.meta.hot.accept(acceptHMRUpdate(useStore, import.meta.hot)); }