From dc065a9c29ee072734910adc6cbc4b73da4fa03d Mon Sep 17 00:00:00 2001 From: ila Date: Thu, 9 Jul 2026 22:22:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(handler):=20=E8=8D=AF=E5=93=81=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E6=8C=89=E5=88=86=E9=A1=B5=E5=A5=91=E7=BA=A6=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=20YPML00001=EF=BC=88T-211=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - DrugQuery.PageNo 去 omitempty(pageNo=0 也上送)+ 加 PageSize - handler Drugs 改 pageNo 必填(parseRequiredInt,缺则返回 400)+ 读 pageSize - 补缺 pageNo 返回 400 的测试 - 依 docx;YPML00001 未真实联调,pageNo 是否真必填待厂家样本核对 Co-Authored-By: Claude Opus 4.8 --- handler/public.go | 21 +++++++++++++++++---- handler/public_test.go | 10 ++++++++++ osi/public.go | 7 ++++--- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/handler/public.go b/handler/public.go index 8883efd..b2c0291 100644 --- a/handler/public.go +++ b/handler/public.go @@ -56,14 +56,19 @@ func (h *PublicHandler) Drugs(w http.ResponseWriter, r *http.Request) { return } q := r.URL.Query() - pageNo, ok := parseOptionalInt(w, q.Get("pageNo"), "pageNo") + pageNo, ok := parseRequiredInt(w, q.Get("pageNo"), "pageNo") // docx: 药品目录是分页查询,pageNo 必填 + if !ok { + return + } + pageSize, ok := parseOptionalInt(w, q.Get("pageSize"), "pageSize") if !ok { return } _, result, err := h.client.QueryDrugs(r.Context(), osi.DrugQuery{ - PageNo: pageNo, - YPMC: q.Get("ypmc"), - PYM: q.Get("pym"), + PageNo: pageNo, + PageSize: pageSize, + YPMC: q.Get("ypmc"), + PYM: q.Get("pym"), }) writeRawOrError(w, result, err) } @@ -82,6 +87,14 @@ func (h *PublicHandler) Orgs(w http.ResponseWriter, r *http.Request) { writeRawOrError(w, result, err) } +func parseRequiredInt(w http.ResponseWriter, raw string, name string) (int, bool) { + if raw == "" { + writeJSONError(w, http.StatusBadRequest, name+" is required") + return 0, false + } + return parseOptionalInt(w, raw, name) +} + func parseOptionalInt(w http.ResponseWriter, raw string, name string) (int, bool) { if raw == "" { return 0, true diff --git a/handler/public_test.go b/handler/public_test.go index 7460b75..7391e88 100644 --- a/handler/public_test.go +++ b/handler/public_test.go @@ -88,6 +88,16 @@ func TestPublicOrgsReturnsRawResponse(t *testing.T) { } } +func TestPublicDrugsRequiresPageNo(t *testing.T) { + h := NewPublicHandler(nil) // 缺 pageNo 在触达 client 前返回 400(药品目录分页必填) + req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/drugs?ypmc=测试", nil) + rec := httptest.NewRecorder() + h.Drugs(rec, req) + if rec.Code != http.StatusBadRequest { + t.Fatalf("status = %d, want 400 (pageNo required)", rec.Code) + } +} + func TestPublicDictionaryRejectsInvalidPageNo(t *testing.T) { h := NewPublicHandler(nil) diff --git a/osi/public.go b/osi/public.go index fc22eeb..ab4c8e1 100644 --- a/osi/public.go +++ b/osi/public.go @@ -25,9 +25,10 @@ type Doctor struct { } type DrugQuery struct { - PageNo int `json:"pageNo,omitempty"` - YPMC string `json:"ypmc,omitempty"` - PYM string `json:"pym,omitempty"` + PageNo int `json:"pageNo"` // docx 必填,不加 omitempty 以确保 pageNo=0 也上送(避免被平台判缺参) + PageSize int `json:"pageSize,omitempty"` + YPMC string `json:"ypmc,omitempty"` + PYM string `json:"pym,omitempty"` } type Drug struct {