From eb5d68422f668b9e344c29d8e3780a285375ddd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BA=B8=E5=87=A4=E5=AD=A4=E5=87=B0?= <3100542635@qq.com> Date: Tue, 10 Dec 2024 16:59:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B7=AF=E7=94=B1=E5=AE=88=E5=8D=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- napcat.webui/src/backend/githubApi.ts | 10 ++++------ napcat.webui/src/main.ts | 2 +- napcat.webui/src/router/index.ts | 18 +++++++++++++++++- napcat.webui/src/utils/request.ts | 4 ++-- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/napcat.webui/src/backend/githubApi.ts b/napcat.webui/src/backend/githubApi.ts index c30e7b3f..d8f6d205 100644 --- a/napcat.webui/src/backend/githubApi.ts +++ b/napcat.webui/src/backend/githubApi.ts @@ -1,9 +1,7 @@ -import { request } from '@/utils/request.js'; - export class githubApiManager { public async GetBaseData(): Promise { try { - const ConfigResponse = await request('https://api.github.com/repos/NapNeko/NapCatQQ', { + const ConfigResponse = await fetch('https://api.github.com/repos/NapNeko/NapCatQQ', { method: 'GET', headers: { 'Content-Type': 'application/json', @@ -19,7 +17,7 @@ export class githubApiManager { } public async GetReleasesData(): Promise { try { - const ConfigResponse = await request('https://api.github.com/repos/NapNeko/NapCatQQ/releases', { + const ConfigResponse = await fetch('https://api.github.com/repos/NapNeko/NapCatQQ/releases', { method: 'GET', headers: { 'Content-Type': 'application/json', @@ -35,7 +33,7 @@ export class githubApiManager { } public async GetPullsData(): Promise { try { - const ConfigResponse = await request('https://api.github.com/repos/NapNeko/NapCatQQ/pulls', { + const ConfigResponse = await fetch('https://api.github.com/repos/NapNeko/NapCatQQ/pulls', { method: 'GET', headers: { 'Content-Type': 'application/json', @@ -51,7 +49,7 @@ export class githubApiManager { } public async GetContributors(): Promise { try { - const ConfigResponse = await request('https://api.github.com/repos/NapNeko/NapCatQQ/contributors', { + const ConfigResponse = await fetch('https://api.github.com/repos/NapNeko/NapCatQQ/contributors', { method: 'GET', headers: { 'Content-Type': 'application/json', diff --git a/napcat.webui/src/main.ts b/napcat.webui/src/main.ts index 6f337172..5c02091c 100644 --- a/napcat.webui/src/main.ts +++ b/napcat.webui/src/main.ts @@ -46,7 +46,7 @@ import { Loading as TLoading, HeadMenu as THeadMenu } from 'tdesign-vue-next'; -import { router } from './router'; +import router from './router'; import 'tdesign-vue-next/es/style/index.css'; const app = createApp(App); app.use(router); diff --git a/napcat.webui/src/router/index.ts b/napcat.webui/src/router/index.ts index 50f67c11..5883b925 100644 --- a/napcat.webui/src/router/index.ts +++ b/napcat.webui/src/router/index.ts @@ -7,6 +7,7 @@ import NetWork from '../pages/NetWork.vue'; import QQLogin from '../components/QQLogin.vue'; import WebUiLogin from '../components/WebUiLogin.vue'; import OtherConfig from '../pages/OtherConfig.vue'; +import { MessagePlugin } from 'tdesign-vue-next'; const routes: Array = [ { path: '/', redirect: '/webui' }, @@ -26,7 +27,22 @@ const routes: Array = [ }, ]; -export const router = createRouter({ +const router = createRouter({ history: createWebHashHistory(), routes, }); + +router.beforeEach((to, from, next) => { + const token = localStorage.getItem('auth'); + if (!token && to.path !== '/webui' && to.path !== '/qqlogin') { + MessagePlugin.error('Token 过期啦, 重新登录吧'); + localStorage.clear(); + setTimeout(() => { + next('/webui'); + }, 500); + } else { + next(); + } +}); + +export default router; diff --git a/napcat.webui/src/utils/request.ts b/napcat.webui/src/utils/request.ts index 88ce64ca..6e02c3a4 100644 --- a/napcat.webui/src/utils/request.ts +++ b/napcat.webui/src/utils/request.ts @@ -1,10 +1,10 @@ import { MessagePlugin } from 'tdesign-vue-next'; -import { router } from '@/router/index.js'; +import router from '@/router/index.js'; export const request = async (input: RequestInfo | URL, init?: RequestInit) => { const res = await fetch(input, init); const json = await res.json(); - if (json.msg.includes('Unauthorized')) { + if (json.message.includes('Unauthorized')) { MessagePlugin.error('Token 过期啦, 重新登录吧'); localStorage.clear(); router.push('/webui');