gaoluyang
3 天以前 0333d66e4b397c161c6a44ce1e2a121c2cc41082
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
import { logout, getInfo, loginCheckFactory } from "@/api/login";
import { getRouters as getRoutersApi } from "@/api/menu";
import { getToken, setToken, removeToken } from "@/utils/auth";
import defAva from "@/static/images/profile.jpg";
import { defineStore } from "pinia";
import config from "@/config.js";
 
export interface LoginForm {
  userName: string;
  password: string;
}
 
const useUserStore = defineStore("user", {
  state: () => ({
    token: getToken(),
    id: "",
    name: "",
    avatar: "",
    currentFactoryName: "",
    nickName: "",
    roleName: "",
    currentDeptId: "",
    currentLoginTime: "",
    roles: Array(),
    permissions: [],
    routers: [],
  }),
  actions: {
    loginCheckFactory(userInfo: any) {
      const userName = userInfo.userName.trim();
      const password = userInfo.password;
      const factoryId = userInfo.factoryId;
      return new Promise((resolve, reject) => {
        loginCheckFactory(userName, password, factoryId)
          .then((res: any) => {
            const token = res.token || res.data?.token;
            if (token) {
              setToken(token);
              this.token = token;
              resolve(null);
            } else {
              reject("未获取到登录令牌");
            }
          })
          .catch((error: any) => {
            reject(error);
          });
      });
    },
    getInfo() {
      return new Promise((resolve, reject) => {
        getInfo()
          .then((res: any) => {
            // 兼容 res.data 结构
            const data = res.data || res;
            const user = data.user || {};
            let avatar = user.avatar || "";
            avatar = config.baseUrl + "/profile/" + avatar;
            if (data.roles && data.roles.length > 0) {
              // 验证返回的roles是否是一个非空数组
              this.roles = data.roles;
              this.permissions = data.permissions;
            } else {
              this.roles = ["ROLE_DEFAULT"];
            }
            this.id = user.userId || "";
            this.name = user.userName || "";
            this.avatar = avatar;
            this.currentFactoryName = user.currentFactoryName || "";
            this.nickName = user.nickName || "";
            this.roleName = Array.isArray(user.roles) && user.roles.length > 0 ? user.roles[0].roleName || "" : "";
            this.currentDeptId = user.tenantId || "";
            this.currentLoginTime = this.getCurrentTime();
            resolve(data);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    logOut() {
      return new Promise<null>((resolve, reject) => {
        logout()
          .then(() => {
            this.token = "";
            this.roles = [];
            this.permissions = [];
            this.routers = [];
            this.name = "";
            this.avatar = "";
            removeToken();
            resolve(null);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    getCurrentTime() {
      const now = new Date();
      const year = now.getFullYear(); // 获取年份
      const month = String(now.getMonth() + 1).padStart(2, "0"); // 月份从0开始,要+1,并补零
      const day = String(now.getDate()).padStart(2, "0"); // 日期补零
      const hours = String(now.getHours()).padStart(2, "0"); // 小时补零
      const minutes = String(now.getMinutes()).padStart(2, "0"); // 分钟补零
      const seconds = String(now.getSeconds()).padStart(2, "0"); // 秒数补零
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    getRouters() {
      return new Promise((resolve, reject) => {
        getRoutersApi()
          .then((res: any) => {
            // 存储路由权限数据
            this.routers = res.data || [];
            resolve(res);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
  },
});
 
export default useUserStore;