package utils import ( "encoding/hex" "encoding/json" "errors" "fmt" "gd_gateway/apis" derrors "gd_gateway/errors" "reflect" "strconv" "strings" "gd_gateway/common.in/config" "gd_gateway/common.in/id" "gd_gateway/common.in/jsonrpc2" ) func clearParam(param interface{}, name string) { v := reflect.ValueOf(param) if v.Kind() == reflect.Ptr { v = v.Elem() } if v.Kind() != reflect.Struct { return } numField := v.NumField() for i := 0; i < numField; i++ { field := v.Type().Field(i) val := v.Field(i) jsonTag := field.Tag.Get("json") if jsonTag != name { continue } val.Set(reflect.Zero(val.Type())) } } func encrypt(src string, secret string) (string, error) { bytes, err := config.AesEncrypt(src, secret) if err != nil { return "", err } ret := hex.EncodeToString(bytes) return ret, nil } func GetRequestParam(checkApiResult apis.CheckMerchantApiResult, param interface{}) error { v := reflect.ValueOf(param) if v.Kind() != reflect.Ptr { return errors.New("参数错误") } for _, v := range checkApiResult.RequstParamConf { name := v.Name if v.Selected == false { clearParam(param, name) } } return nil } func FetchSelectedRequestParams(allParams []apis.ManagementBaseApiParam, inputParams []byte) ([]byte, error) { var m = map[string]interface{}{} json.Unmarshal(inputParams, &m) for _, v := range allParams { if v.In == "" { if v.Selected == false { delete(m, v.Name) } } else { if v.Selected == false { delete(m, v.In) delete(m, v.Name) } else { if _, ok := m[v.In]; ok { m[v.Name] = m[v.In] delete(m, v.In) } } } } bytes, err := json.Marshal(m) return bytes, err } func GetRequestParamNew(checkApiResult apis.CheckMerchantApiResult, param []byte) ([]byte, error) { return checkApiResult.DecryptParam, nil var m = map[string]interface{}{} var oldMap = map[string]interface{}{} json.Unmarshal(param, &oldMap) for k, _ := range oldMap { newK := strings.TrimSpace(k) if k != newK { m[newK] = oldMap[k] } else { m[k] = oldMap[k] } } for _, v := range checkApiResult.RequstParamConf { //name := v.Name if v.In == "" { if v.Selected == false { delete(m, v.Name) } } else { if v.Selected == false { delete(m, v.In) delete(m, v.Name) } else { if _, ok := m[v.In]; ok { m[v.Name] = m[v.In] delete(m, v.In) } } } } if v, ok := oldMap["origin_plate_type"]; ok { m["origin_plate_type"] = v } bytes, err := json.Marshal(m) return bytes, err } func getListResponseParam(checkResult []apis.ManagementBaseApiParam, result interface{}) (ret interface{}, err error) { rtype := fmt.Sprintf("%v", reflect.TypeOf(result)) if rtype == "" { return nil, nil } resultParamList := result.([]interface{}) retList := make([]interface{}, 0) for _, resultParam := range resultParamList { resultParamMap, _ := resultParam.(map[string]interface{}) retMap := make(map[string]interface{}, 0) for _, v := range checkResult { if v.Selected == false { // 没有勾选不返回该字段 continue } else { if _, ok := resultParamMap[v.Name]; ok { if v.Required == true { // 必选项必须不为空,为空返回查无 val := reflect.ValueOf(resultParamMap[v.Name]) if val.Kind() != reflect.Bool && reflect.DeepEqual(resultParamMap[v.Name], reflect.Zero(val.Type()).Interface()) { return nil, derrors.NoRecordMustSelect } } if v.Type == "struct" { resultParamMap[v.Name], err = getStructResponseParam(v.Child, resultParamMap[v.Name]) if err != nil { return nil, err } } else if v.Type == "list-struct" { resultParamMap[v.Name], err = getListResponseParam(v.Child, resultParamMap[v.Name]) if err != nil { return nil, err } } if v.In != "" { retMap[v.In] = resultParamMap[v.Name] } else { retMap[v.Name] = resultParamMap[v.Name] } }else{ if v.In != "" { retMap[v.In] = "" } else { retMap[v.Name] = "" } } } } retList = append(retList, retMap) } return retList, nil } func getStructResponseParam(checkResult []apis.ManagementBaseApiParam, result interface{}) (ret interface{}, err error) { resultParamMap := result.(map[string]interface{}) retMap := make(map[string]interface{}, 0) for _, v := range checkResult { if v.Selected == false { // 没有勾选不返回该字段 continue } else { if _, ok := resultParamMap[v.Name]; ok { if v.Required == true { // 必选项必须不为空,为空返回查无 val := reflect.ValueOf(resultParamMap[v.Name]) if val.Kind() != reflect.Bool && reflect.DeepEqual(resultParamMap[v.Name], reflect.Zero(val.Type()).Interface()) { return nil, derrors.NoRecordMustSelect } } if v.Type == "struct" { resultParamMap[v.Name], err = getStructResponseParam(v.Child, resultParamMap[v.Name]) if err != nil { return nil, err } } else if v.Type == "list-struct" { resultParamMap[v.Name], err = getListResponseParam(v.Child, resultParamMap[v.Name]) if err != nil { return nil, err } } if v.In != "" { retMap[v.In] = resultParamMap[v.Name] } else { retMap[v.Name] = resultParamMap[v.Name] } }else{ if v.In != "" { retMap[v.In] = "" } else { retMap[v.Name] = "" } } } } return retMap, nil } func GetRawResponseParam(checkApiResult apis.CheckMerchantApiResult, param interface{}) (ret string, err error) { defer func() { if r := recover(); r != nil { err = jsonrpc2.NewJsonError(1000, "系统错误:"+fmt.Sprintf("%+v", r)) } }() v := reflect.ValueOf(param) if v.Kind() != reflect.Ptr { return "", errors.New("param must be ptr") } bytes, err := json.Marshal(param) if err != nil { return "", err } return string(bytes),nil var m = map[string]interface{}{} json.Unmarshal(bytes, &m) rawRetMap := make(map[string]interface{}, 0) for _, v := range checkApiResult.ResponseParamConf { if v.In != "" { rawRetMap[v.In] = m[v.Name] } else { rawRetMap[v.Name] = m[v.Name] } } bytes, err = json.Marshal(rawRetMap) rawRetMap = nil m = nil if err != nil { return "", err } return string(bytes), nil } func GetResponseParam(checkApiResult apis.CheckMerchantApiResult, param interface{},encryptType string) (ret string, err error) { defer func() { if r := recover(); r != nil { err = jsonrpc2.NewJsonError(1000, "系统错误:"+fmt.Sprintf("%+v", r)) } }() v := reflect.ValueOf(param) if v.Kind() != reflect.Ptr { return "", errors.New("param must be ptr") } bytes, err := json.Marshal(param) if err != nil { return "", err } var m = map[string]interface{}{} json.Unmarshal(bytes, &m) retMap := make(map[string]interface{}, 0) for _, v := range checkApiResult.ResponseParamConf { if v.Selected == false { // 没有勾选不返回该字段 continue } else { if _, ok := m[v.Name]; ok { if v.Required == true && v.Name != "approved_number" && v.Name != "approved_load" { // 必选项必须不为空,为空返回查无 if strings.Contains(v.Type, "list") { if m[v.Name] == nil { return "", derrors.NoRecordMustSelect } rli := m[v.Name].([]interface{}) if len(rli) == 0 { return "", derrors.NoRecordMustSelect } } else { val := reflect.ValueOf(m[v.Name]) if val.Kind() != reflect.Bool && reflect.DeepEqual(m[v.Name], reflect.Zero(val.Type()).Interface()) { return "", derrors.NoRecordMustSelect } if v.Name == "vin" { if len(m[v.Name].(string)) < 17 { return "", derrors.NoRecordMustSelect } } } } if v.Type == "struct" && len(v.Child) > 0 { m[v.Name], err = getStructResponseParam(v.Child, m[v.Name]) if err != nil { return "", err } } else if v.Type == "list-struct" && len(v.Child) > 0 { m[v.Name], err = getListResponseParam(v.Child, m[v.Name]) if err != nil { return "", err } } if v.In != "" { retMap[v.In] = m[v.Name] } else { retMap[v.Name] = m[v.Name] } }else{ key := v.Name if v.In != "" { key = v.In //retMap[v.In] = "" } if v.Type == "int"{ retMap[key] = 0 }else if v.Type == "float"{ retMap[key] = 0 }else if v.Type == "bool"{ retMap[key] = false }else if v.Type == "string"{ retMap[key] = "" }else if v.Type == "struct"{ retMap[key] = make(map[string]interface{}) }else if strings.Contains(v.Type,"list"){ retMap[key] = make([]interface{},0) } } } } bytes, err = json.Marshal(retMap) retMap = nil m = nil if err != nil { return "", err } if encryptType == "" { return string(bytes), nil }else if encryptType == "MD5"{ return string(bytes), nil }else if encryptType == "AES"{ ret, err = encrypt(string(bytes), checkApiResult.AppSecret) if err != nil { return "", err } } return ret, nil } /* type Test2 struct { P1 string `json:"p1"` P2 string `json:"p2"` } type Test1 struct { Name string `json:"name"` TestValue bool `json:"test_value"` TestValue1 byte `json:"test_value1"` XXX Test2 `json:"xxx"` } func main() { t := Test1{} t.Name = "test" t.XXX.P1 = "XX" t.XXX.P2 = "yyy" fmt.Printf("!!!:%v\n", t) clearParam(&t, "") fmt.Printf("!!!:%v\n", t) } */ func GetResponseOrderNo(responseParamConf []apis.ManagementBaseApiParam) string { for _, v := range responseParamConf { if v.Name == "order_no" { if v.Selected { orderNo, _ := id.GetDistributedUniqueID() return strconv.FormatUint(orderNo, 10) } else { return "" } } } return "" }