92 lines
2.7 KiB
TypeScript
92 lines
2.7 KiB
TypeScript
|
import { defineStore } from "pinia";
|
||
|
import jwtDecode from "jwt-decode";
|
||
|
import api from "@/axios";
|
||
|
import { useTabsStore } from "@/stores/modules/tabsStore";
|
||
|
|
||
|
export interface UserInfo {
|
||
|
snowFlakeId: number;
|
||
|
name: string,
|
||
|
nickName: string
|
||
|
headImage: string;
|
||
|
telephone: string;
|
||
|
sex: number;
|
||
|
email: string;
|
||
|
extData: {
|
||
|
roleName: string
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export interface UserStore {
|
||
|
token: string;
|
||
|
userInfo: UserInfo;
|
||
|
authMenuList: RouterVo[];
|
||
|
}
|
||
|
|
||
|
export const useUserStore = defineStore({
|
||
|
id: "userStore",
|
||
|
state: (): UserStore => {
|
||
|
return {
|
||
|
sokentdata: undefined,
|
||
|
token: undefined,
|
||
|
userInfo: undefined,
|
||
|
authMenuList: [],
|
||
|
};
|
||
|
},
|
||
|
actions: {
|
||
|
async initAuthRouterList() {
|
||
|
const resp = await api.get<RouterVo[]>('/user/get/auth/menus')
|
||
|
this.authMenuList = resp.data;
|
||
|
},
|
||
|
saveUserInfo(token: string): void {
|
||
|
|
||
|
//保存用户token
|
||
|
this.token = token.session;
|
||
|
console.log("🚀 ~ saveUserInfo ~ this:", this)
|
||
|
//保存用户信息
|
||
|
// this.userInfo = jwtDecode<UserInfo>(token);
|
||
|
this.userInfo = token;
|
||
|
},
|
||
|
setwebsoketdata(data) {
|
||
|
this.sokentdata = data;
|
||
|
},
|
||
|
async resetUserInfo() {
|
||
|
this.token = undefined;
|
||
|
this.userInfo = undefined;
|
||
|
this.authMenuList = [];
|
||
|
useTabsStore().resetTab();
|
||
|
},
|
||
|
},
|
||
|
getters: {
|
||
|
getsoketData: (state): string => state.sokentdata,
|
||
|
getToken: (state): string => state.token,
|
||
|
getUserInfo: (state): UserInfo => state.userInfo,
|
||
|
getBreadcrumbList: state => getAllBreadcrumbList(state.authMenuList),
|
||
|
getFlatRouterList: state => getFlatRouterList(state.authMenuList),
|
||
|
},
|
||
|
persist: {
|
||
|
key: "user-stores",
|
||
|
storage: window.localStorage,
|
||
|
paths: ["token", "userInfo", 'keepAliveNames'],
|
||
|
},
|
||
|
});
|
||
|
|
||
|
/**
|
||
|
* 获取所有的面包屑
|
||
|
*/
|
||
|
const getAllBreadcrumbList = (menuList: RouterVo[], parent: Breadcrumb[] = [], result: { [key: string]: Breadcrumb[] } = {}) => {
|
||
|
for (const item of menuList) {
|
||
|
result[item.path] = [...parent, { path: item.path, label: item.meta.title, icon: item.meta.icon }];
|
||
|
if (item.children) getAllBreadcrumbList(item.children, result[item.path], result);
|
||
|
}
|
||
|
return result;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* 扁平化路由
|
||
|
* @param menuList
|
||
|
*/
|
||
|
const getFlatRouterList = (menuList: RouterVo[]): RouterVo[] => {
|
||
|
let newMenuList: RouterVo[] = JSON.parse(JSON.stringify(menuList));
|
||
|
return newMenuList.flatMap(item => [item, ...(item.children ? getFlatRouterList(item.children) : [])]);
|
||
|
}
|