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

2 years ago
1 year ago
2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. func KsortToStrInterface(params map[string]interface{}) []string {
  29. keys := make([]string, len(params))
  30. i := 0
  31. for k, _ := range params {
  32. keys[i] = k
  33. i++
  34. }
  35. sort.Strings(keys)
  36. return keys
  37. }
  38. // Marshal 序列化参数
  39. func GetSortJson(o interface{}) string {
  40. // 序列化一次
  41. raw, _ := json.Marshal(o)
  42. // 反序列化为map
  43. m := make(map[string]interface{})
  44. reader := bytes.NewReader(raw)
  45. decode := json.NewDecoder(reader)
  46. decode.UseNumber()
  47. _ = decode.Decode(&m)
  48. // 重新做一次序列化,并禁用Html Escape
  49. buffer := bytes.NewBufferString("")
  50. encoder := json.NewEncoder(buffer)
  51. encoder.SetEscapeHTML(false)
  52. _ = encoder.Encode(m)
  53. marshal := strings.TrimSpace(buffer.String()) // Trim掉末尾的换行符
  54. return marshal
  55. }