zhangchen
2023-10-23 61b8b562f74691ae4dfe11008339450d0bc91ea1
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
import { defineStore } from 'pinia';
import { Session, Local } from '/@/utils/storage';
import { api_Login } from "/@/api/login";
import { api_get_userinfo } from "/@/api/user";
import { api_confirm_client } from "/@/api/client";
import { ElMessage } from 'element-plus';
 
/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUser = defineStore('user', {
    state: (): UserState => ({
        isFinallyToken: Local.get("isFinallyToken") || false, // 是否是最终token
        token: Session.get("token"),
        user: {
            // userName: Session.get()
            userName: Local.get("userName"),
            clientName: Local.get("clientName")
        }
    }),
    actions: {
        login(loginData: ILoginForm) {
            // eslint-disable-next-line no-async-promise-executor
            return new Promise<boolean>(async (resolve, reject) => {
                try {
                    const { data: token } = await api_Login(loginData);
                    Session.set("token", token);
                    const { data: userData } = await api_get_userinfo();
                    const { roles, clientInfos, userName } = userData;
                    Local.set("userName", userName);
                    this.user.userName = userName;
                    if (!roles || roles.length <= 0) {
                        // 当前用户没有被分配权限
                        Session.clear();
                        ElMessage.error("当前登录用户没有分配权限,请先分配权限后在登录");
                        reject();
                    } else {
 
                        // 判断当前账号是否是 admin 角色 或者当前账号下是否存在多个客户数据,两者其一为真则需要选择客户后然后再更新token
                        const isAdmin = roles.some((r: any) => r.roleName === "admin");
                        const isManyClient = clientInfos.length > 1;
 
                        if (isAdmin || isManyClient) {
                            resolve(true);
                        } else {
                            this.isFinallyToken = true;
                            Local.set("isFinallyToken", true);
                            // 增加客户名称
                            this.user.clientName = clientInfos[0].clientName;
                            Local.set("clientName", clientInfos[0].clientName);
                            resolve(false);
                        }
                    }
                } catch (error) {
                    reject(error);
                }
            });
        },
        clientLogin(code: string, clientName: string) {
            // eslint-disable-next-line no-async-promise-executor
            return new Promise<void>(async (resolve, reject) => {
                try {
                    const { data: token } = await api_confirm_client(code);
                    Session.set("token", token);
                    Local.set("isFinallyToken", true);
                    this.user.clientName = clientName;
                    Local.set("clientName", clientName);
                    resolve();
                } catch (error) {
                    reject(error);
                }
            });
        },
        logout() {
            return new Promise<void>((resolve) => {
                Session.clear();
                localStorage.clear();
                this.user.userName = "";
                this.user.clientName = "";
                this.isFinallyToken = false;
                this.token = "";
                resolve();
            });
        }
    },
});