123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- 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 == "<nil>" {
- 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 ""
- }
|