Przeglądaj źródła

Update 优化重新登录及注销功能

Yue 8 miesięcy temu
rodzic
commit
806a7c8a0d

+ 7 - 5
UI/VAP_V3.VUE/src/core/services/RequestService.ts

@@ -143,7 +143,8 @@ Rs.interceptors.response.use(
 		const code = res.data.code || HttpStatus.SUCCESS
 		const data = res.data
 		// 获取错误信息
-		const msg = errorCode[code] || res.data.msg || errorCode.default
+		let msg = errorCode[code] || res.data.msg || errorCode.default
+
 		// 二进制数据则直接返回
 		if (res.request.responseType === "blob" || res.request.responseType === "arraybuffer") {
 			return data
@@ -159,9 +160,7 @@ Rs.interceptors.response.use(
 					})
 					.then(() => {
 						isReLogin.show = false
-						appStore.authStore.logout().then(() => {
-							location.href = import.meta.env.VITE_APP_CONTEXT_PATH + "home"
-						})
+						appStore.authStore.logout()
 					})
 					.catch(() => {
 						isReLogin.show = false
@@ -172,8 +171,11 @@ Rs.interceptors.response.use(
 			if (config.errorAlert) {
 				msgUtil.msgError("没有权限,请联系管理员!")
 			}
-			return Promise.reject(msg)
+			return Promise.reject("没有权限,请联系管理员!")
 		} else if (code === HttpStatus.SERVER_ERROR) {
+			if (!import.meta.env.DEV) {
+				msg = "服务器异常,请联系管理员!"
+			}
 			if (config.errorAlert) {
 				msgUtil.msgError(msg)
 			}

+ 3 - 2
UI/VAP_V3.VUE/src/layouts/main/header/navbar/UserAccountMenu.vue

@@ -2,6 +2,7 @@
 import appStore from "@/stores"
 import apis from "@a"
 import VbWorkflow from "@/layouts/main/header/navbar/UserSubMenuWorkflow.vue"
+import router from "@r"
 
 const user = appStore.authStore.user
 const dynamic = computed(() => {
@@ -19,8 +20,8 @@ function refreshConfig() {
 	})
 }
 function signOut() {
-	appStore.authStore.logout().then(() => {
-		location.href = import.meta.env.VITE_APP_CONTEXT_PATH + "home"
+	appStore.authStore.logout(false).then(() => {
+		router.push("/login")
 	})
 }
 </script>

+ 3 - 3
UI/VAP_V3.VUE/src/router/index.ts

@@ -145,9 +145,9 @@ router.beforeEach((to, _from, next) => {
 					})
 					.catch((err) => {
 						isReLogin.show = false
-						authStore.logout().then(() => {
+						authStore.logout(false).then(() => {
 							message.msgError(err)
-							next({ path: import.meta.env.VITE_APP_CONTEXT_PATH + "home" })
+							router.push("/login")
 						})
 					})
 				appStore.appConfigStore.loadConfig()
@@ -161,7 +161,7 @@ router.beforeEach((to, _from, next) => {
 			// 在免登录白名单,直接进入
 			next()
 		} else {
-			next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
+			next(`/login?redirect=${encodeURIComponent(to.fullPath)}`) // 否则全部重定向到登录页
 			NProgressClose()
 		}
 	}

+ 5 - 1
UI/VAP_V3.VUE/src/stores/_auth.ts

@@ -4,6 +4,7 @@ import apis from "@/api"
 import type { User } from "@@/types/User"
 import type { LoginData } from "@@/types/Account"
 import { use } from "echarts"
+import router from "@r"
 
 export const useAuthStore = defineStore("auth", () => {
 	const errors = ref({})
@@ -105,12 +106,15 @@ export const useAuthStore = defineStore("auth", () => {
 		})
 	}
 	// 退出系统
-	function logout() {
+	function logout(isJump = true) {
 		return new Promise((resolve, reject) => {
 			apis.loginApi
 				.logout()
 				.then(() => {
 					purgeAuth()
+					if (isJump) {
+						router.push(`/login?redirect=${encodeURIComponent(window.location.pathname)}`)
+					}
 					resolve(true)
 				})
 				.catch((error) => {