bihuHttpClient.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package thirdparty
  2. import (
  3. "gd_management/common.in/config"
  4. "gd_management/common.in/httpClient"
  5. "gd_management/common.in/utils"
  6. "fmt"
  7. hurl "net/url"
  8. "sort"
  9. )
  10. func HttpBiHuGet(api string, query map[string]string) ([]byte, error) {
  11. agent := config.Conf.ThirdPart.BiHuAgent
  12. secret := config.Conf.ThirdPart.BiHuSecret
  13. names := []string{}
  14. query["Agent"] = agent
  15. for k, _ := range query {
  16. names = append(names, k)
  17. }
  18. sort.Strings(names)
  19. value := ""
  20. signText := ""
  21. for _, v := range names {
  22. value = fmt.Sprintf("%v", query[v])
  23. if signText == "" {
  24. signText = fmt.Sprintf("%s=%s", v, value)
  25. continue
  26. }
  27. signText = fmt.Sprintf("%s&%s=%s", signText, v, value)
  28. }
  29. urlValue := hurl.Values{}
  30. for k, v := range query {
  31. urlValue.Add(k, v)
  32. }
  33. querystring := urlValue.Encode()
  34. signText = fmt.Sprintf("%s%s", signText, secret)
  35. sign := utils.MD5(signText)
  36. query["SecCode"] = sign
  37. querystring = fmt.Sprintf("%s&%s=%s", querystring, "SecCode", sign)
  38. url := fmt.Sprintf("%s?%s", api, querystring)
  39. resp, err := httpClient.HttpGetWithHead(url, nil, nil)
  40. return resp, err
  41. }
  42. /*
  43. func HttpBiHuPost(host string, router string, body map[string]interface{}) ([]byte, error) {
  44. names := []string{}
  45. for k, _ := range body {
  46. names = append(names, k)
  47. }
  48. sort.Strings(names)
  49. value := ""
  50. signText := ""
  51. for _, v := range names {
  52. if reflect.TypeOf(body[v]).Kind() == reflect.Struct ||
  53. reflect.TypeOf(body[v]).Kind() == reflect.Slice {
  54. bytes, _ := json.Marshal(body[v])
  55. value = string(bytes)
  56. } else {
  57. value = fmt.Sprintf("%v", body[v])
  58. }
  59. if signText == "" {
  60. signText = fmt.Sprintf("%s=%s", v, value)
  61. continue
  62. }
  63. signText = fmt.Sprintf("%s&%s=%s", signText, v, value)
  64. }
  65. signText = fmt.Sprintf("%s%s", signText, config.Conf.ThirdPart.LeCheSecret)
  66. sign := utils.MD5(signText)
  67. body["sign"] = strings.ToUpper(sign)
  68. bytes, _ := json.Marshal(body)
  69. resp, err := httpClient.HttpPostWithHead(host+"/"+router, nil, nil, bytes)
  70. return resp, err
  71. }
  72. */