|
@@ -14,27 +14,20 @@ export interface User {
|
|
|
|
|
|
|
|
export const useAuthStore = defineStore("auth", () => {
|
|
export const useAuthStore = defineStore("auth", () => {
|
|
|
const USER_KEY = "user"
|
|
const USER_KEY = "user"
|
|
|
- const errors = ref({})
|
|
|
|
|
const user = ref<User | null>(JSON.parse(storages.getItem(USER_KEY) ?? "{}"))
|
|
const user = ref<User | null>(JSON.parse(storages.getItem(USER_KEY) ?? "{}"))
|
|
|
const isAuthenticated = ref(!!JwtService.getToken())
|
|
const isAuthenticated = ref(!!JwtService.getToken())
|
|
|
|
|
|
|
|
function setAuth(authUser: User) {
|
|
function setAuth(authUser: User) {
|
|
|
isAuthenticated.value = true
|
|
isAuthenticated.value = true
|
|
|
- errors.value = {}
|
|
|
|
|
user.value = authUser
|
|
user.value = authUser
|
|
|
console.log("AUTHUSER", authUser)
|
|
console.log("AUTHUSER", authUser)
|
|
|
storages.setItem(USER_KEY, JSON.stringify(authUser))
|
|
storages.setItem(USER_KEY, JSON.stringify(authUser))
|
|
|
JwtService.saveToken(authUser.token)
|
|
JwtService.saveToken(authUser.token)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function setError(error: any) {
|
|
|
|
|
- errors.value = { ...error }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
function purgeAuth() {
|
|
function purgeAuth() {
|
|
|
isAuthenticated.value = false
|
|
isAuthenticated.value = false
|
|
|
user.value = null
|
|
user.value = null
|
|
|
- errors.value = []
|
|
|
|
|
storages.removeItem(USER_KEY)
|
|
storages.removeItem(USER_KEY)
|
|
|
appStore.menuStore.removeMenus()
|
|
appStore.menuStore.removeMenus()
|
|
|
JwtService.destroyToken()
|
|
JwtService.destroyToken()
|
|
@@ -42,22 +35,28 @@ export const useAuthStore = defineStore("auth", () => {
|
|
|
|
|
|
|
|
function login(userName: string, password: string) {
|
|
function login(userName: string, password: string) {
|
|
|
purgeAuth()
|
|
purgeAuth()
|
|
|
|
|
+ //console.log("LOGIN")
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
- apis.authApi.login({ userName: userName, password: password }).then(({ data }) => {
|
|
|
|
|
- if (data) {
|
|
|
|
|
- const result = data
|
|
|
|
|
- setAuth({
|
|
|
|
|
- userName: result.authName || "",
|
|
|
|
|
- userType: result.userType || "0",
|
|
|
|
|
- orgId: result.enterpriseId || "",
|
|
|
|
|
- token: result.auth,
|
|
|
|
|
- })
|
|
|
|
|
- resolve(result.loginMsg)
|
|
|
|
|
- } else {
|
|
|
|
|
- setError(data.msg)
|
|
|
|
|
- reject(data)
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ apis.authApi
|
|
|
|
|
+ .login({ userName: userName, password: password })
|
|
|
|
|
+ .then(({ data }) => {
|
|
|
|
|
+ if (data) {
|
|
|
|
|
+ const result = data
|
|
|
|
|
+ setAuth({
|
|
|
|
|
+ userName: result.authName || "",
|
|
|
|
|
+ userType: result.userType || "0",
|
|
|
|
|
+ orgId: result.enterpriseId || "",
|
|
|
|
|
+ token: result.auth,
|
|
|
|
|
+ })
|
|
|
|
|
+ resolve(result.loginMsg)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ reject(data.msg)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch(() => {
|
|
|
|
|
+ //console.log("ERROR_LOGIN", e)
|
|
|
|
|
+ reject("用户名或密码错误!")
|
|
|
|
|
+ })
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -74,7 +73,6 @@ export const useAuthStore = defineStore("auth", () => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
return {
|
|
|
- errors,
|
|
|
|
|
user,
|
|
user,
|
|
|
isAuthenticated,
|
|
isAuthenticated,
|
|
|
login,
|
|
login,
|