main.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package main
  2. import (
  3. "fmt"
  4. "mtp2_if/utils"
  5. "reflect"
  6. "sort"
  7. "github.com/fatih/structs"
  8. )
  9. type aPIAAA struct {
  10. Account string `json:"account" binding:"required"` // 用户唯一识别码(请转入UserID)
  11. SerialNo string `json:"serialNo"` // 实名认证流水号
  12. Name *string `json:"name" structs:",omitempty"` // 用户姓名
  13. IdCard string `json:"idCard" structs:",omitempty"` // 个人身份证、台胞证、港澳通行证等证件号
  14. IdCardType *int `json:"idCardType" structs:",omitempty"` // 证件类型 1:居民身份证 2:台湾居民来往内地通行证 3:港澳居民往来内地通行证 10:武装警察身份证 11:军人身份证 15:警察(警官)证 21:外国人永久居留证 23:护照
  15. Mobile string `json:"mobile" structs:",omitempty"` // 手机号码
  16. SignPwd string `json:"signPwd" structs:",omitempty"` // 签约密码(MTP2登录密码加密方式),如果为空将随机生成签约密码(当签约方式为“签约密码签约”时会使用到,可通过重置接口修改)
  17. IsSignPwdNotice *float64 `json:"isSignPwdNotice" structs:",omitempty"` // 是否将签约密码以短信形式通知用户 0:不通知(默认) 1:通知
  18. IsNotice *bool `json:"isNotice" structs:",omitempty"` // 用户发起合同或需要签署时是否进行短信通知 0:否(默认) 1:是
  19. }
  20. func main() {
  21. req := &aPIAAA{
  22. Account: "1111",
  23. }
  24. req.SerialNo = "2222"
  25. req.Name = utils.SetPointValue("")
  26. req.IdCardType = utils.SetPointValue(1)
  27. req.IsSignPwdNotice = utils.SetPointValue(5.01)
  28. reqMap := structs.Map(req)
  29. aaa := sortMapByKey(reqMap)
  30. fmt.Println(aaa)
  31. }
  32. func sortMapByKey(data map[string]interface{}) (sortedData string) {
  33. keys := make([]string, 0, len(data))
  34. for k := range data {
  35. keys = append(keys, k)
  36. }
  37. sort.Strings(keys)
  38. for i, k := range keys {
  39. if i > 0 {
  40. sortedData += ","
  41. }
  42. // 判断是否指针
  43. v := reflect.ValueOf(data[k])
  44. if v.Kind() == reflect.Ptr {
  45. switch data[k].(type) {
  46. case *string:
  47. sortedData += fmt.Sprintf(`"%s":"%s"`, k, *(data[k].(*string)))
  48. case *map[string]interface{}:
  49. sortedData += fmt.Sprintf(`"%s":%s`, k, sortMapByKey(*(data[k].(*map[string]interface{}))))
  50. case *[]interface{}:
  51. list := data[k].([]interface{})
  52. sortedData += fmt.Sprintf(`"%s":[`, k)
  53. for j, item := range list {
  54. if j > 0 {
  55. sortedData += ","
  56. }
  57. sortedData += sortMapByKey(item.(map[string]interface{}))
  58. }
  59. sortedData += "]"
  60. default:
  61. sortedData += fmt.Sprintf(`"%s":%v`, k, reflect.ValueOf(data[k]).Elem())
  62. }
  63. } else {
  64. switch data[k].(type) {
  65. case string:
  66. sortedData += fmt.Sprintf(`"%s":"%s"`, k, data[k].(string))
  67. case map[string]interface{}:
  68. sortedData += fmt.Sprintf(`"%s":%s`, k, sortMapByKey(data[k].(map[string]interface{})))
  69. case []interface{}:
  70. list := data[k].([]interface{})
  71. sortedData += fmt.Sprintf(`"%s":[`, k)
  72. for j, item := range list {
  73. if j > 0 {
  74. sortedData += ","
  75. }
  76. sortedData += sortMapByKey(item.(map[string]interface{}))
  77. }
  78. sortedData += "]"
  79. default:
  80. sortedData += fmt.Sprintf(`"%s":%v`, k, data[k])
  81. }
  82. }
  83. }
  84. return fmt.Sprintf("{%s}", sortedData)
  85. }