fix: 修复前端登录错误

This commit is contained in:
tbphp
2025-07-15 22:01:26 +08:00
parent 06525f3857
commit d3f0861de4
4 changed files with 30 additions and 30 deletions

View File

@@ -1,18 +1,11 @@
<script setup lang="ts"> <script setup lang="ts">
import GlobalProviders from "@/components/GlobalProviders.vue"; 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> </script>
<template> <template>
<global-providers> <global-providers>
<div id="app-root"> <div id="app-root">
<layout v-if="isLoggedIn" key="layout" /> <router-view />
<router-view v-else key="auth" />
</div> </div>
</global-providers> </global-providers>
</template> </template>

View File

@@ -1,27 +1,34 @@
import { useAuthService } from "@/services/auth"; import { useAuthService } from "@/services/auth";
import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router"; import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router";
import Layout from "@/components/Layout.vue";
const routes: Array<RouteRecordRaw> = [ const routes: Array<RouteRecordRaw> = [
{ {
path: "/", path: "/",
component: Layout,
children: [
{
path: "",
name: "dashboard", name: "dashboard",
component: () => import("@/views/Dashboard.vue"), component: () => import("@/views/Dashboard.vue"),
}, },
{ {
path: "/keys", path: "keys",
name: "keys", name: "keys",
component: () => import("@/views/Keys.vue"), component: () => import("@/views/Keys.vue"),
}, },
{ {
path: "/logs", path: "logs",
name: "logs", name: "logs",
component: () => import("@/views/Logs.vue"), component: () => import("@/views/Logs.vue"),
}, },
{ {
path: "/settings", path: "settings",
name: "settings", name: "settings",
component: () => import("@/views/Settings.vue"), component: () => import("@/views/Settings.vue"),
}, },
],
},
{ {
path: "/login", path: "/login",
name: "login", name: "login",

View File

@@ -45,10 +45,12 @@ http.interceptors.response.use(
// The request was made and the server responded with a status code // The request was made and the server responded with a status code
// that falls out of the range of 2xx // that falls out of the range of 2xx
if (error.response.status === 401) { if (error.response.status === 401) {
if (window.location.pathname !== "/login") {
const { logout } = useAuthService(); const { logout } = useAuthService();
logout(); logout();
window.location.href = "/login"; window.location.href = "/login";
} }
}
window.$message.error(error.response.data?.message || `请求失败: ${error.response.status}`); window.$message.error(error.response.data?.message || `请求失败: ${error.response.status}`);
} else if (error.request) { } else if (error.request) {
// The request was made but no response was received // The request was made but no response was received

View File

@@ -22,8 +22,6 @@ const handleLogin = async () => {
loading.value = false; loading.value = false;
if (success) { if (success) {
router.push("/"); router.push("/");
} else {
message.error("登录失败,请检查您的授权密钥");
} }
}; };
</script> </script>