From b86a28092a4f0103f2e3b6c3217ad11e5c3ae171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Fri, 13 Dec 2024 17:38:27 +0800 Subject: [PATCH] fix: Login Check --- napcat.webui/src/backend/log.ts | 6 ++---- napcat.webui/src/backend/shell.ts | 25 ++++++++++++------------- napcat.webui/src/components/QQLogin.vue | 18 ++++++++++++------ napcat.webui/src/router/index.ts | 24 +++++++++++++++--------- napcat.webui/src/utils/request.ts | 14 -------------- 5 files changed, 41 insertions(+), 46 deletions(-) delete mode 100644 napcat.webui/src/utils/request.ts diff --git a/napcat.webui/src/backend/log.ts b/napcat.webui/src/backend/log.ts index b88a8db4..32fdaac2 100644 --- a/napcat.webui/src/backend/log.ts +++ b/napcat.webui/src/backend/log.ts @@ -1,5 +1,3 @@ -import { request } from '@/utils/request.js'; - import { EventSourcePolyfill } from 'event-source-polyfill'; type LogListItem = string; type LogListData = LogListItem[]; @@ -15,7 +13,7 @@ export class LogManager { } public async GetLogList(): Promise { try { - const ConfigResponse = await request(`${this.apiPrefix}/Log/GetLogList`, { + const ConfigResponse = await fetch(`${this.apiPrefix}/Log/GetLogList`, { method: 'GET', headers: { Authorization: 'Bearer ' + this.retCredential, @@ -35,7 +33,7 @@ export class LogManager { } public async GetLog(FileName: string): Promise { try { - const ConfigResponse = await request(`${this.apiPrefix}/Log/GetLog?id=${FileName}`, { + const ConfigResponse = await fetch(`${this.apiPrefix}/Log/GetLog?id=${FileName}`, { method: 'GET', headers: { Authorization: 'Bearer ' + this.retCredential, diff --git a/napcat.webui/src/backend/shell.ts b/napcat.webui/src/backend/shell.ts index 711c53be..dab3f86e 100644 --- a/napcat.webui/src/backend/shell.ts +++ b/napcat.webui/src/backend/shell.ts @@ -1,6 +1,5 @@ -import { request } from '@/utils/request.js'; import { OneBotConfig } from '../../../src/onebot/config/config'; - +import { ResponseCode } from '../../../src/webui/src/const/status'; export class QQLoginManager { private retCredential: string; private readonly apiPrefix: string; @@ -14,7 +13,7 @@ export class QQLoginManager { // TODO: public async GetOB11Config(): Promise { try { - const ConfigResponse = await request(`${this.apiPrefix}/OB11Config/GetConfig`, { + const ConfigResponse = await fetch(`${this.apiPrefix}/OB11Config/GetConfig`, { method: 'POST', headers: { Authorization: 'Bearer ' + this.retCredential, @@ -23,8 +22,8 @@ export class QQLoginManager { }); if (ConfigResponse.status == 200) { const ConfigResponseJson = await ConfigResponse.json(); - if (ConfigResponseJson.code == 0) { - return ConfigResponseJson?.data as OneBotConfig; + if (ConfigResponseJson.code == ResponseCode.Success) { + return ConfigResponseJson; } } } catch (error) { @@ -35,7 +34,7 @@ export class QQLoginManager { public async SetOB11Config(config: OneBotConfig): Promise { try { - const ConfigResponse = await request(`${this.apiPrefix}/OB11Config/SetConfig`, { + const ConfigResponse = await fetch(`${this.apiPrefix}/OB11Config/SetConfig`, { method: 'POST', headers: { Authorization: 'Bearer ' + this.retCredential, @@ -57,7 +56,7 @@ export class QQLoginManager { public async checkQQLoginStatus(): Promise { try { - const QQLoginResponse = await request(`${this.apiPrefix}/QQLogin/CheckLoginStatus`, { + const QQLoginResponse = await fetch(`${this.apiPrefix}/QQLogin/CheckLoginStatus`, { method: 'POST', headers: { Authorization: 'Bearer ' + this.retCredential, @@ -77,7 +76,7 @@ export class QQLoginManager { } public async checkQQLoginStatusWithQrcode(): Promise<{ qrcodeurl: string; isLogin: string } | undefined> { try { - const QQLoginResponse = await request(`${this.apiPrefix}/QQLogin/CheckLoginStatus`, { + const QQLoginResponse = await fetch(`${this.apiPrefix}/QQLogin/CheckLoginStatus`, { method: 'POST', headers: { Authorization: 'Bearer ' + this.retCredential, @@ -98,7 +97,7 @@ export class QQLoginManager { public async checkWebUiLogined(): Promise { try { - const LoginResponse = await request(`${this.apiPrefix}/auth/check`, { + const LoginResponse = await fetch(`${this.apiPrefix}/auth/check`, { method: 'POST', headers: { Authorization: 'Bearer ' + this.retCredential, @@ -119,7 +118,7 @@ export class QQLoginManager { public async loginWithToken(token: string): Promise { try { - const loginResponse = await request(`${this.apiPrefix}/auth/login`, { + const loginResponse = await fetch(`${this.apiPrefix}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -140,7 +139,7 @@ export class QQLoginManager { public async getQQLoginQrcode(): Promise { try { - const QQLoginResponse = await request(`${this.apiPrefix}/QQLogin/GetQQLoginQrcode`, { + const QQLoginResponse = await fetch(`${this.apiPrefix}/QQLogin/GetQQLoginQrcode`, { method: 'POST', headers: { Authorization: 'Bearer ' + this.retCredential, @@ -161,7 +160,7 @@ export class QQLoginManager { public async getQQQuickLoginList(): Promise { try { - const QQLoginResponse = await request(`${this.apiPrefix}/QQLogin/GetQuickLoginList`, { + const QQLoginResponse = await fetch(`${this.apiPrefix}/QQLogin/GetQuickLoginList`, { method: 'POST', headers: { Authorization: 'Bearer ' + this.retCredential, @@ -182,7 +181,7 @@ export class QQLoginManager { public async setQuickLogin(uin: string): Promise<{ result: boolean; errMsg: string }> { try { - const QQLoginResponse = await request(`${this.apiPrefix}/QQLogin/SetQuickLogin`, { + const QQLoginResponse = await fetch(`${this.apiPrefix}/QQLogin/SetQuickLogin`, { method: 'POST', headers: { Authorization: 'Bearer ' + this.retCredential, diff --git a/napcat.webui/src/components/QQLogin.vue b/napcat.webui/src/components/QQLogin.vue index 68fe62b3..04c09531 100644 --- a/napcat.webui/src/components/QQLogin.vue +++ b/napcat.webui/src/components/QQLogin.vue @@ -27,7 +27,7 @@ @@ -178,4 +184,4 @@ onBeforeUnmount(() => { left: 0; right: 0; } - + \ No newline at end of file diff --git a/napcat.webui/src/router/index.ts b/napcat.webui/src/router/index.ts index 5883b925..263822eb 100644 --- a/napcat.webui/src/router/index.ts +++ b/napcat.webui/src/router/index.ts @@ -8,6 +8,7 @@ import QQLogin from '../components/QQLogin.vue'; import WebUiLogin from '../components/WebUiLogin.vue'; import OtherConfig from '../pages/OtherConfig.vue'; import { MessagePlugin } from 'tdesign-vue-next'; +import { QQLoginManager } from '@/backend/shell'; const routes: Array = [ { path: '/', redirect: '/webui' }, @@ -32,17 +33,22 @@ const router = createRouter({ routes, }); -router.beforeEach((to, from, next) => { +router.beforeEach(async (to, from, next) => { + const isPublicRoute = ['/webui', '/qqlogin'].includes(to.path); const token = localStorage.getItem('auth'); - if (!token && to.path !== '/webui' && to.path !== '/qqlogin') { - MessagePlugin.error('Token 过期啦, 重新登录吧'); - localStorage.clear(); - setTimeout(() => { - next('/webui'); - }, 500); - } else { - next(); + + if (!isPublicRoute) { + if (!token) { + MessagePlugin.error('请先登录'); + return next('/webui'); + } + const login = await new QQLoginManager(token).checkWebUiLogined(); + if (!login) { + MessagePlugin.error('请先登录'); + return next('/webui'); + } } + next(); }); export default router; diff --git a/napcat.webui/src/utils/request.ts b/napcat.webui/src/utils/request.ts deleted file mode 100644 index 6e02c3a4..00000000 --- a/napcat.webui/src/utils/request.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { MessagePlugin } from 'tdesign-vue-next'; -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.message.includes('Unauthorized')) { - MessagePlugin.error('Token 过期啦, 重新登录吧'); - localStorage.clear(); - router.push('/webui'); - } - res.json = async () => json; - return res; -};