fix
This commit is contained in:
@@ -60,8 +60,6 @@ Applepay:
|
||||
LoadPrivateKeyPath: "etc/merchant/AuthKey_LAY65829DQ.p8"
|
||||
Ali:
|
||||
Code: "d55b58829efb41c8aa8e86769cba4844"
|
||||
SystemConfig:
|
||||
ThreeVerify: false
|
||||
AdminConfig:
|
||||
AccessSecret: "jK8nP3qR7tV2xZ5aB9cD1eF6gH4iJ0kL8mN5oP6qR7sT"
|
||||
AccessExpire: 604800
|
||||
@@ -87,3 +85,8 @@ Log:
|
||||
- "stat" # 统计日志
|
||||
- "business" # 业务日志
|
||||
- "security" # 安全日志
|
||||
Tianyuanapi:
|
||||
AccessID: "3c042bb99b240ccc"
|
||||
Key: "2732f526167c2de9b8dc6aa0f24ba8b7"
|
||||
BaseURL: "https://api.tianyuanapi.com"
|
||||
Timeout: 60
|
||||
|
||||
@@ -62,8 +62,6 @@ Applepay:
|
||||
LoadPrivateKeyPath: "etc/merchant/AuthKey_LAY65829DQ.p8"
|
||||
Ali:
|
||||
Code: "d55b58829efb41c8aa8e86769cba4844"
|
||||
SystemConfig:
|
||||
ThreeVerify: false
|
||||
AdminConfig:
|
||||
AccessSecret: "jK8nP3qR7tV2xZ5aB9cD1eF6gH4iJ0kL8mN5oP6qR7sT"
|
||||
AccessExpire: 604800
|
||||
@@ -74,3 +72,8 @@ CleanTask:
|
||||
Enabled: true
|
||||
Time: "0 3 * * *"
|
||||
Days: 15
|
||||
Tianyuanapi:
|
||||
AccessID: "3c042bb99b240ccc"
|
||||
Key: "2732f526167c2de9b8dc6aa0f24ba8b7"
|
||||
BaseURL: "https://api.tianyuanapi.com"
|
||||
Timeout: 60
|
||||
|
||||
@@ -19,10 +19,10 @@ type Config struct {
|
||||
WestConfig WestConfig
|
||||
YushanConfig YushanConfig
|
||||
TianjuConfig TianjuConfig
|
||||
SystemConfig SystemConfig
|
||||
AdminConfig AdminConfig
|
||||
AdminPromotion AdminPromotion
|
||||
CleanTask CleanTask
|
||||
Tianyuanapi TianyuanapiConfig
|
||||
}
|
||||
|
||||
// JwtAuth 用于 JWT 鉴权配置
|
||||
@@ -91,9 +91,6 @@ type TianjuConfig struct {
|
||||
ApiKey string
|
||||
BaseURL string
|
||||
}
|
||||
type SystemConfig struct {
|
||||
ThreeVerify bool
|
||||
}
|
||||
|
||||
type AdminConfig struct {
|
||||
AccessSecret string
|
||||
@@ -109,3 +106,9 @@ type CleanTask struct {
|
||||
Time string
|
||||
Days int
|
||||
}
|
||||
type TianyuanapiConfig struct {
|
||||
AccessID string
|
||||
Key string
|
||||
BaseURL string
|
||||
Timeout int64
|
||||
}
|
||||
|
||||
@@ -75,15 +75,10 @@ func (l *QueryServiceLogic) VerifyCode(mobile string, code string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 二、三要素验证
|
||||
// 三要素验证
|
||||
func (l *QueryServiceLogic) Verify(Name string, IDCard string, Mobile string) error {
|
||||
if l.svcCtx.Config.SystemConfig.ThreeVerify {
|
||||
// 三要素验证
|
||||
return l.VerifyThreeFactors(Name, IDCard, Mobile)
|
||||
} else {
|
||||
// 二要素验证
|
||||
return l.VerifyTwoFactors(Name, IDCard)
|
||||
}
|
||||
// 三要素验证
|
||||
return l.VerifyThreeFactors(Name, IDCard, Mobile)
|
||||
}
|
||||
|
||||
// VerifyTwoFactors 二要素验证
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
@@ -15,9 +14,9 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"tyc-server/app/main/api/internal/config"
|
||||
tianyuanapi "tyc-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"tyc-server/app/main/model"
|
||||
"tyc-server/pkg/lzkit/crypto"
|
||||
"tyc-server/pkg/lzkit/lzUtils"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/tidwall/gjson"
|
||||
@@ -29,12 +28,13 @@ type ApiRequestService struct {
|
||||
westDexService *WestDexService
|
||||
yushanService *YushanService
|
||||
tianjuService *TianjuService
|
||||
tianyuanapiService *tianyuanapi.Client
|
||||
featureModel model.FeatureModel
|
||||
productFeatureModel model.ProductFeatureModel
|
||||
}
|
||||
|
||||
// NewApiRequestService 是一个构造函数,用于初始化 ApiRequestService
|
||||
func NewApiRequestService(c config.Config, westDexService *WestDexService, yushanService *YushanService, tianjuService *TianjuService, featureModel model.FeatureModel, productFeatureModel model.ProductFeatureModel) *ApiRequestService {
|
||||
func NewApiRequestService(c config.Config, westDexService *WestDexService, yushanService *YushanService, tianjuService *TianjuService, tianyuanapiService *tianyuanapi.Client, featureModel model.FeatureModel, productFeatureModel model.ProductFeatureModel) *ApiRequestService {
|
||||
return &ApiRequestService{
|
||||
config: c,
|
||||
featureModel: featureModel,
|
||||
@@ -42,6 +42,7 @@ func NewApiRequestService(c config.Config, westDexService *WestDexService, yusha
|
||||
westDexService: westDexService,
|
||||
yushanService: yushanService,
|
||||
tianjuService: tianjuService,
|
||||
tianyuanapiService: tianyuanapiService,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,14 +266,16 @@ func (a *ApiRequestService) ProcessG09SC02Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"certNumMan": a.westDexService.Encrypt(idCard.String()),
|
||||
"nameMan": a.westDexService.Encrypt(name.String()),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G09SC02", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("IVYZ5733", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := gjson.GetBytes(resp, "data.0.maritalStatus")
|
||||
|
||||
@@ -300,15 +303,17 @@ func (a *ApiRequestService) ProcessG27BJ05Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"id": a.westDexService.Encrypt(idCard.String()),
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"cell": a.westDexService.Encrypt(mobile.String()),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
"mobile_no": mobile.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G27BJ05", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("JRZQ0A03", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 获取 code 字段
|
||||
codeResult := gjson.GetBytes(resp, "code")
|
||||
@@ -355,16 +360,18 @@ func (a *ApiRequestService) ProcessG26BJ05Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"id": a.westDexService.Encrypt(idCard.String()),
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"cell": a.westDexService.Encrypt(mobile.String()),
|
||||
"time_range": 5,
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
"mobile_no": mobile.String(),
|
||||
"time_range": "5",
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G26BJ05", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("G26BJ05", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
codeResult := gjson.GetBytes(resp, "code")
|
||||
if !codeResult.Exists() {
|
||||
@@ -409,14 +416,16 @@ func (a *ApiRequestService) ProcessG34BJ03Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"id_card": a.westDexService.Encrypt(idCard.String()),
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
},
|
||||
"id_card": idCard.String(),
|
||||
"name": name.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G34BJ03", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("FLXG0V3B", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dataResult := gjson.GetBytes(resp, "negative_info.data.risk_level")
|
||||
if dataResult.Exists() {
|
||||
@@ -443,15 +452,16 @@ func (a *ApiRequestService) ProcessG35SC01Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"idcard": a.westDexService.Encrypt(idCard.String()),
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"inquired_auth": a.westDexService.GetDateRange(),
|
||||
},
|
||||
"id_card": idCard.String(),
|
||||
"name": name.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G35SC01", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("FLXGCA3D", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 第一步:提取外层的 data 字段
|
||||
dataResult := gjson.GetBytes(resp, "data")
|
||||
@@ -501,15 +511,17 @@ func (a *ApiRequestService) ProcessG28BJ05Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"id": a.westDexService.Encrypt(idCard.String()),
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"cell": a.westDexService.Encrypt(mobile.String()),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
"mobile_no": mobile.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G28BJ05", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("JRZQ8203", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 获取 code 字段
|
||||
codeResult := gjson.GetBytes(resp, "code")
|
||||
@@ -592,17 +604,19 @@ func (a *ApiRequestService) ProcessQ23SC01Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"uscc": a.westDexService.Encrypt(entCode.String()),
|
||||
"org_name": a.westDexService.Encrypt(entName.String()),
|
||||
"inquired_auth": a.westDexService.GetDateRange(),
|
||||
},
|
||||
"ent_code": entCode.String(),
|
||||
"ent_name": entName.String(),
|
||||
"inquired_auth": a.westDexService.GetDateRange(),
|
||||
}
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("QYGL8261", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("Q23SC01", request)
|
||||
logx.Infof("企业涉诉返回%+v", string(resp))
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
}
|
||||
// 第一步:提取外层的 data 字段
|
||||
dataResult := gjson.GetBytes(resp, "data")
|
||||
if !dataResult.Exists() {
|
||||
@@ -652,15 +666,17 @@ func (a *ApiRequestService) ProcessG15BJ02Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"idNo": a.westDexService.Encrypt(idCard.String()),
|
||||
"phone": a.westDexService.Encrypt(mobile.String()),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
"mobile_no": mobile.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G15BJ02", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("YYSY6F2E", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dataResult := gjson.GetBytes(resp, "data.code")
|
||||
if !dataResult.Exists() {
|
||||
@@ -686,14 +702,16 @@ func (a *ApiRequestService) ProcessG17BJ02Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"phone": a.westDexService.Encrypt(mobile.String()),
|
||||
},
|
||||
"name": name.String(),
|
||||
"mobile_no": mobile.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G17BJ02", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("YYSYBE08", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dataResult := gjson.GetBytes(resp, "data.code")
|
||||
if !dataResult.Exists() {
|
||||
@@ -719,14 +737,16 @@ func (a *ApiRequestService) ProcessG08SC02Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"xm": a.westDexService.Encrypt(name.String()),
|
||||
"gmsfzhm": a.westDexService.Encrypt(idCard.String()),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G08SC02", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("IVYZ385E", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &APIInternalResult{
|
||||
Data: resp,
|
||||
@@ -1081,42 +1101,57 @@ func (a *ApiRequestService) ProcessG10SC02Request(ctx context.Context, params []
|
||||
|
||||
// 构造请求数据
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"certNumMan": a.westDexService.Encrypt(idCardMan.String()),
|
||||
"nameMan": a.westDexService.Encrypt(nameMan.String()),
|
||||
"certNumWoman": a.westDexService.Encrypt(idCardWoman.String()),
|
||||
"nameWoman": a.westDexService.Encrypt(nameWoman.String()),
|
||||
},
|
||||
"man_id_card": idCardMan.String(),
|
||||
"man_name": nameMan.String(),
|
||||
"woman_id_card": idCardWoman.String(),
|
||||
"woman_name": nameWoman.String(),
|
||||
}
|
||||
|
||||
// 调用 API
|
||||
resp, callApiErr := a.westDexService.CallAPI("G10SC02", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("IVYZ9363", request)
|
||||
if err != nil && tianyuanapiResp == nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 解析响应数据
|
||||
code := gjson.GetBytes(resp, "code").String()
|
||||
|
||||
// 状态码校验
|
||||
if code != "200" {
|
||||
return nil, fmt.Errorf("婚姻查询失败:%s", string(resp))
|
||||
result := gjson.GetBytes(resp, "data.data")
|
||||
if !result.Exists() {
|
||||
return nil, fmt.Errorf("婚姻状态查询失败")
|
||||
}
|
||||
|
||||
result := gjson.GetBytes(resp, "data.0.maritalStatus")
|
||||
// 获取原始结果
|
||||
rawResult := result.String()
|
||||
|
||||
if result.Exists() {
|
||||
responseMap := map[string]string{"status": result.String()}
|
||||
jsonResponse, err := json.Marshal(responseMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &APIInternalResult{
|
||||
Data: jsonResponse,
|
||||
}, nil
|
||||
} else {
|
||||
return nil, errors.New("查询为空")
|
||||
// 根据结果转换状态码
|
||||
var statusCode string
|
||||
switch {
|
||||
case strings.HasPrefix(rawResult, "INR"):
|
||||
statusCode = "0" // 匹配不成功
|
||||
case strings.HasPrefix(rawResult, "IA"):
|
||||
statusCode = "1" // 结婚
|
||||
case strings.HasPrefix(rawResult, "IB"):
|
||||
statusCode = "2" // 离婚
|
||||
default:
|
||||
return nil, fmt.Errorf("婚姻状态查询失败,未知状态码: %s", statusCode)
|
||||
}
|
||||
|
||||
// 构建新的返回结果
|
||||
response := map[string]string{
|
||||
"status": statusCode,
|
||||
}
|
||||
|
||||
// 序列化为JSON
|
||||
jsonResponse, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("序列化结果失败: %v", err)
|
||||
}
|
||||
|
||||
return &APIInternalResult{
|
||||
Data: jsonResponse,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 手机号码风险
|
||||
@@ -1128,13 +1163,15 @@ func (a *ApiRequestService) ProcessG03HZ01Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"mobile": a.westDexService.Encrypt(mobile.String()),
|
||||
},
|
||||
"mobile_no": mobile.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G03HZ01", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("FLXG54F5", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 获取 code 字段
|
||||
codeResult := gjson.GetBytes(resp, "code")
|
||||
@@ -1159,13 +1196,15 @@ func (a *ApiRequestService) ProcessG02BJ02Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"phone": a.westDexService.Encrypt(mobile.String()),
|
||||
},
|
||||
"mobile_no": mobile.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G02BJ02", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("YYSY4B37", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 获取 code 字段
|
||||
codeResult := gjson.GetBytes(resp, "code")
|
||||
@@ -1191,14 +1230,16 @@ func (a *ApiRequestService) ProcessG19BJ02Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"phone": a.westDexService.Encrypt(mobile.String()),
|
||||
"startDate": startDate.String(),
|
||||
},
|
||||
"mobile_no": mobile.String(),
|
||||
"start_date": startDate.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G19BJ02", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("YYSYF7DB", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 获取 code 字段
|
||||
codeResult := gjson.GetBytes(resp, "code")
|
||||
@@ -1226,16 +1267,18 @@ func (a *ApiRequestService) ProcessG20GZ01Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"idcard": a.westDexService.Encrypt(idCard.String()),
|
||||
"acc_no": a.westDexService.Encrypt(bankCard.String()),
|
||||
"mobile": a.westDexService.Encrypt(mobile.String()),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
"bank_card": bankCard.String(),
|
||||
"mobile_no": mobile.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G20GZ01", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("JRZQDCBE", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 获取 code 字段
|
||||
codeResult := gjson.GetBytes(resp, "code")
|
||||
@@ -1248,7 +1291,7 @@ func (a *ApiRequestService) ProcessG20GZ01Request(ctx context.Context, params []
|
||||
}
|
||||
// 解析 data.Raw 字符串为接口类型
|
||||
var parsedData interface{}
|
||||
err := json.Unmarshal([]byte(data.String()), &parsedData)
|
||||
err = json.Unmarshal([]byte(data.String()), &parsedData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("解析 data 失败: %v", err)
|
||||
}
|
||||
@@ -1274,29 +1317,21 @@ func (a *ApiRequestService) ProcessG37SC01Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"idcard": a.westDexService.Encrypt(idCard.String()),
|
||||
"auth_authorizeFileCode": a.westDexService.Encrypt("CON0120250730743901"),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G37SC01", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("FLXG8A3F", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parseResult, parseErr := lzUtils.ParseJsonResponse(resp)
|
||||
if parseErr != nil {
|
||||
return nil, parseErr
|
||||
}
|
||||
// 通过gjson获取指定路径的数据
|
||||
contentResult := gjson.GetBytes(parseResult, "G37SC0101.G37SC0102.content")
|
||||
if contentResult.Exists() {
|
||||
return &APIInternalResult{
|
||||
Data: []byte(contentResult.Raw),
|
||||
}, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("找不到数据")
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &APIInternalResult{
|
||||
Data: resp,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// G36SC01 自然人限高信息
|
||||
@@ -1309,29 +1344,21 @@ func (a *ApiRequestService) ProcessG36SC01Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"idcard": a.westDexService.Encrypt(idCard.String()),
|
||||
"auth_authorizeFileCode": a.westDexService.Encrypt("CON0120250730743901"),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G36SC01", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("FLXG5B2E", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parseResult, parseErr := lzUtils.ParseJsonResponse(resp)
|
||||
if parseErr != nil {
|
||||
return nil, parseErr
|
||||
}
|
||||
// 通过gjson获取指定路径的数据
|
||||
contentResult := gjson.GetBytes(parseResult, "G36SC0101.G36SC0102.content")
|
||||
if contentResult.Exists() {
|
||||
return &APIInternalResult{
|
||||
Data: []byte(contentResult.Raw),
|
||||
}, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("找不到数据")
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &APIInternalResult{
|
||||
Data: resp,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// G22SC01 自然人司法模型
|
||||
@@ -1344,32 +1371,21 @@ func (a *ApiRequestService) ProcessG22SC01Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"idcard": a.westDexService.Encrypt(idCard.String()),
|
||||
"auth_authorizeFileCode": a.westDexService.Encrypt("CON0120250730743901"),
|
||||
"inquired_auth": fmt.Sprintf("authed:%s", a.westDexService.GetDateRange()),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
}
|
||||
log.Printf("G22SC01 request: %+v", request)
|
||||
resp, callApiErr := a.westDexService.CallAPI("G22SC01", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("FLXG0V4B", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 正常返回 - 不管有没有deps.Options.Json都进行ParseJsonResponse
|
||||
parseResult, parseErr := lzUtils.ParseJsonResponse(resp)
|
||||
if parseErr != nil {
|
||||
return nil, parseErr
|
||||
}
|
||||
// 通过gjson获取指定路径的数据
|
||||
contentResult := gjson.GetBytes(parseResult, "G22SC0101.G22SC0102.content")
|
||||
if contentResult.Exists() {
|
||||
return &APIInternalResult{
|
||||
Data: []byte(contentResult.Raw),
|
||||
}, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("找不到数据")
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &APIInternalResult{
|
||||
Data: resp,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Q03SC01 企业涉诉信息
|
||||
@@ -1382,30 +1398,22 @@ func (a *ApiRequestService) ProcessQ03SC01Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"uscc": a.westDexService.Encrypt(entCode.String()),
|
||||
"org_name": a.westDexService.Encrypt(entName.String()),
|
||||
"auth_authorizeFileCode": a.westDexService.Encrypt("CON0120250730743901"),
|
||||
"inquired_auth": fmt.Sprintf("authed:%s", a.westDexService.GetDateRange()),
|
||||
},
|
||||
"ent_code": entCode.String(),
|
||||
"ent_name": entName.String(),
|
||||
"inquired_auth": fmt.Sprintf("authed:%s", a.westDexService.GetDateRange()),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("Q03SC01", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("QYGL8271", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parseResult, parseErr := lzUtils.ParseJsonResponse(resp)
|
||||
if parseErr != nil {
|
||||
return nil, parseErr
|
||||
}
|
||||
// 通过gjson获取指定路径的数据
|
||||
contentResult := gjson.GetBytes(parseResult, "Q03SC0101.Q03SC0102.content")
|
||||
if contentResult.Exists() {
|
||||
return &APIInternalResult{
|
||||
Data: []byte(contentResult.Raw),
|
||||
}, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("找不到数据")
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &APIInternalResult{
|
||||
Data: resp,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 出境限制查询
|
||||
@@ -1729,16 +1737,17 @@ func (a *ApiRequestService) ProcessLayoutIdcardRequest(ctx context.Context, para
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"xM": a.westDexService.Encrypt(name.String()),
|
||||
"gMSFZHM": a.westDexService.Encrypt(idCard.String()),
|
||||
"customerNumber": a.config.WestConfig.SecretId,
|
||||
"timeStamp": fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond)),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("layoutIdcard", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
// 注意:layoutIdcard在api.json中没有对应的serviceId,需要根据实际情况调整
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("YYSYBE08", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 使用gjson获取resultCode
|
||||
resultCode := gjson.GetBytes(resp, "ctidRequest.ctidAuth.resultCode")
|
||||
@@ -2222,15 +2231,17 @@ func (a *ApiRequestService) ProcessG30BJ05Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"id": a.westDexService.Encrypt(idCard.String()),
|
||||
"cell": a.westDexService.Encrypt(Mobile.String()),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
"mobile_no": Mobile.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G30BJ05", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("FLXGC9D1", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println(resp)
|
||||
flagBlackgraylevel := gjson.GetBytes(resp, "flag_blackgraylevel")
|
||||
@@ -2259,15 +2270,17 @@ func (a *ApiRequestService) ProcessG31BJ05Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"id": a.westDexService.Encrypt(idCard.String()),
|
||||
"cell": a.westDexService.Encrypt(Mobile.String()),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
"mobile_no": Mobile.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G31BJ05", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("FLXG9687", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println(resp)
|
||||
flagTelefraudpredictstd := gjson.GetBytes(resp, "flag_telefraudpredictstd")
|
||||
@@ -2296,15 +2309,17 @@ func (a *ApiRequestService) ProcessG32BJ05Request(ctx context.Context, params []
|
||||
}
|
||||
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"id": a.westDexService.Encrypt(idCard.String()),
|
||||
"cell": a.westDexService.Encrypt(Mobile.String()),
|
||||
},
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
"mobile_no": Mobile.String(),
|
||||
}
|
||||
resp, callApiErr := a.westDexService.CallAPI("G32BJ05", request)
|
||||
if callApiErr != nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("FLXG162A", request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
flagFraudrelation := gjson.GetBytes(resp, "flag_fraudrelation")
|
||||
@@ -2342,16 +2357,19 @@ func (a *ApiRequestService) ProcessG09XM02Request(ctx context.Context, params []
|
||||
if !idCard.Exists() || !name.Exists() {
|
||||
return nil, errors.New("api请求, G09XM02, 获取相关参数失败")
|
||||
}
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"idCard": a.westDexService.Encrypt(idCard.String()),
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
},
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("IVYZ5733", map[string]interface{}{
|
||||
"id_card": idCard.String(),
|
||||
"name": name.String(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := a.westDexService.CallAPI("G09XM02", request)
|
||||
if err != nil && resp == nil {
|
||||
return nil, fmt.Errorf("婚姻状态查询失败: %v", err)
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := gjson.GetBytes(resp, "data.data")
|
||||
if !result.Exists() {
|
||||
return nil, fmt.Errorf("婚姻状态查询失败")
|
||||
@@ -2403,18 +2421,20 @@ func (a *ApiRequestService) ProcessG10XM02Request(ctx context.Context, params []
|
||||
|
||||
// 构造请求数据
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"idCardMan": a.westDexService.Encrypt(idCardMan.String()),
|
||||
"nameMan": a.westDexService.Encrypt(nameMan.String()),
|
||||
"idCardWoman": a.westDexService.Encrypt(idCardWoman.String()),
|
||||
"nameWoman": a.westDexService.Encrypt(nameWoman.String()),
|
||||
},
|
||||
"man_id_card": idCardMan.String(),
|
||||
"man_name": nameMan.String(),
|
||||
"woman_id_card": idCardWoman.String(),
|
||||
"woman_name": nameWoman.String(),
|
||||
}
|
||||
|
||||
// 调用 API
|
||||
resp, callApiErr := a.westDexService.CallAPI("G10XM02", request)
|
||||
if callApiErr != nil && resp == nil {
|
||||
return nil, callApiErr
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("IVYZ9363", request)
|
||||
if err != nil && tianyuanapiResp == nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := gjson.GetBytes(resp, "data.data")
|
||||
@@ -2460,15 +2480,17 @@ func (a *ApiRequestService) ProcessG11BJ06Request(ctx context.Context, params []
|
||||
return nil, errors.New("api请求, G11BJ06, 获取相关参数失败")
|
||||
}
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"id_card_value": a.westDexService.Encrypt(idCard.String()),
|
||||
"name_value": a.westDexService.Encrypt(name.String()),
|
||||
},
|
||||
"id_card": name.String(),
|
||||
"name": name.String(),
|
||||
}
|
||||
resp, err := a.westDexService.CallAPI("G11BJ06", request)
|
||||
if err != nil && resp == nil {
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("IVYZ9A2B", request)
|
||||
if err != nil && tianyuanapiResp == nil {
|
||||
return nil, fmt.Errorf("教育经历核验查询失败: %v", err)
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 解析响应
|
||||
codeResult := gjson.GetBytes(resp, "data.education_background.code")
|
||||
@@ -2529,16 +2551,18 @@ func (a *ApiRequestService) ProcessG29BJ05Request(ctx context.Context, params []
|
||||
return nil, errors.New("api请求, G29BJ05, 获取相关参数失败")
|
||||
}
|
||||
request := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"id": a.westDexService.Encrypt(idCard.String()),
|
||||
"name": a.westDexService.Encrypt(name.String()),
|
||||
"cell": a.westDexService.Encrypt(mobile.String()),
|
||||
},
|
||||
"id_card": idCard.String(),
|
||||
"name": name.String(),
|
||||
"mobile_no": mobile.String(),
|
||||
}
|
||||
resp, err := a.westDexService.CallAPI("G29BJ05", request)
|
||||
if err != nil && resp == nil {
|
||||
tianyuanapiResp, err := a.tianyuanapiService.CallInterface("JRZQ4AA8", request)
|
||||
if err != nil && tianyuanapiResp == nil {
|
||||
return nil, fmt.Errorf("偿贷压力查询失败: %v", err)
|
||||
}
|
||||
resp, err := convertTianyuanResponse(tianyuanapiResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 获取响应码和偿贷压力标志
|
||||
code := gjson.GetBytes(resp, "code").String()
|
||||
flagDebtRepayStress := gjson.GetBytes(resp, "flag_debtrepaystress").String()
|
||||
@@ -2565,3 +2589,8 @@ func (a *ApiRequestService) ProcessG29BJ05Request(ctx context.Context, params []
|
||||
Data: jsonResult,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 辅助函数:将天远API响应转换为JSON字节数组
|
||||
func convertTianyuanResponse(resp *tianyuanapi.Response) ([]byte, error) {
|
||||
return json.Marshal(resp.Data)
|
||||
}
|
||||
|
||||
416
app/main/api/internal/service/tianyuanapi_sdk/client.go
Normal file
416
app/main/api/internal/service/tianyuanapi_sdk/client.go
Normal file
@@ -0,0 +1,416 @@
|
||||
package tianyuanapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// API调用相关错误类型
|
||||
var (
|
||||
ErrQueryEmpty = errors.New("查询为空")
|
||||
ErrSystem = errors.New("接口异常")
|
||||
ErrDecryptFail = errors.New("解密失败")
|
||||
ErrRequestParam = errors.New("请求参数结构不正确")
|
||||
ErrInvalidParam = errors.New("参数校验不正确")
|
||||
ErrInvalidIP = errors.New("未经授权的IP")
|
||||
ErrMissingAccessId = errors.New("缺少Access-Id")
|
||||
ErrInvalidAccessId = errors.New("未经授权的AccessId")
|
||||
ErrFrozenAccount = errors.New("账户已冻结")
|
||||
ErrArrears = errors.New("账户余额不足,无法请求")
|
||||
ErrProductNotFound = errors.New("产品不存在")
|
||||
ErrProductDisabled = errors.New("产品已停用")
|
||||
ErrNotSubscribed = errors.New("未订阅此产品")
|
||||
ErrBusiness = errors.New("业务失败")
|
||||
)
|
||||
|
||||
// 错误码映射 - 严格按照用户要求
|
||||
var ErrorCodeMap = map[error]int{
|
||||
ErrQueryEmpty: 1000,
|
||||
ErrSystem: 1001,
|
||||
ErrDecryptFail: 1002,
|
||||
ErrRequestParam: 1003,
|
||||
ErrInvalidParam: 1003,
|
||||
ErrInvalidIP: 1004,
|
||||
ErrMissingAccessId: 1005,
|
||||
ErrInvalidAccessId: 1006,
|
||||
ErrFrozenAccount: 1007,
|
||||
ErrArrears: 1007,
|
||||
ErrProductNotFound: 1008,
|
||||
ErrProductDisabled: 1008,
|
||||
ErrNotSubscribed: 1008,
|
||||
ErrBusiness: 2001,
|
||||
}
|
||||
|
||||
// ApiCallOptions API调用选项
|
||||
type ApiCallOptions struct {
|
||||
Json bool `json:"json,omitempty"` // 是否返回JSON格式
|
||||
}
|
||||
|
||||
// Client 天元API客户端
|
||||
type Client struct {
|
||||
accessID string
|
||||
key string
|
||||
baseURL string
|
||||
timeout time.Duration
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// Config 客户端配置
|
||||
type Config struct {
|
||||
AccessID string // 访问ID
|
||||
Key string // AES密钥(16进制)
|
||||
BaseURL string // API基础URL
|
||||
Timeout time.Duration // 超时时间
|
||||
}
|
||||
|
||||
// Request 请求参数
|
||||
type Request struct {
|
||||
InterfaceName string `json:"interfaceName"` // 接口名称
|
||||
Params map[string]interface{} `json:"params"` // 请求参数
|
||||
Timeout int `json:"timeout"` // 超时时间(毫秒)
|
||||
Options *ApiCallOptions `json:"options"` // 调用选项
|
||||
}
|
||||
|
||||
// ApiResponse HTTP API响应
|
||||
type ApiResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
TransactionID string `json:"transaction_id"` // 流水号
|
||||
Data string `json:"data"` // 加密的数据
|
||||
}
|
||||
|
||||
// Response Call方法的响应
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Success bool `json:"success"`
|
||||
TransactionID string `json:"transaction_id"` // 流水号
|
||||
Data map[string]interface{} `json:"data"` // 解密后的数据
|
||||
Timeout int64 `json:"timeout"` // 请求耗时(毫秒)
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// NewClient 创建新的客户端实例
|
||||
func NewClient(config Config) (*Client, error) {
|
||||
// 参数校验
|
||||
if config.AccessID == "" {
|
||||
return nil, fmt.Errorf("accessID不能为空")
|
||||
}
|
||||
if config.Key == "" {
|
||||
return nil, fmt.Errorf("key不能为空")
|
||||
}
|
||||
if config.BaseURL == "" {
|
||||
config.BaseURL = "http://127.0.0.1:8080"
|
||||
}
|
||||
if config.Timeout == 0 {
|
||||
config.Timeout = 60 * time.Second
|
||||
}
|
||||
|
||||
// 验证密钥格式
|
||||
if _, err := hex.DecodeString(config.Key); err != nil {
|
||||
return nil, fmt.Errorf("无效的密钥格式,必须是16进制字符串: %v", err)
|
||||
}
|
||||
|
||||
return &Client{
|
||||
accessID: config.AccessID,
|
||||
key: config.Key,
|
||||
baseURL: config.BaseURL,
|
||||
timeout: config.Timeout,
|
||||
client: &http.Client{
|
||||
Timeout: config.Timeout,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Call 调用API接口
|
||||
func (c *Client) Call(req Request) (*Response, error) {
|
||||
startTime := time.Now()
|
||||
|
||||
// 参数校验
|
||||
if err := c.validateRequest(req); err != nil {
|
||||
return nil, fmt.Errorf("请求参数校验失败: %v", err)
|
||||
}
|
||||
|
||||
// 加密参数
|
||||
jsonData, err := json.Marshal(req.Params)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("参数序列化失败: %v", err)
|
||||
}
|
||||
|
||||
encryptedData, err := c.encrypt(string(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("数据加密失败: %v", err)
|
||||
}
|
||||
|
||||
// 构建请求体
|
||||
requestBody := map[string]interface{}{
|
||||
"data": encryptedData,
|
||||
}
|
||||
|
||||
// 添加选项
|
||||
if req.Options != nil {
|
||||
requestBody["options"] = req.Options
|
||||
} else {
|
||||
// 默认选项
|
||||
defaultOptions := &ApiCallOptions{
|
||||
Json: true,
|
||||
}
|
||||
requestBody["options"] = defaultOptions
|
||||
}
|
||||
|
||||
requestBodyBytes, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求体序列化失败: %v", err)
|
||||
}
|
||||
|
||||
// 创建HTTP请求
|
||||
url := fmt.Sprintf("%s/api/v1/%s", c.baseURL, req.InterfaceName)
|
||||
|
||||
httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBodyBytes))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建HTTP请求失败: %v", err)
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Access-Id", c.accessID)
|
||||
httpReq.Header.Set("User-Agent", "TianyuanAPI-Go-SDK/1.0.0")
|
||||
|
||||
// 发送请求
|
||||
resp, err := c.client.Do(httpReq)
|
||||
if err != nil {
|
||||
endTime := time.Now()
|
||||
requestTime := endTime.Sub(startTime).Milliseconds()
|
||||
return &Response{
|
||||
Success: false,
|
||||
Message: "请求失败",
|
||||
Error: err.Error(),
|
||||
Timeout: requestTime,
|
||||
}, nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 读取响应
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
endTime := time.Now()
|
||||
requestTime := endTime.Sub(startTime).Milliseconds()
|
||||
return &Response{
|
||||
Success: false,
|
||||
Message: "读取响应失败",
|
||||
Error: err.Error(),
|
||||
Timeout: requestTime,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 解析HTTP API响应
|
||||
var apiResp ApiResponse
|
||||
if err := json.Unmarshal(body, &apiResp); err != nil {
|
||||
endTime := time.Now()
|
||||
requestTime := endTime.Sub(startTime).Milliseconds()
|
||||
return &Response{
|
||||
Success: false,
|
||||
Message: "响应解析失败",
|
||||
Error: err.Error(),
|
||||
Timeout: requestTime,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 计算请求耗时
|
||||
endTime := time.Now()
|
||||
requestTime := endTime.Sub(startTime).Milliseconds()
|
||||
|
||||
// 构建Call方法的响应
|
||||
response := &Response{
|
||||
Code: apiResp.Code,
|
||||
Message: apiResp.Message,
|
||||
Success: apiResp.Code == 0,
|
||||
TransactionID: apiResp.TransactionID,
|
||||
Timeout: requestTime,
|
||||
}
|
||||
|
||||
// 如果有加密数据,尝试解密
|
||||
if apiResp.Data != "" {
|
||||
decryptedData, err := c.decrypt(apiResp.Data)
|
||||
if err == nil {
|
||||
var decryptedMap map[string]interface{}
|
||||
if json.Unmarshal([]byte(decryptedData), &decryptedMap) == nil {
|
||||
response.Data = decryptedMap
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 根据响应码返回对应的错误
|
||||
if apiResp.Code != 0 {
|
||||
err := GetErrorByCode(apiResp.Code)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// CallInterface 简化接口调用方法
|
||||
func (c *Client) CallInterface(interfaceName string, params map[string]interface{}, options ...*ApiCallOptions) (*Response, error) {
|
||||
var opts *ApiCallOptions
|
||||
if len(options) > 0 {
|
||||
opts = options[0]
|
||||
}
|
||||
|
||||
req := Request{
|
||||
InterfaceName: interfaceName,
|
||||
Params: params,
|
||||
Timeout: 60000,
|
||||
Options: opts,
|
||||
}
|
||||
|
||||
return c.Call(req)
|
||||
}
|
||||
|
||||
// validateRequest 校验请求参数
|
||||
func (c *Client) validateRequest(req Request) error {
|
||||
if req.InterfaceName == "" {
|
||||
return fmt.Errorf("interfaceName不能为空")
|
||||
}
|
||||
if req.Params == nil {
|
||||
return fmt.Errorf("params不能为空")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// encrypt AES CBC加密
|
||||
func (c *Client) encrypt(plainText string) (string, error) {
|
||||
keyBytes, err := hex.DecodeString(c.key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(keyBytes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 生成随机IV
|
||||
iv := make([]byte, aes.BlockSize)
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 填充数据
|
||||
paddedData := c.pkcs7Pad([]byte(plainText), aes.BlockSize)
|
||||
|
||||
// 加密
|
||||
ciphertext := make([]byte, len(iv)+len(paddedData))
|
||||
copy(ciphertext, iv)
|
||||
|
||||
mode := cipher.NewCBCEncrypter(block, iv)
|
||||
mode.CryptBlocks(ciphertext[len(iv):], paddedData)
|
||||
|
||||
return base64.StdEncoding.EncodeToString(ciphertext), nil
|
||||
}
|
||||
|
||||
// decrypt AES CBC解密
|
||||
func (c *Client) decrypt(encryptedText string) (string, error) {
|
||||
keyBytes, err := hex.DecodeString(c.key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
ciphertext, err := base64.StdEncoding.DecodeString(encryptedText)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(keyBytes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(ciphertext) < aes.BlockSize {
|
||||
return "", fmt.Errorf("密文太短")
|
||||
}
|
||||
|
||||
iv := ciphertext[:aes.BlockSize]
|
||||
ciphertext = ciphertext[aes.BlockSize:]
|
||||
|
||||
if len(ciphertext)%aes.BlockSize != 0 {
|
||||
return "", fmt.Errorf("密文长度不是块大小的倍数")
|
||||
}
|
||||
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
mode.CryptBlocks(ciphertext, ciphertext)
|
||||
|
||||
// 去除填充
|
||||
unpaddedData, err := c.pkcs7Unpad(ciphertext)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(unpaddedData), nil
|
||||
}
|
||||
|
||||
// pkcs7Pad PKCS7填充
|
||||
func (c *Client) pkcs7Pad(data []byte, blockSize int) []byte {
|
||||
padding := blockSize - len(data)%blockSize
|
||||
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||
return append(data, padtext...)
|
||||
}
|
||||
|
||||
// pkcs7Unpad PKCS7去除填充
|
||||
func (c *Client) pkcs7Unpad(data []byte) ([]byte, error) {
|
||||
length := len(data)
|
||||
if length == 0 {
|
||||
return nil, fmt.Errorf("数据为空")
|
||||
}
|
||||
unpadding := int(data[length-1])
|
||||
if unpadding > length {
|
||||
return nil, fmt.Errorf("无效的填充")
|
||||
}
|
||||
return data[:length-unpadding], nil
|
||||
}
|
||||
|
||||
// GetErrorByCode 根据错误码获取错误
|
||||
func GetErrorByCode(code int) error {
|
||||
// 对于有多个错误对应同一错误码的情况,返回第一个
|
||||
switch code {
|
||||
case 1000:
|
||||
return ErrQueryEmpty
|
||||
case 1001:
|
||||
return ErrSystem
|
||||
case 1002:
|
||||
return ErrDecryptFail
|
||||
case 1003:
|
||||
return ErrRequestParam
|
||||
case 1004:
|
||||
return ErrInvalidIP
|
||||
case 1005:
|
||||
return ErrMissingAccessId
|
||||
case 1006:
|
||||
return ErrInvalidAccessId
|
||||
case 1007:
|
||||
return ErrFrozenAccount
|
||||
case 1008:
|
||||
return ErrProductNotFound
|
||||
case 2001:
|
||||
return ErrBusiness
|
||||
default:
|
||||
return fmt.Errorf("未知错误码: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
// GetCodeByError 根据错误获取错误码
|
||||
func GetCodeByError(err error) int {
|
||||
if code, exists := ErrorCodeMap[err]; exists {
|
||||
return code
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"time"
|
||||
"tyc-server/app/main/api/internal/config"
|
||||
"tyc-server/app/main/api/internal/middleware"
|
||||
"tyc-server/app/main/api/internal/service"
|
||||
tianyuanapi "tyc-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"tyc-server/app/main/model"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
@@ -26,6 +28,7 @@ type ServiceContext struct {
|
||||
WestDexService *service.WestDexService
|
||||
YushanService *service.YushanService
|
||||
TianjuService *service.TianjuService
|
||||
TianyuanapiService *tianyuanapi.Client
|
||||
ApiRequestService *service.ApiRequestService
|
||||
AsynqServer *asynq.Server
|
||||
AsynqService *service.AsynqService
|
||||
@@ -139,6 +142,15 @@ func initBusinessServices(c config.Config, models *BaseModels, adminModels *Admi
|
||||
westDexService := service.NewWestDexService(c)
|
||||
yushanService := service.NewYushanService(c)
|
||||
tianjuService := service.NewTianjuService(c)
|
||||
tianyuanapiService, err := tianyuanapi.NewClient(tianyuanapi.Config{
|
||||
AccessID: c.Tianyuanapi.AccessID,
|
||||
Key: c.Tianyuanapi.Key,
|
||||
BaseURL: c.Tianyuanapi.BaseURL,
|
||||
Timeout: time.Duration(c.Tianyuanapi.Timeout) * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("初始化天远API失败: %+v", err)
|
||||
}
|
||||
asynqService := service.NewAsynqService(c)
|
||||
|
||||
// 初始化API请求服务
|
||||
@@ -147,6 +159,7 @@ func initBusinessServices(c config.Config, models *BaseModels, adminModels *Admi
|
||||
westDexService,
|
||||
yushanService,
|
||||
tianjuService,
|
||||
tianyuanapiService,
|
||||
models.FeatureModel,
|
||||
models.ProductFeatureModel,
|
||||
)
|
||||
@@ -163,6 +176,7 @@ func initBusinessServices(c config.Config, models *BaseModels, adminModels *Admi
|
||||
WestDexService: westDexService,
|
||||
YushanService: yushanService,
|
||||
TianjuService: tianjuService,
|
||||
TianyuanapiService: tianyuanapiService,
|
||||
ApiRequestService: apiRequestService,
|
||||
VerificationService: service.NewVerificationService(c, westDexService, apiRequestService),
|
||||
DictService: service.NewDictService(adminModels.AdminDictTypeModel, adminModels.AdminDictDataModel),
|
||||
@@ -198,6 +212,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
WestDexService: businessServices.WestDexService,
|
||||
YushanService: businessServices.YushanService,
|
||||
TianjuService: businessServices.TianjuService,
|
||||
TianyuanapiService: businessServices.TianyuanapiService,
|
||||
ApiRequestService: businessServices.ApiRequestService,
|
||||
AsynqService: businessServices.AsynqService,
|
||||
VerificationService: businessServices.VerificationService,
|
||||
@@ -277,6 +292,7 @@ type BusinessServices struct {
|
||||
WestDexService *service.WestDexService
|
||||
YushanService *service.YushanService
|
||||
TianjuService *service.TianjuService
|
||||
TianyuanapiService *tianyuanapi.Client
|
||||
ApiRequestService *service.ApiRequestService
|
||||
AsynqService *service.AsynqService
|
||||
VerificationService *service.VerificationService
|
||||
|
||||
Reference in New Issue
Block a user