response_param.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package utils
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "gd_gateway/apis"
  8. derrors "gd_gateway/errors"
  9. "reflect"
  10. "strconv"
  11. "strings"
  12. "gd_gateway/common.in/config"
  13. "gd_gateway/common.in/id"
  14. "gd_gateway/common.in/jsonrpc2"
  15. )
  16. func clearParam(param interface{}, name string) {
  17. v := reflect.ValueOf(param)
  18. if v.Kind() == reflect.Ptr {
  19. v = v.Elem()
  20. }
  21. if v.Kind() != reflect.Struct {
  22. return
  23. }
  24. numField := v.NumField()
  25. for i := 0; i < numField; i++ {
  26. field := v.Type().Field(i)
  27. val := v.Field(i)
  28. jsonTag := field.Tag.Get("json")
  29. if jsonTag != name {
  30. continue
  31. }
  32. val.Set(reflect.Zero(val.Type()))
  33. }
  34. }
  35. func encrypt(src string, secret string) (string, error) {
  36. bytes, err := config.AesEncrypt(src, secret)
  37. if err != nil {
  38. return "", err
  39. }
  40. ret := hex.EncodeToString(bytes)
  41. return ret, nil
  42. }
  43. func GetRequestParam(checkApiResult apis.CheckMerchantApiResult, param interface{}) error {
  44. v := reflect.ValueOf(param)
  45. if v.Kind() != reflect.Ptr {
  46. return errors.New("参数错误")
  47. }
  48. for _, v := range checkApiResult.RequstParamConf {
  49. name := v.Name
  50. if v.Selected == false {
  51. clearParam(param, name)
  52. }
  53. }
  54. return nil
  55. }
  56. func FetchSelectedRequestParams(allParams []apis.ManagementBaseApiParam, inputParams []byte) ([]byte, error) {
  57. var m = map[string]interface{}{}
  58. json.Unmarshal(inputParams, &m)
  59. for _, v := range allParams {
  60. if v.In == "" {
  61. if v.Selected == false {
  62. delete(m, v.Name)
  63. }
  64. } else {
  65. if v.Selected == false {
  66. delete(m, v.In)
  67. delete(m, v.Name)
  68. } else {
  69. if _, ok := m[v.In]; ok {
  70. m[v.Name] = m[v.In]
  71. delete(m, v.In)
  72. }
  73. }
  74. }
  75. }
  76. bytes, err := json.Marshal(m)
  77. return bytes, err
  78. }
  79. func GetRequestParamNew(checkApiResult apis.CheckMerchantApiResult, param []byte) ([]byte, error) {
  80. return checkApiResult.DecryptParam, nil
  81. var m = map[string]interface{}{}
  82. var oldMap = map[string]interface{}{}
  83. json.Unmarshal(param, &oldMap)
  84. for k, _ := range oldMap {
  85. newK := strings.TrimSpace(k)
  86. if k != newK {
  87. m[newK] = oldMap[k]
  88. } else {
  89. m[k] = oldMap[k]
  90. }
  91. }
  92. for _, v := range checkApiResult.RequstParamConf {
  93. //name := v.Name
  94. if v.In == "" {
  95. if v.Selected == false {
  96. delete(m, v.Name)
  97. }
  98. } else {
  99. if v.Selected == false {
  100. delete(m, v.In)
  101. delete(m, v.Name)
  102. } else {
  103. if _, ok := m[v.In]; ok {
  104. m[v.Name] = m[v.In]
  105. delete(m, v.In)
  106. }
  107. }
  108. }
  109. }
  110. if v, ok := oldMap["origin_plate_type"]; ok {
  111. m["origin_plate_type"] = v
  112. }
  113. bytes, err := json.Marshal(m)
  114. return bytes, err
  115. }
  116. func getListResponseParam(checkResult []apis.ManagementBaseApiParam, result interface{}) (ret interface{}, err error) {
  117. rtype := fmt.Sprintf("%v", reflect.TypeOf(result))
  118. if rtype == "<nil>" {
  119. return nil, nil
  120. }
  121. resultParamList := result.([]interface{})
  122. retList := make([]interface{}, 0)
  123. for _, resultParam := range resultParamList {
  124. resultParamMap, _ := resultParam.(map[string]interface{})
  125. retMap := make(map[string]interface{}, 0)
  126. for _, v := range checkResult {
  127. if v.Selected == false { // 没有勾选不返回该字段
  128. continue
  129. } else {
  130. if _, ok := resultParamMap[v.Name]; ok {
  131. if v.Required == true { // 必选项必须不为空,为空返回查无
  132. val := reflect.ValueOf(resultParamMap[v.Name])
  133. if val.Kind() != reflect.Bool && reflect.DeepEqual(resultParamMap[v.Name], reflect.Zero(val.Type()).Interface()) {
  134. return nil, derrors.NoRecordMustSelect
  135. }
  136. }
  137. if v.Type == "struct" {
  138. resultParamMap[v.Name], err = getStructResponseParam(v.Child, resultParamMap[v.Name])
  139. if err != nil {
  140. return nil, err
  141. }
  142. } else if v.Type == "list-struct" {
  143. resultParamMap[v.Name], err = getListResponseParam(v.Child, resultParamMap[v.Name])
  144. if err != nil {
  145. return nil, err
  146. }
  147. }
  148. if v.In != "" {
  149. retMap[v.In] = resultParamMap[v.Name]
  150. } else {
  151. retMap[v.Name] = resultParamMap[v.Name]
  152. }
  153. }else{
  154. if v.In != "" {
  155. retMap[v.In] = ""
  156. } else {
  157. retMap[v.Name] = ""
  158. }
  159. }
  160. }
  161. }
  162. retList = append(retList, retMap)
  163. }
  164. return retList, nil
  165. }
  166. func getStructResponseParam(checkResult []apis.ManagementBaseApiParam, result interface{}) (ret interface{}, err error) {
  167. resultParamMap := result.(map[string]interface{})
  168. retMap := make(map[string]interface{}, 0)
  169. for _, v := range checkResult {
  170. if v.Selected == false { // 没有勾选不返回该字段
  171. continue
  172. } else {
  173. if _, ok := resultParamMap[v.Name]; ok {
  174. if v.Required == true { // 必选项必须不为空,为空返回查无
  175. val := reflect.ValueOf(resultParamMap[v.Name])
  176. if val.Kind() != reflect.Bool && reflect.DeepEqual(resultParamMap[v.Name], reflect.Zero(val.Type()).Interface()) {
  177. return nil, derrors.NoRecordMustSelect
  178. }
  179. }
  180. if v.Type == "struct" {
  181. resultParamMap[v.Name], err = getStructResponseParam(v.Child, resultParamMap[v.Name])
  182. if err != nil {
  183. return nil, err
  184. }
  185. } else if v.Type == "list-struct" {
  186. resultParamMap[v.Name], err = getListResponseParam(v.Child, resultParamMap[v.Name])
  187. if err != nil {
  188. return nil, err
  189. }
  190. }
  191. if v.In != "" {
  192. retMap[v.In] = resultParamMap[v.Name]
  193. } else {
  194. retMap[v.Name] = resultParamMap[v.Name]
  195. }
  196. }else{
  197. if v.In != "" {
  198. retMap[v.In] = ""
  199. } else {
  200. retMap[v.Name] = ""
  201. }
  202. }
  203. }
  204. }
  205. return retMap, nil
  206. }
  207. func GetRawResponseParam(checkApiResult apis.CheckMerchantApiResult, param interface{}) (ret string, err error) {
  208. defer func() {
  209. if r := recover(); r != nil {
  210. err = jsonrpc2.NewJsonError(1000, "系统错误:"+fmt.Sprintf("%+v", r))
  211. }
  212. }()
  213. v := reflect.ValueOf(param)
  214. if v.Kind() != reflect.Ptr {
  215. return "", errors.New("param must be ptr")
  216. }
  217. bytes, err := json.Marshal(param)
  218. if err != nil {
  219. return "", err
  220. }
  221. return string(bytes),nil
  222. var m = map[string]interface{}{}
  223. json.Unmarshal(bytes, &m)
  224. rawRetMap := make(map[string]interface{}, 0)
  225. for _, v := range checkApiResult.ResponseParamConf {
  226. if v.In != "" {
  227. rawRetMap[v.In] = m[v.Name]
  228. } else {
  229. rawRetMap[v.Name] = m[v.Name]
  230. }
  231. }
  232. bytes, err = json.Marshal(rawRetMap)
  233. rawRetMap = nil
  234. m = nil
  235. if err != nil {
  236. return "", err
  237. }
  238. return string(bytes), nil
  239. }
  240. func GetResponseParam(checkApiResult apis.CheckMerchantApiResult, param interface{},encryptType string) (ret string, err error) {
  241. defer func() {
  242. if r := recover(); r != nil {
  243. err = jsonrpc2.NewJsonError(1000, "系统错误:"+fmt.Sprintf("%+v", r))
  244. }
  245. }()
  246. v := reflect.ValueOf(param)
  247. if v.Kind() != reflect.Ptr {
  248. return "", errors.New("param must be ptr")
  249. }
  250. bytes, err := json.Marshal(param)
  251. if err != nil {
  252. return "", err
  253. }
  254. var m = map[string]interface{}{}
  255. json.Unmarshal(bytes, &m)
  256. retMap := make(map[string]interface{}, 0)
  257. for _, v := range checkApiResult.ResponseParamConf {
  258. if v.Selected == false { // 没有勾选不返回该字段
  259. continue
  260. } else {
  261. if _, ok := m[v.Name]; ok {
  262. if v.Required == true && v.Name != "approved_number" && v.Name != "approved_load" { // 必选项必须不为空,为空返回查无
  263. if strings.Contains(v.Type, "list") {
  264. if m[v.Name] == nil {
  265. return "", derrors.NoRecordMustSelect
  266. }
  267. rli := m[v.Name].([]interface{})
  268. if len(rli) == 0 {
  269. return "", derrors.NoRecordMustSelect
  270. }
  271. } else {
  272. val := reflect.ValueOf(m[v.Name])
  273. if val.Kind() != reflect.Bool && reflect.DeepEqual(m[v.Name], reflect.Zero(val.Type()).Interface()) {
  274. return "", derrors.NoRecordMustSelect
  275. }
  276. if v.Name == "vin" {
  277. if len(m[v.Name].(string)) < 17 {
  278. return "", derrors.NoRecordMustSelect
  279. }
  280. }
  281. }
  282. }
  283. if v.Type == "struct" && len(v.Child) > 0 {
  284. m[v.Name], err = getStructResponseParam(v.Child, m[v.Name])
  285. if err != nil {
  286. return "", err
  287. }
  288. } else if v.Type == "list-struct" && len(v.Child) > 0 {
  289. m[v.Name], err = getListResponseParam(v.Child, m[v.Name])
  290. if err != nil {
  291. return "", err
  292. }
  293. }
  294. if v.In != "" {
  295. retMap[v.In] = m[v.Name]
  296. } else {
  297. retMap[v.Name] = m[v.Name]
  298. }
  299. }else{
  300. key := v.Name
  301. if v.In != "" {
  302. key = v.In
  303. //retMap[v.In] = ""
  304. }
  305. if v.Type == "int"{
  306. retMap[key] = 0
  307. }else if v.Type == "float"{
  308. retMap[key] = 0
  309. }else if v.Type == "bool"{
  310. retMap[key] = false
  311. }else if v.Type == "string"{
  312. retMap[key] = ""
  313. }else if v.Type == "struct"{
  314. retMap[key] = make(map[string]interface{})
  315. }else if strings.Contains(v.Type,"list"){
  316. retMap[key] = make([]interface{},0)
  317. }
  318. }
  319. }
  320. }
  321. bytes, err = json.Marshal(retMap)
  322. retMap = nil
  323. m = nil
  324. if err != nil {
  325. return "", err
  326. }
  327. if encryptType == "" {
  328. return string(bytes), nil
  329. }else if encryptType == "MD5"{
  330. return string(bytes), nil
  331. }else if encryptType == "AES"{
  332. ret, err = encrypt(string(bytes), checkApiResult.AppSecret)
  333. if err != nil {
  334. return "", err
  335. }
  336. }
  337. return ret, nil
  338. }
  339. /*
  340. type Test2 struct {
  341. P1 string `json:"p1"`
  342. P2 string `json:"p2"`
  343. }
  344. type Test1 struct {
  345. Name string `json:"name"`
  346. TestValue bool `json:"test_value"`
  347. TestValue1 byte `json:"test_value1"`
  348. XXX Test2 `json:"xxx"`
  349. }
  350. func main() {
  351. t := Test1{}
  352. t.Name = "test"
  353. t.XXX.P1 = "XX"
  354. t.XXX.P2 = "yyy"
  355. fmt.Printf("!!!:%v\n", t)
  356. clearParam(&t, "")
  357. fmt.Printf("!!!:%v\n", t)
  358. }
  359. */
  360. func GetResponseOrderNo(responseParamConf []apis.ManagementBaseApiParam) string {
  361. for _, v := range responseParamConf {
  362. if v.Name == "order_no" {
  363. if v.Selected {
  364. orderNo, _ := id.GetDistributedUniqueID()
  365. return strconv.FormatUint(orderNo, 10)
  366. } else {
  367. return ""
  368. }
  369. }
  370. }
  371. return ""
  372. }