第三方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.

api.go 1.4 KiB

2 years ago
5 months ago
2 years ago
1 year ago
2 years ago
5 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package comm
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "encoding/json"
  8. "sort"
  9. "strings"
  10. )
  11. func Ksort(params map[string]interface{}) []string {
  12. keys := make([]string, len(params))
  13. i := 0
  14. for k, _ := range params {
  15. keys[i] = k
  16. i++
  17. }
  18. sort.Strings(keys)
  19. return keys
  20. }
  21. func KsortToStr(params map[string]string) []string {
  22. keys := make([]string, len(params))
  23. i := 0
  24. for k, _ := range params {
  25. keys[i] = k
  26. i++
  27. }
  28. sort.Strings(keys)
  29. return keys
  30. }
  31. func KsortToStrInterface(params map[string]interface{}) []string {
  32. keys := make([]string, len(params))
  33. i := 0
  34. for k, _ := range params {
  35. keys[i] = k
  36. i++
  37. }
  38. sort.Strings(keys)
  39. return keys
  40. }
  41. // Marshal 序列化参数
  42. func GetSortJson(o interface{}) string {
  43. // 序列化一次
  44. raw, _ := json.Marshal(o)
  45. // 反序列化为map
  46. m := make(map[string]interface{})
  47. reader := bytes.NewReader(raw)
  48. decode := json.NewDecoder(reader)
  49. decode.UseNumber()
  50. _ = decode.Decode(&m)
  51. // 重新做一次序列化,并禁用Html Escape
  52. buffer := bytes.NewBufferString("")
  53. encoder := json.NewEncoder(buffer)
  54. encoder.SetEscapeHTML(false)
  55. _ = encoder.Encode(m)
  56. marshal := strings.TrimSpace(buffer.String()) // Trim掉末尾的换行符
  57. return marshal
  58. }
  59. func HmacMd5(key, data string) string {
  60. h := hmac.New(md5.New, []byte(key))
  61. h.Write([]byte(data))
  62. return strings.ToUpper(hex.EncodeToString(h.Sum([]byte(""))))
  63. }