edb.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package eta_hub
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_task/utils"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "sort"
  10. "strings"
  11. "time"
  12. )
  13. // BaseEtaHubDataResp 基础返回参数
  14. type BaseEtaHubDataResp struct {
  15. Ret int
  16. Msg string
  17. ErrMsg string
  18. ErrCode string
  19. Data interface{}
  20. Success bool `description:"true 执行成功,false 执行失败"`
  21. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  22. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  23. }
  24. // HttpEtaHubPost
  25. // @Description: eta-bridge的post请求
  26. // @author: Roc
  27. // @datetime 2024-02-28 16:55:02
  28. // @param urlStr string
  29. // @param postData string
  30. // @param param interface{}
  31. // @return bResult []byte
  32. // @return err error
  33. // @return errMsg string
  34. func HttpEtaHubPost(urlStr string, param interface{}) (bResult []byte, err error, errMsg string) {
  35. if utils.EtaHubUrl == "" {
  36. errMsg = `未配置第三方的桥接服务地址`
  37. err = errors.New(errMsg)
  38. return
  39. }
  40. data, e := json.Marshal(param)
  41. if e != nil {
  42. err = fmt.Errorf("data json marshal err: %s", e.Error())
  43. return
  44. }
  45. postUrl := utils.EtaHubUrl + "/v1" + urlStr
  46. body := io.NopCloser(strings.NewReader(string(data)))
  47. client := &http.Client{}
  48. req, e := http.NewRequest("POST", postUrl, body)
  49. if e != nil {
  50. err = fmt.Errorf("http create request err: %s", e.Error())
  51. return
  52. }
  53. // 随机字符串
  54. nonce := utils.GetRandStringNoSpecialChar(16)
  55. timestamp := fmt.Sprint(time.Now().Second())
  56. sign := GetSign(nonce, timestamp)
  57. contentType := "application/json;charset=utf-8"
  58. req.Header.Set("Content-Type", contentType)
  59. req.Header.Set("nonce", nonce)
  60. req.Header.Set("timestamp", timestamp)
  61. req.Header.Set("appid", utils.APPID)
  62. req.Header.Set("signature", sign)
  63. resp, e := client.Do(req)
  64. if e != nil {
  65. err = fmt.Errorf("http client do err: %s", e.Error())
  66. return
  67. }
  68. defer func() {
  69. _ = resp.Body.Close()
  70. }()
  71. bResult, e = io.ReadAll(resp.Body)
  72. if e != nil {
  73. err = fmt.Errorf("resp body read err: %s", e.Error())
  74. return
  75. }
  76. if len(bResult) == 0 {
  77. err = fmt.Errorf("resp body is empty")
  78. return
  79. }
  80. result := new(BaseEtaHubDataResp)
  81. if e = json.Unmarshal(bResult, &result); e != nil {
  82. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(bResult))
  83. utils.FileLog.Info("桥接服务post请求失败:\n" + string(bResult))
  84. return
  85. }
  86. if result.Ret != 200 {
  87. errMsg = result.Msg
  88. err = fmt.Errorf("result: %s", string(bResult))
  89. return
  90. }
  91. return
  92. }
  93. // HttpEtaHubGet
  94. // @Description: eta-bridge的get请求
  95. // @author: Roc
  96. // @datetime 2024-02-28 16:55:02
  97. // @param urlStr string
  98. // @param postData string
  99. // @return bResult []byte
  100. // @return err error
  101. // @return errMsg string
  102. func HttpEtaHubGet(urlStr string) (bResult []byte, err error, errMsg string) {
  103. if utils.EtaHubUrl == "" {
  104. errMsg = `未配置第三方的桥接服务地址`
  105. err = errors.New(errMsg)
  106. return
  107. }
  108. postUrl := utils.EtaHubUrl + "/v1" + urlStr
  109. client := &http.Client{}
  110. req, e := http.NewRequest("GET", postUrl, nil)
  111. if e != nil {
  112. err = fmt.Errorf("http create request err: %s", e.Error())
  113. return
  114. }
  115. contentType := "application/json;charset=utf-8"
  116. // 随机字符串
  117. nonce := utils.GetRandStringNoSpecialChar(16)
  118. timestamp := fmt.Sprint(time.Now().Second())
  119. sign := GetSign(nonce, timestamp)
  120. req.Header.Set("Content-Type", contentType)
  121. req.Header.Set("nonce", nonce)
  122. req.Header.Set("timestamp", timestamp)
  123. req.Header.Set("appid", utils.APPID)
  124. req.Header.Set("signature", sign)
  125. resp, e := client.Do(req)
  126. if e != nil {
  127. err = fmt.Errorf("http client do err: %s", e.Error())
  128. return
  129. }
  130. defer func() {
  131. _ = resp.Body.Close()
  132. }()
  133. bResult, e = io.ReadAll(resp.Body)
  134. if e != nil {
  135. err = fmt.Errorf("resp body read err: %s", e.Error())
  136. return
  137. }
  138. if len(bResult) == 0 {
  139. err = fmt.Errorf("resp body is empty")
  140. return
  141. }
  142. result := new(BaseEtaHubDataResp)
  143. if e = json.Unmarshal(bResult, &result); e != nil {
  144. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(bResult))
  145. utils.FileLog.Info("桥接服务get请求失败:\n" + string(bResult))
  146. return
  147. }
  148. if result.Ret != 200 {
  149. errMsg = result.Msg
  150. err = fmt.Errorf("result: %s", string(bResult))
  151. return
  152. }
  153. return
  154. }
  155. func GetSign(nonce, timestamp string) (sign string) {
  156. signStrMap := map[string]string{
  157. "nonce": nonce,
  158. "timestamp": timestamp,
  159. "appid": utils.APPID,
  160. }
  161. keys := make([]string, 0, len(signStrMap))
  162. for k := range signStrMap {
  163. keys = append(keys, k)
  164. }
  165. sort.Strings(keys)
  166. var signStr string
  167. for _, k := range keys {
  168. signStr += k + "=" + signStrMap[k] + "&"
  169. }
  170. signStr = strings.Trim(signStr, "&")
  171. //fmt.Println("signStr:" + signStr)
  172. sign = utils.HmacSha256ToBase64(utils.SECRET, signStr)
  173. //fmt.Println("sign:" + sign)
  174. return
  175. }