| | |
| | | const useUserStore = defineStore("user", { |
| | | state: () => ({ |
| | | token: getToken(), |
| | | id: "", |
| | | id: "", |
| | | name: "", |
| | | avatar: "", |
| | | currentFactoryName: "", |
| | | nickName: "", |
| | | roleName: "", |
| | | currentDeptId: "", |
| | | currentLoginTime: "", |
| | | currentFactoryName: "", |
| | | nickName: "", |
| | | roleName: "", |
| | | currentDeptId: "", |
| | | currentLoginTime: "", |
| | | roles: Array(), |
| | | permissions: [], |
| | | }), |
| | | 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) => { |
| | | setToken(res.token) |
| | | this.token = res.token |
| | | resolve(null) |
| | | }).catch((error: any) => { |
| | | reject(error) |
| | | }) |
| | | }) |
| | | }, |
| | | // 部门登录 |
| | | 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) => { |
| | | setToken(res.token) |
| | | this.token = res.token |
| | | resolve(null) |
| | | }).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 |
| | | } 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 = user.roles[0].roleName |
| | | this.currentDeptId = user.tenantId |
| | | this.currentLoginTime = this.getCurrentTime() |
| | | 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 |
| | | } 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 = user.roles[0].roleName |
| | | this.currentDeptId = user.tenantId |
| | | this.currentLoginTime = this.getCurrentTime() |
| | | resolve(res); |
| | | }) |
| | | .catch((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}`; |
| | | }, |
| | | 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}`; |
| | | }, |
| | | }, |
| | | }); |
| | | |