Update index.js

This commit is contained in:
huxuejian 2026-09-09 11:07:27 +08:00
parent 288f9abba6
commit 0adca17bbe

View File

@ -201,9 +201,16 @@ async function addDrugInfo(item) {
async function getHlwDrugInfo(ctx) {
try {
const page = Number.isInteger(ctx.page) && ctx.page > 0 ? ctx.page : 1;
const pageSize = Number.isInteger(ctx.pageSize) && ctx.pageSize > 0 ? ctx.pageSize : 10;
const query = {}
// HTTP query parameters may arrive as strings. Number.isInteger('385')
// is false, which previously reset every such request to page 1.
const requestedPage = Number(ctx.page);
const requestedPageSize = Number(ctx.pageSize);
const page = Number.isSafeInteger(requestedPage) && requestedPage > 0 ? requestedPage : 1;
const pageSize = Number.isSafeInteger(requestedPageSize) && requestedPageSize > 0 ? requestedPageSize : 10;
const query = {};
if (typeof ctx.corpId === "string" && ctx.corpId.trim()) {
query.corpId = ctx.corpId.trim();
}
if (typeof ctx.name === "string" && ctx.name.trim()) {
query.name = new RegExp(ctx.name.trim(), "i")
};
@ -222,7 +229,8 @@ async function getHlwDrugInfo(ctx) {
if (typeof ctx.insuranceCode === 'string' && ctx.insuranceCode.trim()) {
query.insurance_code = ctx.insuranceCode.trim()
}
const res = await db.collection("drug-info").find(query).sort({ onSale: -1, createTime: -1 }).skip((page - 1) * pageSize).limit(pageSize).toArray();
// _id makes records with the same createTime deterministic across pages.
const res = await db.collection("drug-info").find(query).sort({ onSale: -1, createTime: -1, _id: 1 }).skip((page - 1) * pageSize).limit(pageSize).toArray();
const total = await db.collection("drug-info").countDocuments(query);
return {
success: true,