fux comb298y
This commit is contained in:
parent
c8da895533
commit
6bb7f33864
@ -1,19 +1,41 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// StructToMap 将结构体转换为 map[string]interface{}
|
||||
func StructToMap(data interface{}) (map[string]interface{}, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
result := make(map[string]interface{})
|
||||
|
||||
// 使用反射获取结构体的值
|
||||
v := reflect.ValueOf(data)
|
||||
if v.Kind() == reflect.Ptr {
|
||||
v = v.Elem()
|
||||
}
|
||||
if v.Kind() != reflect.Struct {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal(jsonData, &result); err != nil {
|
||||
return nil, err
|
||||
// 遍历结构体字段
|
||||
t := v.Type()
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
value := v.Field(i)
|
||||
|
||||
// 获取字段名(优先使用 json tag)
|
||||
key := field.Name
|
||||
if jsonTag := field.Tag.Get("json"); jsonTag != "" && jsonTag != "-" {
|
||||
key = jsonTag
|
||||
}
|
||||
|
||||
// 处理字段值
|
||||
if value.IsValid() && !value.IsZero() {
|
||||
if value.Kind() == reflect.Ptr {
|
||||
value = value.Elem()
|
||||
}
|
||||
result[key] = value.Interface()
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
@ -121,7 +121,7 @@ func (l *COMB298YLogic) COMB298Y(req *types.Request) (resp string, err *errs.App
|
||||
// {SourceId: "G09XM02", ServiceId: "IVYZ5733", Mapping: westmodel.IVYZ5733FieldMapping, Wrap: "data", Service: "west"},
|
||||
// {SourceId: "G11BJ06", ServiceId: "IVYZ9A2B", Mapping: westmodel.IVYZ9A2BFieldMapping, Wrap: "data", Service: "west"},
|
||||
{SourceId: "G05HZ01", ServiceId: "QYGLB4C0", Mapping: westmodel.QYGLB4C0FieldMapping, Wrap: "", Service: "west"},
|
||||
{SourceId: "RIS031", ServiceId: "FLXG8UI0", Mapping: westmodel.FLXG8UI0FieldMapping, Wrap: "data", Service: "yushan"},
|
||||
{SourceId: "RIS031", ServiceId: "FLXG8UI0", Mapping: westmodel.FLXG8UI0FieldMapping, Wrap: "", Service: "yushan"},
|
||||
}
|
||||
|
||||
// 为每个请求构建对应的请求参数
|
||||
@ -136,7 +136,10 @@ func (l *COMB298YLogic) COMB298Y(req *types.Request) (resp string, err *errs.App
|
||||
logx.Errorf("结构体转map失败:%v", err)
|
||||
return "", errs.ErrSystem
|
||||
}
|
||||
logx.Infof("G05HZ01 原始数据: %+v", data)
|
||||
logx.Infof("G05HZ01 转换后数据: %+v", dataMap)
|
||||
apiRequests[i].Request = common.MapStructToAPIRequest(dataMap, apiRequests[i].Mapping, apiRequests[i].Wrap)
|
||||
logx.Infof("G05HZ01 最终请求数据: %+v", apiRequests[i].Request)
|
||||
} else {
|
||||
// 其他西部服务需要加密
|
||||
encryptedRequest, encryptErr := common.EncryptStructFields(data, westConfig.Key)
|
||||
@ -150,10 +153,8 @@ func (l *COMB298YLogic) COMB298Y(req *types.Request) (resp string, err *errs.App
|
||||
// 如果是 RIS031,添加 type: 3
|
||||
if apiRequests[i].SourceId == "RIS031" {
|
||||
apiRequests[i].Request = map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"keyWord": data.IDCard,
|
||||
"type": 3,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user