From 86f8d0b64425920bec948a55f0d9b89a2bf2b77c Mon Sep 17 00:00:00 2001 From: huang Date: Sat, 7 Mar 2026 11:29:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=80=82=E9=85=8D=20Gateway=20=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E6=A8=A1=E5=9E=8B=E5=8F=98=E6=9B=B4=EF=BC=8C=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E8=BD=AE=E8=AF=A2=E5=A4=84=E7=90=86=E5=99=A8=E5=92=8C?= =?UTF-8?q?=20Mock=20=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - polling_handler: Status→RealStatus, UsedFlow→Used, parseRealnameStatus 参数改为 bool - mock_gateway: 同步接口路径和响应结构与上游文档一致 --- internal/task/polling_handler.go | 17 +++++++---------- scripts/benchmark/mock_gateway.go | 15 +++++++-------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/internal/task/polling_handler.go b/internal/task/polling_handler.go index 4a72fe7..20aeb4d 100644 --- a/internal/task/polling_handler.go +++ b/internal/task/polling_handler.go @@ -123,11 +123,11 @@ func (h *PollingHandler) HandleRealnameCheck(ctx context.Context, t *asynq.Task) } // 解析实名状态 - newRealnameStatus = h.parseRealnameStatus(result.Status) + newRealnameStatus = h.parseRealnameStatus(result.RealStatus) h.logger.Info("实名检查完成", zap.Uint64("card_id", cardID), zap.String("iccid", card.ICCID), - zap.String("gateway_status", result.Status), + zap.Bool("gateway_real_status", result.RealStatus), zap.Int("new_status", newRealnameStatus), zap.Int("old_status", card.RealNameStatus)) } else { @@ -236,7 +236,7 @@ func (h *PollingHandler) HandleCarddataCheck(ctx context.Context, t *asynq.Task) } // Gateway 返回的是 MB 单位的流量 - gatewayFlowMB = float64(result.UsedFlow) + gatewayFlowMB = result.Used h.logger.Info("流量检查完成", zap.Uint64("card_id", cardID), zap.String("iccid", card.ICCID), @@ -692,15 +692,12 @@ func (h *PollingHandler) stopCards(ctx context.Context, cards []*model.IotCard, } // parseRealnameStatus 解析实名状态 -func (h *PollingHandler) parseRealnameStatus(gatewayStatus string) int { - switch gatewayStatus { - case "已实名", "realname", "1": +// Gateway 返回 bool 类型:true=已实名, false=未实名 +func (h *PollingHandler) parseRealnameStatus(realStatus bool) int { + if realStatus { return 2 // 已实名 - case "实名中", "processing": - return 1 // 实名中 - default: - return 0 // 未实名 } + return 0 // 未实名 } // extractTaskType 从完整的任务类型中提取简短的类型名 diff --git a/scripts/benchmark/mock_gateway.go b/scripts/benchmark/mock_gateway.go index 9c68dd0..e1e37e2 100644 --- a/scripts/benchmark/mock_gateway.go +++ b/scripts/benchmark/mock_gateway.go @@ -45,7 +45,7 @@ func main() { } // 实名查询接口(匹配 gateway client 的路径) - http.HandleFunc("/flow-card/realname-status", handleRealnameQuery) + http.HandleFunc("/flow-card/realName", handleRealnameQuery) // 流量查询接口 http.HandleFunc("/flow-card/flow", handleFlowQuery) @@ -67,7 +67,7 @@ func main() { fmt.Println("模拟响应时间: 200ms - 4s") fmt.Println("") fmt.Println("接口列表:") - fmt.Println(" POST /flow-card/realname-status - 实名查询") + fmt.Println(" POST /flow-card/realName - 实名查询") fmt.Println(" POST /flow-card/flow - 流量查询") fmt.Println(" POST /flow-card/status - 卡状态查询") fmt.Println(" POST /flow-card/cardStop - 停机操作") @@ -112,14 +112,13 @@ func handleRealnameQuery(w http.ResponseWriter, r *http.Request) { // 90% 成功,10% 失败 if rand.Float64() < 0.90 { atomic.AddInt64(&successRequests, 1) - // 随机返回实名状态 - statuses := []string{"未实名", "实名中", "已实名"} - status := statuses[rand.Intn(3)] + // 随机返回实名状态(匹配文档:realStatus 为 bool 类型) + realStatus := rand.Float64() < 0.5 resp := GatewayResponse{ Code: 200, Msg: "success", TraceID: fmt.Sprintf("trace-%d", time.Now().UnixNano()), - Data: json.RawMessage(fmt.Sprintf(`{"status": "%s"}`, status)), + Data: json.RawMessage(fmt.Sprintf(`{"iccid": "mock-iccid", "realStatus": %t}`, realStatus)), } json.NewEncoder(w).Encode(resp) } else { @@ -140,13 +139,13 @@ func handleFlowQuery(w http.ResponseWriter, r *http.Request) { if rand.Float64() < 0.90 { atomic.AddInt64(&successRequests, 1) - // 随机返回流量数据(匹配 FlowUsageResp 结构) + // 随机返回流量数据(匹配文档:used 字段) usedFlow := rand.Intn(10000) resp := GatewayResponse{ Code: 200, Msg: "success", TraceID: fmt.Sprintf("trace-%d", time.Now().UnixNano()), - Data: json.RawMessage(fmt.Sprintf(`{"usedFlow": %d, "unit": "MB"}`, usedFlow)), + Data: json.RawMessage(fmt.Sprintf(`{"iccid": "mock-iccid", "used": %d, "unit": "MB"}`, usedFlow)), } json.NewEncoder(w).Encode(resp) } else {