| 对比新文件 |
| | |
| | | import { logout, getInfo, loginCheckFactory } from "@/api/login"; |
| | | import { getToken, setToken, removeToken } from "@/utils/auth"; |
| | | 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; |
| | | const password = userInfo.password; |
| | | 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); |
| | | }); |
| | | }); |
| | | }, |
| | | 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) { |
| | | 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) => { |
| | | 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"); |
| | | 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) => { |
| | | this.routers = []; |
| | | resolve({ data: [] }); |
| | | }); |
| | | }, |
| | | }, |
| | | }); |
| | | |
| | | export default useUserStore; |