docmee.ts
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { getServerSideConfig } from "@/app/config/server";
import { NextRequest, NextResponse } from "next/server";
// 处理每日使用限制逻辑
function parseDailyUsage(allowNum: string, configMax: number): number {
if (allowNum === "first") return configMax;
const parsed = parseInt(allowNum, 10);
return Number.isNaN(parsed) ? configMax : parsed;
}
export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
const config = getServerSideConfig();
const baseUrl = config.docmeeUrl;
const subPath = params.path.join("/");
// if(subPath==='createApiToken'){const reqUrl = `${baseUrl}/api/user/${subPath}`;}
const reqUrl = `${baseUrl}/api/user/${subPath}`;
console.log(reqUrl);
try {
// 获取 accessCode
const body = await new Response(req.body).text();
const parsedBody = JSON.parse(body);
// 提取 accessCode 和 max_use 参数
const uid = parsedBody.accessCode;
let maxUse = parsedBody.max_use;
if (!uid) {
return NextResponse.json({
data: "请前往设置页面输入登录密码",
status: 400,
});
}
if (!maxUse) {
return NextResponse.json({
data: "参数错误",
status: 400,
});
}
maxUse = parseDailyUsage(maxUse, config.docmeeMaxDailyUses);
const headers = new Headers({
"Api-Key": config.docmeeApiKey,
"Content-Type": "application/json",
});
console.log("********************" + config.docmeeApiKey);
// 使用 async/await 处理 fetch 请求
const response = await fetch(reqUrl, {
headers,
method: "POST",
body: JSON.stringify({ uid: uid, limit: maxUse }),
});
if (!response.ok) {
throw new Error(
`HTTP error! status: ${response.status} ${response.text()}`,
);
}
const data = await response.json();
console.log("-----------------data", data);
// 返回固定的 token
return NextResponse.json({
data: data,
status: 200,
});
} catch (error) {
console.error("处理请求时出错:", error);
return NextResponse.json({
data: "处理请求时出错",
status: 500,
});
}
}