diff --git a/internal/domains/api/services/processors/yysy/yysybe08_processor.go b/internal/domains/api/services/processors/yysy/yysybe08_processor.go index dcacab1..d5a5cf4 100644 --- a/internal/domains/api/services/processors/yysy/yysybe08_processor.go +++ b/internal/domains/api/services/processors/yysy/yysybe08_processor.go @@ -4,6 +4,8 @@ import ( "context" "encoding/json" "errors" + "strconv" + "strings" "tyapi-server/internal/domains/api/dto" "tyapi-server/internal/domains/api/services/processors" @@ -47,8 +49,8 @@ func ProcessYYSYBE08Request(ctx context.Context, params []byte, deps *processors return nil, errors.Join(processors.ErrSystem, aerr) } var aliyunData struct { - Result *int `json:"result"` - Desc string `json:"desc"` + Result interface{} `json:"result"` + Desc string `json:"desc"` } if err := json.Unmarshal(dataBytes, &aliyunData); err != nil { errorResponse := map[string]interface{}{ @@ -65,23 +67,7 @@ func ProcessYYSYBE08Request(ctx context.Context, params []byte, deps *processors return json.Marshal(errorResponse) } - var resultCode, verifyResult, resultMsg string - switch { - case aliyunData.Result != nil && *aliyunData.Result == 0: - resultCode = "0XXX" - verifyResult = "一致" - resultMsg = aliyunData.Desc - if resultMsg == "" { - resultMsg = "成功" - } - default: - resultCode = "5XXX" - verifyResult = "不一致" - resultMsg = aliyunData.Desc - if resultMsg == "" { - resultMsg = "不一致" - } - } + resultCode, verifyResult, resultMsg := mapIDCardCheckResult(aliyunData.Result, aliyunData.Desc) response := map[string]interface{}{ "ctidRequest": map[string]interface{}{ @@ -101,12 +87,12 @@ func ProcessYYSYBE08Request(ctx context.Context, params []byte, deps *processors // 解析数脉 /v4/id_card/check 的 data 内容(CallAPIForm 返回的即 data 对象) // 数卖响应: result 0-一致 1-不一致 2-无记录(预留); desc 如 "一致"/"不一致" var shumaiData struct { - Result *int `json:"result"` - OrderNo string `json:"order_no"` - Desc string `json:"desc"` - Sex string `json:"sex"` - Birthday string `json:"birthday"` - Address string `json:"address"` + Result interface{} `json:"result"` + OrderNo string `json:"order_no"` + Desc string `json:"desc"` + Sex string `json:"sex"` + Birthday string `json:"birthday"` + Address string `json:"address"` } if err := json.Unmarshal(respBytes, &shumaiData); err != nil { @@ -127,23 +113,7 @@ func ProcessYYSYBE08Request(ctx context.Context, params []byte, deps *processors // 按数卖 result 验证结果处理: 0-一致 1-不一致 2-无记录(预留) // resultCode: 0XXX=一致, 5XXX=不一致/无记录 - var resultCode, verifyResult, resultMsg string - switch { - case shumaiData.Result != nil && *shumaiData.Result == 0: // 一致(收费) - resultCode = "0XXX" - verifyResult = "一致" - resultMsg = shumaiData.Desc - if resultMsg == "" { - resultMsg = "成功" - } - default: - resultCode = "5XXX" - verifyResult = "不一致" - resultMsg = shumaiData.Desc - if resultMsg == "" { - resultMsg = "不一致" - } - } + resultCode, verifyResult, resultMsg := mapIDCardCheckResult(shumaiData.Result, shumaiData.Desc) // 构建目标格式的响应 response := map[string]interface{}{ @@ -160,3 +130,48 @@ func ProcessYYSYBE08Request(ctx context.Context, params []byte, deps *processors return json.Marshal(response) } + +func mapIDCardCheckResult(rawResult interface{}, desc string) (resultCode, verifyResult, resultMsg string) { + if isResultZero(rawResult) { + resultCode = "0XXX" + verifyResult = "一致" + resultMsg = desc + if resultMsg == "" { + resultMsg = "成功" + } + return + } + + resultCode = "5XXX" + verifyResult = "不一致" + resultMsg = desc + if resultMsg == "" { + resultMsg = "不一致" + } + return +} + +func isResultZero(v interface{}) bool { + switch r := v.(type) { + case float64: + return r == 0 + case int: + return r == 0 + case int32: + return r == 0 + case int64: + return r == 0 + case json.Number: + n, err := r.Int64() + return err == nil && n == 0 + case string: + s := strings.TrimSpace(r) + if s == "" { + return false + } + n, err := strconv.ParseFloat(s, 64) + return err == nil && n == 0 + default: + return false + } +} diff --git a/internal/domains/api/services/processors/yysy/yysybe08test_processor.go b/internal/domains/api/services/processors/yysy/yysybe08test_processor.go index 7a20ad4..1fe360b 100644 --- a/internal/domains/api/services/processors/yysy/yysybe08test_processor.go +++ b/internal/domains/api/services/processors/yysy/yysybe08test_processor.go @@ -26,30 +26,14 @@ func ProcessYYSYBE08testRequest(ctx context.Context, params []byte, deps *proces } var aliyunData struct { - Result *int `json:"result"` - Desc string `json:"desc"` + Result interface{} `json:"result"` + Desc string `json:"desc"` } if err := json.Unmarshal(respBytes, &aliyunData); err != nil { return nil, errors.Join(processors.ErrSystem, err) } - var resultCode, verifyResult, resultMsg string - switch { - case aliyunData.Result != nil && *aliyunData.Result == 0: - resultCode = "0XXX" - verifyResult = "一致" - resultMsg = aliyunData.Desc - if resultMsg == "" { - resultMsg = "成功" - } - default: - resultCode = "5XXX" - verifyResult = "不一致" - resultMsg = aliyunData.Desc - if resultMsg == "" { - resultMsg = "不一致" - } - } + resultCode, verifyResult, resultMsg := mapIDCardCheckResult(aliyunData.Result, aliyunData.Desc) response := map[string]interface{}{ "ctidRequest": map[string]interface{}{ diff --git a/internal/infrastructure/external/alicloud/alicloud_service.go b/internal/infrastructure/external/alicloud/alicloud_service.go index bfd852d..913c28d 100644 --- a/internal/infrastructure/external/alicloud/alicloud_service.go +++ b/internal/infrastructure/external/alicloud/alicloud_service.go @@ -97,14 +97,13 @@ func (a *AlicloudService) CallAPI(path string, params map[string]interface{}) (r isTimeout := false if netErr, ok := err.(interface{ Timeout() bool }); ok && netErr.Timeout() { isTimeout = true - } else if errStr := err.Error(); - errStr == "context deadline exceeded" || - errStr == "timeout" || - errStr == "Client.Timeout exceeded" || - errStr == "net/http: request canceled" { + } else if errStr := err.Error(); errStr == "context deadline exceeded" || + errStr == "timeout" || + errStr == "Client.Timeout exceeded" || + errStr == "net/http: request canceled" { isTimeout = true } - + if isTimeout { if a.logger != nil { a.logger.LogError(requestID, transactionID, path, errors.Join(ErrDatasource, fmt.Errorf("API请求超时: %s", err.Error())), params) @@ -140,4 +139,4 @@ func (a *AlicloudService) CallAPI(path string, params map[string]interface{}) (r // GetConfig 获取配置信息 func (a *AlicloudService) GetConfig() AlicloudConfig { return a.config -} \ No newline at end of file +}