zhangwencui
10 天以前 4c2836116a7ea09ec7f237b02fb0410832794240
src/store/modules/user.ts
@@ -1,9 +1,9 @@
import {logout, getInfo, loginCheckFactory} from "@/api/login";
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'
import config from "@/config.js";
export interface LoginForm {
  userName: string;
@@ -28,43 +28,54 @@
  actions: {
    // 部门登录
    loginCheckFactory(userInfo: any) {
      const userName = userInfo.userName
      const password = userInfo.password
      const userName = userInfo.userName.trim();
      const password = userInfo.password;
      const factoryId = userInfo.factoryId;
      return new Promise((resolve, reject) => {
        loginCheckFactory(userName, password).then((res: any) => {
          setToken(res.token)
          this.token = res.token
          resolve(null)
        }).catch((error: any) => {
          reject(error)
        })
      })
        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) => {
            const user = res.user
            let avatar = user.avatar || ""
            avatar = config.baseUrl + '/profile/' + avatar
            if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
              this.roles = res.roles
              this.permissions = res.permissions
            // 兼容 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.roles = ["ROLE_DEFAULT"];
            }
            this.id = user.userId
            this.name = user.userName
            this.avatar = avatar
            this.currentFactoryName = user.currentFactoryName
            this.nickName = user.nickName
            this.roleName = user.roles[0].roleName
            this.currentDeptId = user.tenantId
            this.currentLoginTime = this.getCurrentTime()
            resolve(res);
            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) => {
          .catch(error => {
            reject(error);
          });
      });
@@ -83,19 +94,19 @@
            removeToken();
            resolve(null);
          })
          .catch((error) => {
          .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');  // 秒数补零
      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}`;
    },
    // 获取路由权限
@@ -107,7 +118,7 @@
            this.routers = res.data || [];
            resolve(res);
          })
          .catch((error) => {
          .catch(error => {
            reject(error);
          });
      });