mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
feat: 历史日志
This commit is contained in:
19
src/webui/src/api/Log.ts
Normal file
19
src/webui/src/api/Log.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import type { RequestHandler } from 'express';
|
||||
import { sendError, sendSuccess } from '../utils/response';
|
||||
import { WebUiConfigWrapper } from '../helper/config';
|
||||
|
||||
// 日志记录
|
||||
export const LogHandler: RequestHandler = async (req, res) => {
|
||||
const filename = req.query.id as string;
|
||||
if (filename.includes('..')) {
|
||||
return sendError(res, 'ID不合法');
|
||||
}
|
||||
const logContent = WebUiConfigWrapper.GetLogContent(filename);
|
||||
return sendSuccess(res, logContent);
|
||||
};
|
||||
|
||||
// 日志列表
|
||||
export const LogListHandler: RequestHandler = async (_, res) => {
|
||||
const logList = WebUiConfigWrapper.GetLogsList();
|
||||
return sendSuccess(res, logList);
|
||||
};
|
@@ -1,5 +1,5 @@
|
||||
import { webUiPathWrapper } from '@/webui';
|
||||
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { existsSync, readFileSync, writeFileSync, readdirSync } from 'node:fs';
|
||||
import * as net from 'node:net';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
@@ -131,4 +131,26 @@ export class WebUiConfigWrapper {
|
||||
}
|
||||
return defaultconfig; // 理论上这行代码到不了,到了只能返回默认配置了
|
||||
}
|
||||
|
||||
// 获取日志文件夹路径
|
||||
public static async GetLogsPath(): Promise<string> {
|
||||
return resolve(webUiPathWrapper.logsPath);
|
||||
}
|
||||
// 获取日志列表
|
||||
public static GetLogsList(): string[] {
|
||||
if (existsSync(webUiPathWrapper.logsPath)) {
|
||||
return readdirSync(webUiPathWrapper.logsPath)
|
||||
.filter((file) => file.endsWith('.log'))
|
||||
.map((file) => file.replace('.log', ''));
|
||||
}
|
||||
return [];
|
||||
}
|
||||
// 获取指定日志文件内容
|
||||
public static GetLogContent(filename: string): string {
|
||||
const logPath = resolve(webUiPathWrapper.logsPath, `${filename}.log`);
|
||||
if (existsSync(logPath)) {
|
||||
return readFileSync(logPath, 'utf-8');
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
9
src/webui/src/router/Log.ts
Normal file
9
src/webui/src/router/Log.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Router } from 'express';
|
||||
import { LogHandler, LogListHandler } from '../api/Log';
|
||||
const router = Router();
|
||||
// router:读取日志内容
|
||||
router.get('/GetLog', LogHandler);
|
||||
// router:读取日志列表
|
||||
router.get('/GetLogList', LogListHandler);
|
||||
|
||||
export { router as LogRouter };
|
@@ -10,6 +10,7 @@ import { sendSuccess } from '@webapi/utils/response';
|
||||
|
||||
import { QQLoginRouter } from '@webapi/router/QQLogin';
|
||||
import { AuthRouter } from '@webapi/router/auth';
|
||||
import { LogRouter } from '@webapi/router/Log';
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -26,5 +27,7 @@ router.use('/auth', AuthRouter);
|
||||
router.use('/QQLogin', QQLoginRouter);
|
||||
// router:OB11配置相关路由
|
||||
router.use('/OB11Config', OB11ConfigRouter);
|
||||
// router:日志相关路由
|
||||
router.use('/Log', LogRouter);
|
||||
|
||||
export { router as ALLRouter };
|
||||
|
Reference in New Issue
Block a user