yinbangzhong
2024-08-29 30c1eeca00527a1294c62b1c708edd32ba079b67
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { defineStore } from 'pinia';
import {
  login as userLogin,
  logout as userLogout,
  getUserInfo,
  LoginData,
} from '@/api/user';
import { setToken, clearToken, setUserInfo, setUserResources, clearUserResources } from "@/utils/auth";
import { removeRouteListener } from '@/utils/route-listener';
import { UserState } from './types';
import useAppStore from '../app';
import router from "@/router";
 
 
const useUserStore = defineStore('user', {
  state: (): UserState => ({
    name: undefined,
    avatar: undefined,
    job: undefined,
    organization: undefined,
    location: undefined,
    email: undefined,
    introduction: undefined,
    personalWebsite: undefined,
    jobName: undefined,
    organizationName: undefined,
    locationName: undefined,
    phone: undefined,
    registrationDate: undefined,
    accountId: undefined,
    certification: undefined,
    role: '',
    resources:undefined,
    hrefUrl:undefined
  }),
 
  getters: {
    userInfo(state: UserState): UserState {
      return { ...state };
    },
  },
 
  actions: {
    switchRoles() {
      return new Promise((resolve) => {
        this.role = this.role === 'user' ? 'admin' : 'user';
        resolve(this.role);
      });
    },
    // Set user's information
    setInfo(partial: Partial<UserState>) {
      this.$patch(partial);
    },
 
    // Reset user's information
    resetInfo() {
      clearUserResources()
      this.$reset();
    },
 
    // Get user's information
    async info() {
      const res = await getUserInfo();
 
      this.setInfo(res.data);
    },
 
    // Login
    async login(loginForm: LoginData):string {
      try {
        const res = await userLogin(loginForm);
 
        setToken(res.data.access_token);
       
        const userInfo = {
          avatar: res.data.avatar,
          name: res.data.userName,
          email: res.data.email,
          role: res.data.roles[0].roleKey,
        };
        this.name=res.data.userName
     
        if(res.data?.roles.length>0)
          //遍历roles
          for (const r of res.data.roles) {
            if (!(this.resources)) {
              this.resources = [];
            }
            this.resources = this.resources.concat(r.resources);
          }
            setUserInfo(JSON.stringify(userInfo));
            setUserResources(JSON.stringify(this.resources))
        for (const r of this.resources) {
          if (r.menuType == 0) {
            
            return r.component
          }
        }
      } catch (err) {
        clearToken();
        throw err;
      }
    },
    logoutCallBack() {
      const appStore = useAppStore();
      this.resetInfo();
      clearToken();
      removeRouteListener();
      appStore.clearServerMenu();
    },
    // Logout
    async logout() {
      try {
        await userLogout();
      } finally {
        router.push({
          name: "login",
        });
        this.logoutCallBack();
      }
    },
 
 
    getHreFurl(url: string) {
      this.hrefUrl=url
    },
  },
});
 
export default useUserStore;