feat: 接口统一优化

This commit is contained in:
2026-05-23 13:45:58 +08:00
parent a3b1fba3ea
commit 7d68db723b
20 changed files with 302 additions and 302 deletions
+4 -2
View File
@@ -1,8 +1,9 @@
// server/middleware/api-auth.ts - 统一 API 鉴权层,所有非公开 /api/* 请求都必须通过此处
import { createError, getRequestURL } from "h3";
import { getRequestURL, setResponseStatus } from "h3";
import { isPublicApiRoute } from "~~/server/utils/api-auth-rules";
import { getAuthSession } from "~~/server/utils/auth";
import { apiErr } from "~~/server/utils/response";
/**
* 统一 API 鉴权 middleware
@@ -27,7 +28,8 @@ export default defineEventHandler(async (event) => {
const session = await getAuthSession(event);
if (!session) {
throw createError({ statusCode: 401, statusMessage: "未登录" });
setResponseStatus(event, 401);
return apiErr(401, "unauthorized");
}
// 挂到 event.context,后续 handler 直接读取,不再重复查询 session