feat: 优化请求处理 & 拆分Type
All checks were successful
Egg CI/CD / build-image (push) Successful in 32s
Egg CI/CD / deploy (push) Successful in 1m6s

This commit is contained in:
zhaoyingbo 2024-06-10 02:38:36 +00:00
parent fd3ada6f23
commit 7915003c1b
3 changed files with 37 additions and 6 deletions

View File

@ -5,7 +5,7 @@ import { initSchedule } from "./schedule";
initSchedule();
Bun.serve({
const server = Bun.serve({
async fetch(req) {
try {
const url = new URL(req.url);
@ -16,7 +16,8 @@ Bun.serve({
// 消息代理发送
if (url.pathname === "/message") return await manageMessageReq(req);
// 小程序
if (url.pathname.startsWith("/micro_app")) return await manageMicroAppReq(req);
if (url.pathname.startsWith("/micro_app"))
return await manageMicroAppReq(req);
// 其他
return new Response("hello, glade to see you!");
} catch (error: any) {
@ -27,3 +28,5 @@ Bun.serve({
},
port: 3000,
});
console.log(`Listening on ${server.hostname}:${server.port}`);

View File

@ -14,7 +14,11 @@ const manageLogin = async (req: Request, isSeek = false) => {
}
const { data: userInfo } = await service.lark.user.code2Login(code, isSeek);
console.log("🚀 ~ manageLogin ~ userInfo:", userInfo);
return new Response(JSON.stringify(userInfo));
return Response.json({
code: 0,
message: "success",
data: userInfo,
});
};
/**
@ -30,10 +34,24 @@ const manageBatchUser = async (req: Request, isSeek = false) => {
return new Response("user_ids not found", { status: 400 });
}
const {
data: { user: batchUserInfo },
// data: { user: batchUserInfo },
code,
data,
msg,
} = await service.lark.user.batchGet(user_ids, isSeek);
console.log("🚀 ~ manageBatchUser ~ users:", batchUserInfo);
return new Response(JSON.stringify(batchUserInfo));
console.log("🚀 ~ manageBatchUser ~ code:", code, data, msg);
if (code !== 0) {
return Response.json({
code,
message: msg,
data: null,
});
}
return Response.json({
code,
message: "success",
data: data.user,
});
};
/**

10
test/batchUser.ts Normal file
View File

@ -0,0 +1,10 @@
const res = await fetch("http://localhost:3000/micro_app/egg/batch_user", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
user_ids: ["libo12"],
}),
});
console.log(await res.json());