| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import { acceptHMRUpdate, defineStore } from 'pinia';
- interface BasicUserInfo {
- [key: string]: any;
- /**
- * 头像
- */
- avatar: string;
- /**
- * 用户昵称
- */
- realName: string;
- /**
- * 用户角色
- */
- roles?: Array<string | { id: string; permissions: string[] }>;
- /**
- * 用户id
- */
- userId: string;
- /**
- * 用户名
- */
- username: string;
- }
- interface AccessState {
- /**
- * 用户信息
- */
- userInfo: BasicUserInfo | null;
- /**
- * 用户角色
- */
- userRoles: Array<string | { id: string; permissions: 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: BasicUserInfo['roles'] & {}) {
- this.userRoles = roles;
- },
- updateHomePath(path?: string) {
- this.userInfo = { ...this.userInfo, homePath: path } as BasicUserInfo;
- },
- },
- state: (): AccessState => ({
- userInfo: null,
- userRoles: [],
- }),
- });
- // 解决热更新问题
- const hot = import.meta.hot;
- if (hot) {
- hot.accept(acceptHMRUpdate(useUserStore, hot));
- }
|