admin-vben5/packages/stores/src/modules/user.ts

73 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-07-30 21:10:28 +08:00
import { acceptHMRUpdate, defineStore } from 'pinia';
interface BasicUserInfo {
[key: string]: any;
2024-07-30 21:10:28 +08:00
/**
*
*/
avatar: string;
/**
*
*/
email: string;
2024-08-07 08:57:56 +08:00
/**
*
*/
permissions: string[];
2024-07-30 21:10:28 +08:00
/**
*
*/
realName: string;
/**
*
*/
2024-08-07 08:57:56 +08:00
roles: string[];
2024-07-30 21:10:28 +08:00
/**
* id
*/
2024-08-07 08:57:56 +08:00
userId: number | string;
2024-07-30 21:10:28 +08:00
/**
*
*/
username: string;
}
interface AccessState {
/**
*
*/
userInfo: BasicUserInfo | null;
/**
*
*/
userRoles: string[];
}
/**
* @zh_CN
*/
export const useUserStore = defineStore('core-user', {
actions: {
setUserInfo(userInfo: BasicUserInfo | null) {
// 设置用户信息
this.userInfo = userInfo;
// 设置角色信息
const roles = userInfo?.roles ?? [];
this.setUserRoles(roles);
},
setUserRoles(roles: string[]) {
this.userRoles = roles;
},
},
state: (): AccessState => ({
userInfo: null,
userRoles: [],
}),
});
// 解决热更新问题
const hot = import.meta.hot;
if (hot) {
hot.accept(acceptHMRUpdate(useUserStore, hot));
}