第三方api接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.go 2.4 KiB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package zhios_third_party_utils
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "sort"
  9. "strings"
  10. "time"
  11. )
  12. type LocalTime time.Time
  13. const (
  14. TimeFormat = "2006-01-02 15:04:05"
  15. )
  16. func (t *LocalTime) UnmarshalJSON(data []byte) (err error) {
  17. // 空值不进行解析
  18. if len(data) == 2 {
  19. *t = LocalTime(time.Time{})
  20. return
  21. }
  22. now, err := time.ParseInLocation(`"`+TimeFormat+`"`, string(data), time.Local)
  23. *t = LocalTime(now)
  24. return
  25. }
  26. func (t LocalTime) MarshalJSON() ([]byte, error) {
  27. b := make([]byte, 0, len(TimeFormat)+2)
  28. b = append(b, '"')
  29. b = time.Time(t).AppendFormat(b, TimeFormat)
  30. b = append(b, '"')
  31. return b, nil
  32. }
  33. func (t LocalTime) String() string {
  34. return time.Time(t).Format(TimeFormat)
  35. }
  36. func UnmarshalJSON(data []byte, v interface{}) (err error) {
  37. return json.Unmarshal(data, v)
  38. }
  39. func ConvertStructList(data interface{}) string {
  40. if data == nil {
  41. return "[]"
  42. }
  43. jsonStr, _ := json.Marshal(data)
  44. return string(jsonStr)
  45. }
  46. func ConvertStruct(data interface{}) string {
  47. if data == nil {
  48. return "{}"
  49. }
  50. jsonStr, _ := json.Marshal(data)
  51. return string(jsonStr)
  52. }
  53. func ConvertBasicList(data interface{}) string {
  54. if data == nil {
  55. return "[]"
  56. }
  57. return strings.Replace(strings.Trim(fmt.Sprint(data), "[]"), " ", ",", -1)
  58. }
  59. func HandleJsonResponse(jsonStr string, v interface{}) (err error) {
  60. if strings.Contains(jsonStr[0:20], "error_response") {
  61. err := &TopApiRequestError{}
  62. jsonStr = jsonStr[18 : len(jsonStr)-1]
  63. err2 := json.Unmarshal([]byte(jsonStr), err)
  64. if err2 != nil {
  65. return err2
  66. }
  67. return err
  68. }
  69. return json.Unmarshal([]byte(jsonStr), v)
  70. }
  71. func GetSign(publicParam map[string]interface{}, data map[string]interface{}, secret string) string {
  72. var allParamMap = make(map[string]interface{})
  73. for k, v := range data {
  74. allParamMap[k] = v
  75. }
  76. for k, v := range publicParam {
  77. allParamMap[k] = v
  78. }
  79. var keyList []string
  80. for k := range allParamMap {
  81. keyList = append(keyList, k)
  82. }
  83. sort.Strings(keyList)
  84. var signStr = ""
  85. for _, key := range keyList {
  86. value := allParamMap[key]
  87. signStr = signStr + fmt.Sprintf("%v%v", key, value)
  88. //if(value != ""){
  89. // signStr = signStr + fmt.Sprintf("%v%v", key, value)
  90. //}
  91. }
  92. fmt.Println(signStr)
  93. sign := strings.ToUpper(HmacSha256(signStr, secret))
  94. return sign
  95. }
  96. func HmacSha256(data string, secret string) string {
  97. h := hmac.New(sha256.New, []byte(secret))
  98. h.Write([]byte(data))
  99. return hex.EncodeToString(h.Sum(nil))
  100. }