hz_data_api.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package services
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/json"
  6. "errors"
  7. "eta/eta_task/utils"
  8. "fmt"
  9. "github.com/shopspring/decimal"
  10. "io/ioutil"
  11. "net/http"
  12. "reflect"
  13. "sort"
  14. "strings"
  15. "time"
  16. )
  17. func HttpPost(funcName, method string, postDataMap map[string]interface{}) (result string, err error) {
  18. defer func() {
  19. if err != nil {
  20. fmt.Println("Sync hz crawler err:" + err.Error())
  21. utils.FileLog.Info("Sync hz crawler err:" + err.Error())
  22. }
  23. }()
  24. bytesData, err := handlePostData(postDataMap)
  25. if err != nil {
  26. fmt.Println("handlePostData Err:" + err.Error())
  27. return
  28. }
  29. fmt.Println("URL:" + utils.HzDataApi + method)
  30. fmt.Println("Params:" + string(bytesData))
  31. client := &http.Client{}
  32. req, err := http.NewRequest("POST", utils.HzDataApi+method, bytes.NewReader(bytesData))
  33. if err != nil {
  34. fmt.Println("http.NewRequest Err:" + err.Error())
  35. return "", err
  36. }
  37. req.Header.Set("content-type", "application/json")
  38. utils.FileLog.Info(fmt.Sprintf("请求函数:%s ;请求地址:%s;请求体:%s", funcName, utils.HzDataApi+method, string(bytesData)))
  39. resp, err := client.Do(req)
  40. if err != nil {
  41. return "", err
  42. }
  43. if resp.Body != nil {
  44. body, err := ioutil.ReadAll(resp.Body)
  45. if err != nil {
  46. return "", err
  47. }
  48. result = string(body)
  49. fmt.Println("HttpPost Result:" + string(body))
  50. }
  51. utils.FileLog.Info(fmt.Sprintf("请求函数:%s ;返回体:%s", funcName, result))
  52. return
  53. }
  54. // handlePostData 处理post请求数据
  55. func handlePostData(postDataMap map[string]interface{}) (bytesData []byte, err error) {
  56. postDataMap["nonce_str"] = utils.GetRandString(16) // 随机字符串
  57. postDataMap["timestamp"] = time.Now().Unix() //当前格林威治时间,int64类型
  58. postDataMap["appid"] = utils.APPID //当前格林威治时间,int64类型
  59. // 待签名数据
  60. signData := convertParamInterface(postDataMap)
  61. sign, err := getSignData(signData)
  62. if err != nil {
  63. return
  64. }
  65. postDataMap["sign"] = sign //签名
  66. bytesData, err = json.Marshal(postDataMap)
  67. return
  68. }
  69. // 将请求传入的数据格式转换成签名需要的格式(目前只能处理简单的类型,数组、对象暂不支持)
  70. func convertParamInterface(params map[string]interface{}) (signData map[string]string) {
  71. signData = make(map[string]string)
  72. for key := range params {
  73. val := ``
  74. //fmt.Println("key", key, ";val:", params[key], ";type:", reflect.TypeOf(params[key]))
  75. //signData[key] = params[key][0]
  76. tmpVal := params[key]
  77. switch reflect.TypeOf(tmpVal).Kind() {
  78. case reflect.String:
  79. val = fmt.Sprint(tmpVal)
  80. case reflect.Int, reflect.Int16, reflect.Int64, reflect.Int32, reflect.Int8:
  81. val = fmt.Sprint(tmpVal)
  82. case reflect.Uint, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint64:
  83. val = fmt.Sprint(tmpVal)
  84. case reflect.Bool:
  85. val = fmt.Sprint(tmpVal)
  86. case reflect.Float64:
  87. decimalNum := decimal.NewFromFloat(tmpVal.(float64))
  88. val = decimalNum.String()
  89. //val = strconv.FormatFloat(tmpVal.(float64), 'E', -1, 64) //float64
  90. case reflect.Float32:
  91. decimalNum := decimal.NewFromFloat32(tmpVal.(float32))
  92. val = decimalNum.String()
  93. }
  94. signData[key] = val
  95. }
  96. return signData
  97. }
  98. // getSignData 获取参数签名
  99. func getSignData(postData map[string]string) (sign string, err error) {
  100. appid := utils.APPID
  101. if appid == "" {
  102. err = errors.New("参数异常,缺少appid")
  103. return
  104. }
  105. secret := utils.SECRET
  106. if secret == "" {
  107. err = errors.New("参数异常,缺少secret")
  108. return
  109. }
  110. if postData["nonce_str"] == "" {
  111. err = errors.New("参数异常,缺少随机字符串")
  112. return
  113. }
  114. if postData["timestamp"] == "" {
  115. err = errors.New("参数异常,缺少时间戳")
  116. return
  117. }
  118. //先取出除sign外的所有的提交的参数key
  119. var keys []string
  120. for k := range postData {
  121. if k != "sign" {
  122. keys = append(keys, k)
  123. }
  124. }
  125. //1,根据参数名称的ASCII码表的顺序排序
  126. sort.Strings(keys)
  127. //2 根据排序后的参数名称,取出对应的值,并拼接字符串
  128. var signStr string
  129. for _, v := range keys {
  130. signStr += v + "=" + postData[v] + "&"
  131. }
  132. //3,全转小写(md5(拼装的字符串后+分配给你的app_secret))
  133. //sign := strings.ToLower(fmt.Sprintf("%x", md5.Sum([]byte(strings.Trim(signStr, "&")+key))))
  134. //md5.Sum([]byte(signStr+"key="+key)) 这是md5加密出来后的每个字符的ascall码,需要再转换成对应的字符
  135. //3,全转大写(md5(拼装的字符串后+分配给你的app_secret))
  136. sign = strings.ToUpper(fmt.Sprintf("%x", md5.Sum([]byte(signStr+"secret="+secret))))
  137. return
  138. }