From 0adca17bbe8053c290bd5d0d3df1d7350e7c4c68 Mon Sep 17 00:00:00 2001 From: huxuejian Date: Wed, 9 Sep 2026 11:07:27 +0800 Subject: [PATCH] Update index.js --- hlw/drug-info/index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/hlw/drug-info/index.js b/hlw/drug-info/index.js index 55d52d7..0d7c5e5 100644 --- a/hlw/drug-info/index.js +++ b/hlw/drug-info/index.js @@ -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,