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

61 lines
951 B
TypeScript

import { acceptHMRUpdate, defineStore } from 'pinia';
interface BasicUserInfo {
[key: string]: any;
/**
* 头像
*/
avatar: string;
/**
* 用户昵称
*/
nickname: string;
/**
* 用户 id
*/
userId: string;
/**
* 用户名
*/
username: string;
/**
* 用户邮箱
*/
email?: string;
}
interface AccessState {
/**
* 用户信息
*/
userInfo: BasicUserInfo | null;
/**
* 用户角色
*/
userRoles: string[];
}
/**
* @zh_CN 用户信息相关
*/
export const useUserStore = defineStore('core-user', {
actions: {
setUserInfo(userInfo: BasicUserInfo | null) {
this.userInfo = userInfo;
},
setUserRoles(roles: string[]) {
this.userRoles = roles;
},
},
state: (): AccessState => ({
userInfo: null,
userRoles: [],
})
});
// 解决热更新问题
const hot = import.meta.hot;
if (hot) {
hot.accept(acceptHMRUpdate(useUserStore, hot));
}