fix: 路由守卫

This commit is contained in:
纸凤孤凰
2024-12-10 16:59:57 +08:00
parent 3dc13e5c2e
commit eb5d68422f
4 changed files with 24 additions and 10 deletions

View File

@@ -1,9 +1,7 @@
import { request } from '@/utils/request.js';
export class githubApiManager { export class githubApiManager {
public async GetBaseData(): Promise<Response | null> { public async GetBaseData(): Promise<Response | null> {
try { 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', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -19,7 +17,7 @@ export class githubApiManager {
} }
public async GetReleasesData(): Promise<Response | null> { public async GetReleasesData(): Promise<Response | null> {
try { 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', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -35,7 +33,7 @@ export class githubApiManager {
} }
public async GetPullsData(): Promise<Response | null> { public async GetPullsData(): Promise<Response | null> {
try { 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', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -51,7 +49,7 @@ export class githubApiManager {
} }
public async GetContributors(): Promise<Response | null> { public async GetContributors(): Promise<Response | null> {
try { 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', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',

View File

@@ -46,7 +46,7 @@ import {
Loading as TLoading, Loading as TLoading,
HeadMenu as THeadMenu HeadMenu as THeadMenu
} from 'tdesign-vue-next'; } from 'tdesign-vue-next';
import { router } from './router'; import router from './router';
import 'tdesign-vue-next/es/style/index.css'; import 'tdesign-vue-next/es/style/index.css';
const app = createApp(App); const app = createApp(App);
app.use(router); app.use(router);

View File

@@ -7,6 +7,7 @@ import NetWork from '../pages/NetWork.vue';
import QQLogin from '../components/QQLogin.vue'; import QQLogin from '../components/QQLogin.vue';
import WebUiLogin from '../components/WebUiLogin.vue'; import WebUiLogin from '../components/WebUiLogin.vue';
import OtherConfig from '../pages/OtherConfig.vue'; import OtherConfig from '../pages/OtherConfig.vue';
import { MessagePlugin } from 'tdesign-vue-next';
const routes: Array<RouteRecordRaw> = [ const routes: Array<RouteRecordRaw> = [
{ path: '/', redirect: '/webui' }, { path: '/', redirect: '/webui' },
@@ -26,7 +27,22 @@ const routes: Array<RouteRecordRaw> = [
}, },
]; ];
export const router = createRouter({ const router = createRouter({
history: createWebHashHistory(), history: createWebHashHistory(),
routes, 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;

View File

@@ -1,10 +1,10 @@
import { MessagePlugin } from 'tdesign-vue-next'; 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) => { export const request = async (input: RequestInfo | URL, init?: RequestInit) => {
const res = await fetch(input, init); const res = await fetch(input, init);
const json = await res.json(); const json = await res.json();
if (json.msg.includes('Unauthorized')) { if (json.message.includes('Unauthorized')) {
MessagePlugin.error('Token 过期啦, 重新登录吧'); MessagePlugin.error('Token 过期啦, 重新登录吧');
localStorage.clear(); localStorage.clear();
router.push('/webui'); router.push('/webui');