feat(handler): 体检 HTTP 查询端点(T-302)
- GET /api/health-check/last(最近一次)+ /api/health-check/list(年度已检未检名单) - 回写平台完整响应 Result.Raw,抽 writeRawOrError 共用 - last 按 idCard/phrid/empiId;list 按 checkYear(必填)+idCard+checkType - 单测:路径/原样回写/缺参 400(脱敏假 OSI 后端);默认仅绑本机 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"chis_osi/osi"
|
||||
)
|
||||
|
||||
// HealthCheckHandler 暴露健康体检查询的 HTTP 入口。
|
||||
type HealthCheckHandler struct {
|
||||
client *osi.Client
|
||||
}
|
||||
|
||||
func NewHealthCheckHandler(client *osi.Client) *HealthCheckHandler {
|
||||
return &HealthCheckHandler{client: client}
|
||||
}
|
||||
|
||||
// Last 处理 GET /api/health-check/last?idCard=..|phrid=..|empiId=..
|
||||
// 返回某人最近一次体检的平台完整响应(含全部子节点,见 docs/04 §11)。
|
||||
func (h *HealthCheckHandler) Last(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeJSONError(w, http.StatusMethodNotAllowed, "only GET is supported")
|
||||
return
|
||||
}
|
||||
q := r.URL.Query()
|
||||
query := osi.FindHealthCheckQuery{
|
||||
IDCard: q.Get("idCard"),
|
||||
PHRID: q.Get("phrid"),
|
||||
EMPIID: q.Get("empiId"),
|
||||
}
|
||||
if query.IDCard == "" && query.PHRID == "" && query.EMPIID == "" {
|
||||
writeJSONError(w, http.StatusBadRequest, "provide one of: idCard, phrid, empiId")
|
||||
return
|
||||
}
|
||||
_, result, err := h.client.LastHealthCheck(r.Context(), query)
|
||||
writeRawOrError(w, result, err)
|
||||
}
|
||||
|
||||
// List 处理 GET /api/health-check/list?checkYear=..&idCard=..&checkType=..&page=..&rows=..
|
||||
// 返回某年度已检/未检人员名单的平台完整响应(名单,非体检明细,见 docs/04 §11.4)。
|
||||
func (h *HealthCheckHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeJSONError(w, http.StatusMethodNotAllowed, "only GET is supported")
|
||||
return
|
||||
}
|
||||
q := r.URL.Query()
|
||||
if q.Get("checkYear") == "" {
|
||||
writeJSONError(w, http.StatusBadRequest, "checkYear is required")
|
||||
return
|
||||
}
|
||||
query := osi.ListHealthCheckQuery{
|
||||
CheckYear: q.Get("checkYear"),
|
||||
IDCard: q.Get("idCard"),
|
||||
CheckType: q.Get("checkType"),
|
||||
Page: q.Get("page"),
|
||||
Rows: q.Get("rows"),
|
||||
}
|
||||
_, result, err := h.client.ListHealthCheckPeople(r.Context(), query)
|
||||
writeRawOrError(w, result, err)
|
||||
}
|
||||
|
||||
// writeRawOrError 拿到平台响应就原样回写完整 JSON,否则按错误返回。
|
||||
func writeRawOrError(w http.ResponseWriter, result osi.Result, err error) {
|
||||
if len(result.Raw) > 0 {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_, _ = w.Write(result.Raw)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusBadGateway, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSONError(w, http.StatusBadGateway, "empty response from OSI")
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"chis_osi/osi"
|
||||
)
|
||||
|
||||
func newTestClient(t *testing.T, body string) (*osi.Client, *string) {
|
||||
t.Helper()
|
||||
var seenPath string
|
||||
osiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seenPath = r.URL.Path
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(body))
|
||||
}))
|
||||
t.Cleanup(osiServer.Close)
|
||||
|
||||
transport, err := osi.NewTransport(osi.TransportConfig{Timeout: time.Second})
|
||||
if err != nil {
|
||||
t.Fatalf("NewTransport: %v", err)
|
||||
}
|
||||
client := osi.NewClient(osi.ClientConfig{BaseURL: osiServer.URL, UserName: "u", Ask: "k", Transport: transport})
|
||||
return client, &seenPath
|
||||
}
|
||||
|
||||
func TestHealthCheckLastReturnsRawResponse(t *testing.T) {
|
||||
client, seenPath := newTestClient(t, `{"code":"01","message":"操作成功","data":{"checkId":"CHK-1","idcard":"TEST-1"}}`)
|
||||
h := NewHealthCheckHandler(client)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/health-check/last?idCard=TEST-1", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.Last(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", rec.Code)
|
||||
}
|
||||
if *seenPath != "/osi/api/auto/jktjlscx/query" {
|
||||
t.Fatalf("osi path = %q", *seenPath)
|
||||
}
|
||||
body, _ := io.ReadAll(rec.Body)
|
||||
if !strings.Contains(string(body), `"CHK-1"`) {
|
||||
t.Fatalf("body missing content: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthCheckLastRequiresIdentifier(t *testing.T) {
|
||||
h := NewHealthCheckHandler(nil) // 无标识符在触达 client 前返回 400
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/health-check/last", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.Last(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthCheckListRequiresCheckYear(t *testing.T) {
|
||||
h := NewHealthCheckHandler(nil) // 缺 checkYear 在触达 client 前返回 400
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/health-check/list?idCard=TEST-1", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.List(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthCheckListReturnsRosterRaw(t *testing.T) {
|
||||
client, seenPath := newTestClient(t, `{"code":"01","message":"操作成功","data":[{"idCard":"TEST-1","checkType":"0"}]}`)
|
||||
h := NewHealthCheckHandler(client)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/health-check/list?checkYear=2025&idCard=TEST-1", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.List(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", rec.Code)
|
||||
}
|
||||
if *seenPath != "/osi/api/auto/jktjlist/query" {
|
||||
t.Fatalf("osi path = %q", *seenPath)
|
||||
}
|
||||
body, _ := io.ReadAll(rec.Body)
|
||||
if !strings.Contains(string(body), `"checkType"`) {
|
||||
t.Fatalf("body missing roster: %s", body)
|
||||
}
|
||||
}
|
||||
@@ -16,11 +16,16 @@ func runServer(cfg config.Config, addr string) error {
|
||||
}
|
||||
|
||||
hr := handler.NewHealthRecordHandler(client)
|
||||
hc := handler.NewHealthCheckHandler(client)
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/api/health-record/find", hr.Find)
|
||||
mux.HandleFunc("/api/health-check/last", hc.Last)
|
||||
mux.HandleFunc("/api/health-check/list", hc.List)
|
||||
|
||||
fmt.Printf("chis_osi server listening on %s\n", addr)
|
||||
fmt.Printf(" GET http://%s/api/health-record/find?idCard=<身份证>\n", addr)
|
||||
fmt.Printf(" GET http://%s/api/health-check/last?idCard=<身份证>\n", addr)
|
||||
fmt.Printf(" GET http://%s/api/health-check/list?checkYear=<年度>&idCard=<身份证>\n", addr)
|
||||
|
||||
server := &http.Server{Addr: addr, Handler: mux}
|
||||
return server.ListenAndServe()
|
||||
|
||||
Reference in New Issue
Block a user