fix: Login Check

This commit is contained in:
手瓜一十雪
2024-12-13 17:38:27 +08:00
parent d59e5f2133
commit b86a28092a
5 changed files with 41 additions and 46 deletions

View File

@@ -1,5 +1,3 @@
import { request } from '@/utils/request.js';
import { EventSourcePolyfill } from 'event-source-polyfill'; import { EventSourcePolyfill } from 'event-source-polyfill';
type LogListItem = string; type LogListItem = string;
type LogListData = LogListItem[]; type LogListData = LogListItem[];
@@ -15,7 +13,7 @@ export class LogManager {
} }
public async GetLogList(): Promise<LogListData> { public async GetLogList(): Promise<LogListData> {
try { try {
const ConfigResponse = await request(`${this.apiPrefix}/Log/GetLogList`, { const ConfigResponse = await fetch(`${this.apiPrefix}/Log/GetLogList`, {
method: 'GET', method: 'GET',
headers: { headers: {
Authorization: 'Bearer ' + this.retCredential, Authorization: 'Bearer ' + this.retCredential,
@@ -35,7 +33,7 @@ export class LogManager {
} }
public async GetLog(FileName: string): Promise<string> { public async GetLog(FileName: string): Promise<string> {
try { try {
const ConfigResponse = await request(`${this.apiPrefix}/Log/GetLog?id=${FileName}`, { const ConfigResponse = await fetch(`${this.apiPrefix}/Log/GetLog?id=${FileName}`, {
method: 'GET', method: 'GET',
headers: { headers: {
Authorization: 'Bearer ' + this.retCredential, Authorization: 'Bearer ' + this.retCredential,

View File

@@ -1,6 +1,5 @@
import { request } from '@/utils/request.js';
import { OneBotConfig } from '../../../src/onebot/config/config'; import { OneBotConfig } from '../../../src/onebot/config/config';
import { ResponseCode } from '../../../src/webui/src/const/status';
export class QQLoginManager { export class QQLoginManager {
private retCredential: string; private retCredential: string;
private readonly apiPrefix: string; private readonly apiPrefix: string;
@@ -14,7 +13,7 @@ export class QQLoginManager {
// TODO: // TODO:
public async GetOB11Config(): Promise<OneBotConfig> { public async GetOB11Config(): Promise<OneBotConfig> {
try { try {
const ConfigResponse = await request(`${this.apiPrefix}/OB11Config/GetConfig`, { const ConfigResponse = await fetch(`${this.apiPrefix}/OB11Config/GetConfig`, {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: 'Bearer ' + this.retCredential, Authorization: 'Bearer ' + this.retCredential,
@@ -23,8 +22,8 @@ export class QQLoginManager {
}); });
if (ConfigResponse.status == 200) { if (ConfigResponse.status == 200) {
const ConfigResponseJson = await ConfigResponse.json(); const ConfigResponseJson = await ConfigResponse.json();
if (ConfigResponseJson.code == 0) { if (ConfigResponseJson.code == ResponseCode.Success) {
return ConfigResponseJson?.data as OneBotConfig; return ConfigResponseJson;
} }
} }
} catch (error) { } catch (error) {
@@ -35,7 +34,7 @@ export class QQLoginManager {
public async SetOB11Config(config: OneBotConfig): Promise<boolean> { public async SetOB11Config(config: OneBotConfig): Promise<boolean> {
try { try {
const ConfigResponse = await request(`${this.apiPrefix}/OB11Config/SetConfig`, { const ConfigResponse = await fetch(`${this.apiPrefix}/OB11Config/SetConfig`, {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: 'Bearer ' + this.retCredential, Authorization: 'Bearer ' + this.retCredential,
@@ -57,7 +56,7 @@ export class QQLoginManager {
public async checkQQLoginStatus(): Promise<boolean> { public async checkQQLoginStatus(): Promise<boolean> {
try { try {
const QQLoginResponse = await request(`${this.apiPrefix}/QQLogin/CheckLoginStatus`, { const QQLoginResponse = await fetch(`${this.apiPrefix}/QQLogin/CheckLoginStatus`, {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: 'Bearer ' + this.retCredential, Authorization: 'Bearer ' + this.retCredential,
@@ -77,7 +76,7 @@ export class QQLoginManager {
} }
public async checkQQLoginStatusWithQrcode(): Promise<{ qrcodeurl: string; isLogin: string } | undefined> { public async checkQQLoginStatusWithQrcode(): Promise<{ qrcodeurl: string; isLogin: string } | undefined> {
try { try {
const QQLoginResponse = await request(`${this.apiPrefix}/QQLogin/CheckLoginStatus`, { const QQLoginResponse = await fetch(`${this.apiPrefix}/QQLogin/CheckLoginStatus`, {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: 'Bearer ' + this.retCredential, Authorization: 'Bearer ' + this.retCredential,
@@ -98,7 +97,7 @@ export class QQLoginManager {
public async checkWebUiLogined(): Promise<boolean> { public async checkWebUiLogined(): Promise<boolean> {
try { try {
const LoginResponse = await request(`${this.apiPrefix}/auth/check`, { const LoginResponse = await fetch(`${this.apiPrefix}/auth/check`, {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: 'Bearer ' + this.retCredential, Authorization: 'Bearer ' + this.retCredential,
@@ -119,7 +118,7 @@ export class QQLoginManager {
public async loginWithToken(token: string): Promise<string | null> { public async loginWithToken(token: string): Promise<string | null> {
try { try {
const loginResponse = await request(`${this.apiPrefix}/auth/login`, { const loginResponse = await fetch(`${this.apiPrefix}/auth/login`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -140,7 +139,7 @@ export class QQLoginManager {
public async getQQLoginQrcode(): Promise<string> { public async getQQLoginQrcode(): Promise<string> {
try { try {
const QQLoginResponse = await request(`${this.apiPrefix}/QQLogin/GetQQLoginQrcode`, { const QQLoginResponse = await fetch(`${this.apiPrefix}/QQLogin/GetQQLoginQrcode`, {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: 'Bearer ' + this.retCredential, Authorization: 'Bearer ' + this.retCredential,
@@ -161,7 +160,7 @@ export class QQLoginManager {
public async getQQQuickLoginList(): Promise<string[]> { public async getQQQuickLoginList(): Promise<string[]> {
try { try {
const QQLoginResponse = await request(`${this.apiPrefix}/QQLogin/GetQuickLoginList`, { const QQLoginResponse = await fetch(`${this.apiPrefix}/QQLogin/GetQuickLoginList`, {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: 'Bearer ' + this.retCredential, Authorization: 'Bearer ' + this.retCredential,
@@ -182,7 +181,7 @@ export class QQLoginManager {
public async setQuickLogin(uin: string): Promise<{ result: boolean; errMsg: string }> { public async setQuickLogin(uin: string): Promise<{ result: boolean; errMsg: string }> {
try { try {
const QQLoginResponse = await request(`${this.apiPrefix}/QQLogin/SetQuickLogin`, { const QQLoginResponse = await fetch(`${this.apiPrefix}/QQLogin/SetQuickLogin`, {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: 'Bearer ' + this.retCredential, Authorization: 'Bearer ' + this.retCredential,

View File

@@ -27,7 +27,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'; import { ref, onMounted, onBeforeUnmount, nextTick, watch } from 'vue';
import * as QRCode from 'qrcode'; import * as QRCode from 'qrcode';
import { useRouter } from 'vue-router'; import { useRouter } from 'vue-router';
import { MessagePlugin } from 'tdesign-vue-next'; import { MessagePlugin } from 'tdesign-vue-next';
@@ -41,6 +41,7 @@ const qrcodeCanvas = ref<HTMLCanvasElement | null>(null);
const qqLoginManager = new QQLoginManager(localStorage.getItem('auth') || ''); const qqLoginManager = new QQLoginManager(localStorage.getItem('auth') || '');
let heartBeatTimer: number | null = null; let heartBeatTimer: number | null = null;
let qrcodeUrl: string = ''; let qrcodeUrl: string = '';
const selectAccount = async (accountName: string): Promise<void> => { const selectAccount = async (accountName: string): Promise<void> => {
const { result, errMsg } = await qqLoginManager.setQuickLogin(accountName); const { result, errMsg } = await qqLoginManager.setQuickLogin(accountName);
if (result) { if (result) {
@@ -74,10 +75,6 @@ const HeartBeat = async (): Promise<void> => {
if (heartBeatTimer) { if (heartBeatTimer) {
clearInterval(heartBeatTimer); clearInterval(heartBeatTimer);
} }
// //判断是否已经调转
// if (router.currentRoute.value.path !== '/dashboard/basic-info') {
// return;
// }
await MessagePlugin.success('登录成功即将跳转'); await MessagePlugin.success('登录成功即将跳转');
await router.push({ path: '/dashboard/basic-info' }); await router.push({ path: '/dashboard/basic-info' });
} else if (isLogined?.qrcodeurl && qrcodeUrl !== isLogined.qrcodeurl) { } else if (isLogined?.qrcodeurl && qrcodeUrl !== isLogined.qrcodeurl) {
@@ -89,6 +86,7 @@ const HeartBeat = async (): Promise<void> => {
const InitPages = async (): Promise<void> => { const InitPages = async (): Promise<void> => {
quickLoginList.value = await qqLoginManager.getQQQuickLoginList(); quickLoginList.value = await qqLoginManager.getQQQuickLoginList();
qrcodeUrl = await qqLoginManager.getQQLoginQrcode(); qrcodeUrl = await qqLoginManager.getQQLoginQrcode();
await nextTick();
generateQrCode(qrcodeUrl, qrcodeCanvas.value); generateQrCode(qrcodeUrl, qrcodeCanvas.value);
}; };
@@ -103,6 +101,14 @@ onBeforeUnmount(() => {
if (heartBeatTimer) { if (heartBeatTimer) {
clearInterval(heartBeatTimer); clearInterval(heartBeatTimer);
} }
});
watch(loginMethod, async (newMethod) => {
if (newMethod === 'qrcode') {
await nextTick();
generateQrCode(qrcodeUrl, qrcodeCanvas.value);
}
}); });
</script> </script>

View File

@@ -8,6 +8,7 @@ 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'; import { MessagePlugin } from 'tdesign-vue-next';
import { QQLoginManager } from '@/backend/shell';
const routes: Array<RouteRecordRaw> = [ const routes: Array<RouteRecordRaw> = [
{ path: '/', redirect: '/webui' }, { path: '/', redirect: '/webui' },
@@ -32,17 +33,22 @@ const router = createRouter({
routes, routes,
}); });
router.beforeEach((to, from, next) => { router.beforeEach(async (to, from, next) => {
const isPublicRoute = ['/webui', '/qqlogin'].includes(to.path);
const token = localStorage.getItem('auth'); const token = localStorage.getItem('auth');
if (!token && to.path !== '/webui' && to.path !== '/qqlogin') {
MessagePlugin.error('Token 过期啦, 重新登录吧'); if (!isPublicRoute) {
localStorage.clear(); if (!token) {
setTimeout(() => { MessagePlugin.error('请先登录');
next('/webui'); return next('/webui');
}, 500); }
} else { const login = await new QQLoginManager(token).checkWebUiLogined();
next(); if (!login) {
MessagePlugin.error('请先登录');
return next('/webui');
}
} }
next();
}); });
export default router; export default router;

View File

@@ -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;
};