feat: 新增鉴权

This commit is contained in:
2026-05-22 22:38:01 +08:00
parent b02f15f735
commit 3857f77f8b
38 changed files with 1697 additions and 195 deletions
+52 -47
View File
@@ -1,35 +1,16 @@
// server/utils/runtimeState.ts - 服务启动时间和最近问答记录的内存状态
// server/utils/runtimeState.ts - 服务启动时间内存状态 + 问答记录 DB 读写
import { answerCache } from "~~/server/utils/cache";
import { prisma } from "~~/server/utils/db";
import { serverEnv } from "~~/server/utils/env";
/** 最近问答记录,沿用旧 Python 服务 dashboard/stats 的内存记录语义 */
export interface QaRecord {
/** 本地可读时间,方便后续如果恢复 dashboard 时直接展示 */
time: string;
/** ISO 时间,方便机器处理和排序 */
timestamp: string;
/** 题目正文 */
question: string;
/** 题型 */
type: string;
/** 选项文本 */
options: string;
/** 最终返回给 OCS 的答案 */
answer: string;
}
/** 只保留最近 100 条,避免长时间运行后内存无限增长 */
const MAX_RECORDS = 100;
/** 进程启动时间,用于 `/api/stats` 返回 uptime */
const startTime = Date.now();
/** 进程内问答记录;服务重启或多实例部署时不会共享 */
const qaRecords: QaRecord[] = [];
/** 对外展示的服务版本,和 README/API 文档保持一致 */
export const SERVICE_VERSION = "1.1.0";
/** 格式化成本地 `YYYY-MM-DD HH:mm:ss`,对齐旧 Python 服务记录格式 */
const formatLocalDateTime = (date: Date) => {
export const formatLocalDateTime = (date: Date) => {
const pad = (value: number) => value.toString().padStart(2, "0");
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(
@@ -40,35 +21,60 @@ const formatLocalDateTime = (date: Date) => {
};
/**
* 追加一条问答记录
*
* 记录只用于统计和未来可能恢复的 dashboard,不作为题库持久化数据
* 向数据库写入一条问答记录
*/
export const addQaRecord = (record: Omit<QaRecord, "time" | "timestamp">) => {
const now = new Date();
qaRecords.push({
time: formatLocalDateTime(now),
timestamp: now.toISOString(),
...record
export const addQaRecord = async (record: {
userId: string;
question: string;
type: string;
options: string;
answer: string;
}) => {
await prisma.qaRecord.create({
data: {
userId: record.userId,
question: record.question,
type: record.type,
options: record.options || null,
answer: record.answer || null
}
});
if (qaRecords.length > MAX_RECORDS) {
qaRecords.shift();
}
};
/**
* 获取最近问答记录
*
* 返回副本并按“最新在前”排序,避免 API handler 或前端展示逻辑误改内存原数组
* 从数据库分页读取指定用户的问答记录,按创建时间倒序
*/
export const getQaRecords = () => {
return [...qaRecords].reverse();
};
/** 清空进程内问答记录,通常和 answerCache.clear() 一起调用 */
export const clearQaRecords = () => {
qaRecords.length = 0;
export const getQaRecords = async (
userId: string,
options: { page: number; size: number }
) => {
const { page, size } = options;
const skip = (page - 1) * size;
const [total, rows] = await Promise.all([
prisma.qaRecord.count({ where: { userId } }),
prisma.qaRecord.findMany({
where: { userId },
orderBy: { createdAt: "desc" },
skip,
take: size,
select: {
question: true,
type: true,
options: true,
answer: true,
createdAt: true
}
})
]);
const records = rows.map((r) => ({
time: formatLocalDateTime(r.createdAt),
timestamp: r.createdAt.toISOString(),
question: r.question,
type: r.type,
options: r.options ?? "",
answer: r.answer ?? ""
}));
return { records, total };
};
/** 生成 `/api/stats` 响应,实时计算 uptime 和有效缓存数量 */
export const getRuntimeStats = () => {
@@ -77,7 +83,6 @@ export const getRuntimeStats = () => {
uptime: (Date.now() - startTime) / 1000,
model: serverEnv.openAiModel,
cache_enabled: serverEnv.enableCache,
cache_size: answerCache?.size() ?? 0,
qa_records_count: qaRecords.length
cache_size: answerCache?.size() ?? 0
};
};