Blame view

Yi.Vben5.Vue3/packages/stores/src/modules/user.ts 1.16 KB
515fceeb   “wangming”   框架初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  import { acceptHMRUpdate, defineStore } from 'pinia';
  
  interface BasicUserInfo {
    [key: string]: any;
    /**
     * 头像
     */
    avatar: string;
    /**
     * 邮箱
     */
    email: string;
    /**
     * 用户权限
     */
    permissions: string[];
    /**
     * 用户昵称
     */
    realName: string;
    /**
     * 用户角色
     */
    roles: string[];
    /**
     * 用户id
     */
    userId: number | string;
    /**
     * 用户名
     */
    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));
  }