fix: 修复前端登录错误
This commit is contained in:
@@ -1,18 +1,11 @@
|
||||
<script setup lang="ts">
|
||||
import GlobalProviders from "@/components/GlobalProviders.vue";
|
||||
import Layout from "@/components/Layout.vue";
|
||||
import { useAuthKey } from "@/services/auth";
|
||||
import { computed } from "vue";
|
||||
|
||||
const authKey = useAuthKey();
|
||||
const isLoggedIn = computed(() => !!authKey.value);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<global-providers>
|
||||
<div id="app-root">
|
||||
<layout v-if="isLoggedIn" key="layout" />
|
||||
<router-view v-else key="auth" />
|
||||
<router-view />
|
||||
</div>
|
||||
</global-providers>
|
||||
</template>
|
||||
|
@@ -1,26 +1,33 @@
|
||||
import { useAuthService } from "@/services/auth";
|
||||
import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router";
|
||||
import Layout from "@/components/Layout.vue";
|
||||
|
||||
const routes: Array<RouteRecordRaw> = [
|
||||
{
|
||||
path: "/",
|
||||
name: "dashboard",
|
||||
component: () => import("@/views/Dashboard.vue"),
|
||||
},
|
||||
{
|
||||
path: "/keys",
|
||||
name: "keys",
|
||||
component: () => import("@/views/Keys.vue"),
|
||||
},
|
||||
{
|
||||
path: "/logs",
|
||||
name: "logs",
|
||||
component: () => import("@/views/Logs.vue"),
|
||||
},
|
||||
{
|
||||
path: "/settings",
|
||||
name: "settings",
|
||||
component: () => import("@/views/Settings.vue"),
|
||||
component: Layout,
|
||||
children: [
|
||||
{
|
||||
path: "",
|
||||
name: "dashboard",
|
||||
component: () => import("@/views/Dashboard.vue"),
|
||||
},
|
||||
{
|
||||
path: "keys",
|
||||
name: "keys",
|
||||
component: () => import("@/views/Keys.vue"),
|
||||
},
|
||||
{
|
||||
path: "logs",
|
||||
name: "logs",
|
||||
component: () => import("@/views/Logs.vue"),
|
||||
},
|
||||
{
|
||||
path: "settings",
|
||||
name: "settings",
|
||||
component: () => import("@/views/Settings.vue"),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: "/login",
|
||||
|
@@ -45,9 +45,11 @@ http.interceptors.response.use(
|
||||
// The request was made and the server responded with a status code
|
||||
// that falls out of the range of 2xx
|
||||
if (error.response.status === 401) {
|
||||
const { logout } = useAuthService();
|
||||
logout();
|
||||
window.location.href = "/login";
|
||||
if (window.location.pathname !== "/login") {
|
||||
const { logout } = useAuthService();
|
||||
logout();
|
||||
window.location.href = "/login";
|
||||
}
|
||||
}
|
||||
window.$message.error(error.response.data?.message || `请求失败: ${error.response.status}`);
|
||||
} else if (error.request) {
|
||||
|
@@ -22,8 +22,6 @@ const handleLogin = async () => {
|
||||
loading.value = false;
|
||||
if (success) {
|
||||
router.push("/");
|
||||
} else {
|
||||
message.error("登录失败,请检查您的授权密钥");
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
Reference in New Issue
Block a user