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

52 lines
998 B

  1. package comm
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "sort"
  6. "strings"
  7. )
  8. func Ksort(params map[string]interface{}) []string {
  9. keys := make([]string, len(params))
  10. i := 0
  11. for k, _ := range params {
  12. keys[i] = k
  13. i++
  14. }
  15. sort.Strings(keys)
  16. return keys
  17. }
  18. func KsortToStr(params map[string]string) []string {
  19. keys := make([]string, len(params))
  20. i := 0
  21. for k, _ := range params {
  22. keys[i] = k
  23. i++
  24. }
  25. sort.Strings(keys)
  26. return keys
  27. }
  28. // Marshal 序列化参数
  29. func GetSortJson(o interface{}) string {
  30. // 序列化一次
  31. raw, _ := json.Marshal(o)
  32. // 反序列化为map
  33. m := make(map[string]interface{})
  34. reader := bytes.NewReader(raw)
  35. decode := json.NewDecoder(reader)
  36. decode.UseNumber()
  37. _ = decode.Decode(&m)
  38. // 重新做一次序列化,并禁用Html Escape
  39. buffer := bytes.NewBufferString("")
  40. encoder := json.NewEncoder(buffer)
  41. encoder.SetEscapeHTML(false)
  42. _ = encoder.Encode(m)
  43. marshal := strings.TrimSpace(buffer.String()) // Trim掉末尾的换行符
  44. return marshal
  45. }