fix
This commit is contained in:
@@ -26,6 +26,8 @@ var APIEndpoints = map[string]string{
|
||||
"FinancingHistory": "/open/cd/findHistoryRongzi/2.0", // 融资历史
|
||||
"PunishmentInfo": "/open/mr/punishmentInfo/3.0", // 行政处罚
|
||||
"AbnormalInfo": "/open/mr/abnormal/2.0", // 经营异常
|
||||
"OwnTax": "/open/mr/ownTax", // 欠税公告
|
||||
"TaxContravention": "/open/mr/taxContravention", // 税收违法
|
||||
}
|
||||
|
||||
// TianYanChaConfig 天眼查配置
|
||||
@@ -74,7 +76,7 @@ func (t *TianYanChaService) CallAPI(ctx context.Context, apiCode string, params
|
||||
// 从映射中获取 API 端点
|
||||
endpoint, exists := APIEndpoints[apiCode]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("%w: 未找到 API 代码对应的端点: %s", ErrInvalidParam, apiCode)
|
||||
return nil, errors.Join(ErrInvalidParam, fmt.Errorf("未找到 API 代码对应的端点: %s", apiCode))
|
||||
}
|
||||
|
||||
// 构建完整 URL
|
||||
@@ -82,7 +84,7 @@ func (t *TianYanChaService) CallAPI(ctx context.Context, apiCode string, params
|
||||
|
||||
// 检查 Token 是否配置
|
||||
if t.config.Token == "" {
|
||||
return nil, fmt.Errorf("%w: 天眼查 API Token 未配置", ErrSystem)
|
||||
return nil, errors.Join(ErrSystem, fmt.Errorf("天眼查 API Token 未配置"))
|
||||
}
|
||||
|
||||
// 构建查询参数
|
||||
@@ -100,7 +102,7 @@ func (t *TianYanChaService) CallAPI(ctx context.Context, apiCode string, params
|
||||
// 创建请求
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", requestURL, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: 创建请求失败: %v", ErrSystem, err)
|
||||
return nil, errors.Join(ErrSystem, fmt.Errorf("创建请求失败: %v", err))
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
@@ -110,29 +112,34 @@ func (t *TianYanChaService) CallAPI(ctx context.Context, apiCode string, params
|
||||
client := &http.Client{Timeout: t.config.Timeout}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: API 请求异常: %v", ErrDatasource, err)
|
||||
return nil, errors.Join(ErrDatasource, fmt.Errorf("API 请求异常: %v", err))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 检查 HTTP 状态码
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("%w: API 请求失败,状态码: %d", ErrDatasource, resp.StatusCode)
|
||||
return nil, errors.Join(ErrDatasource, fmt.Errorf("API 请求失败,状态码: %d", resp.StatusCode))
|
||||
}
|
||||
|
||||
// 读取响应体
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: 读取响应体失败: %v", ErrSystem, err)
|
||||
return nil, errors.Join(ErrSystem, fmt.Errorf("读取响应体失败: %v", err))
|
||||
}
|
||||
|
||||
// 解析 JSON 响应
|
||||
var tianYanChaResp TianYanChaResponse
|
||||
if err := json.Unmarshal(body, &tianYanChaResp); err != nil {
|
||||
return nil, fmt.Errorf("%w: 解析响应 JSON 失败: %v", ErrSystem, err)
|
||||
return nil, errors.Join(ErrSystem, fmt.Errorf("解析响应 JSON 失败: %v", err))
|
||||
}
|
||||
|
||||
// 检查天眼查业务状态码
|
||||
if tianYanChaResp.ErrorCode != 0 {
|
||||
// 特殊处理:ErrorCode 300000 表示查询为空,返回ErrNotFound
|
||||
if tianYanChaResp.ErrorCode == 300000 {
|
||||
return nil, errors.Join(ErrNotFound, fmt.Errorf("天眼查查询为空: %s", tianYanChaResp.Reason))
|
||||
}
|
||||
|
||||
return &APIResponse{
|
||||
Success: false,
|
||||
Code: tianYanChaResp.ErrorCode,
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
var (
|
||||
ErrDatasource = errors.New("数据源异常")
|
||||
ErrSystem = errors.New("系统异常")
|
||||
ErrNotFound = errors.New("查询为空")
|
||||
)
|
||||
|
||||
type WestResp struct {
|
||||
@@ -321,8 +322,8 @@ func (w *WestDexService) G05HZ01CallAPI(ctx context.Context, code string, reqDat
|
||||
}
|
||||
|
||||
if westDexResp.Code != "0000" {
|
||||
if westDexResp.Data == nil {
|
||||
err = errors.Join(ErrSystem, fmt.Errorf(westDexResp.Message))
|
||||
if westDexResp.Data == nil || westDexResp.Code == "1404" {
|
||||
err = errors.Join(ErrNotFound, fmt.Errorf(westDexResp.Message))
|
||||
if w.logger != nil {
|
||||
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user