|
@@ -0,0 +1,194 @@
|
|
|
+package eta_hub
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "eta/eta_task/utils"
|
|
|
+ "fmt"
|
|
|
+ "io"
|
|
|
+ "net/http"
|
|
|
+ "sort"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+type BaseEtaHubDataResp struct {
|
|
|
+ Ret int
|
|
|
+ Msg string
|
|
|
+ ErrMsg string
|
|
|
+ ErrCode string
|
|
|
+ Data interface{}
|
|
|
+ Success bool `description:"true 执行成功,false 执行失败"`
|
|
|
+ IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
|
|
|
+ IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func HttpEtaHubPost(urlStr string, param interface{}) (bResult []byte, err error, errMsg string) {
|
|
|
+ if utils.EtaHubUrl == "" {
|
|
|
+ errMsg = `未配置第三方的桥接服务地址`
|
|
|
+ err = errors.New(errMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ data, e := json.Marshal(param)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("data json marshal err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ postUrl := utils.EtaHubUrl + "/v1" + urlStr
|
|
|
+
|
|
|
+ body := io.NopCloser(strings.NewReader(string(data)))
|
|
|
+ client := &http.Client{}
|
|
|
+ req, e := http.NewRequest("POST", postUrl, body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http create request err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ nonce := utils.GetRandStringNoSpecialChar(16)
|
|
|
+ timestamp := fmt.Sprint(time.Now().Second())
|
|
|
+ sign := GetSign(nonce, timestamp)
|
|
|
+
|
|
|
+ contentType := "application/json;charset=utf-8"
|
|
|
+ req.Header.Set("Content-Type", contentType)
|
|
|
+ req.Header.Set("nonce", nonce)
|
|
|
+ req.Header.Set("timestamp", timestamp)
|
|
|
+ req.Header.Set("appid", utils.APPID)
|
|
|
+ req.Header.Set("signature", sign)
|
|
|
+
|
|
|
+ resp, e := client.Do(req)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http client do err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ _ = resp.Body.Close()
|
|
|
+ }()
|
|
|
+ bResult, e = io.ReadAll(resp.Body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("resp body read err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(bResult) == 0 {
|
|
|
+ err = fmt.Errorf("resp body is empty")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ result := new(BaseEtaHubDataResp)
|
|
|
+ if e = json.Unmarshal(bResult, &result); e != nil {
|
|
|
+ err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(bResult))
|
|
|
+ utils.FileLog.Info("桥接服务post请求失败:\n" + string(bResult))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if result.Ret != 200 {
|
|
|
+ errMsg = result.Msg
|
|
|
+ err = fmt.Errorf("result: %s", string(bResult))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func HttpEtaHubGet(urlStr string) (bResult []byte, err error, errMsg string) {
|
|
|
+ if utils.EtaHubUrl == "" {
|
|
|
+ errMsg = `未配置第三方的桥接服务地址`
|
|
|
+ err = errors.New(errMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ postUrl := utils.EtaHubUrl + "/v1" + urlStr
|
|
|
+
|
|
|
+ client := &http.Client{}
|
|
|
+ req, e := http.NewRequest("GET", postUrl, nil)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http create request err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ contentType := "application/json;charset=utf-8"
|
|
|
+
|
|
|
+ nonce := utils.GetRandStringNoSpecialChar(16)
|
|
|
+ timestamp := fmt.Sprint(time.Now().Second())
|
|
|
+ sign := GetSign(nonce, timestamp)
|
|
|
+
|
|
|
+ req.Header.Set("Content-Type", contentType)
|
|
|
+ req.Header.Set("nonce", nonce)
|
|
|
+ req.Header.Set("timestamp", timestamp)
|
|
|
+ req.Header.Set("appid", utils.APPID)
|
|
|
+ req.Header.Set("signature", sign)
|
|
|
+
|
|
|
+ resp, e := client.Do(req)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("http client do err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ _ = resp.Body.Close()
|
|
|
+ }()
|
|
|
+ bResult, e = io.ReadAll(resp.Body)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("resp body read err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(bResult) == 0 {
|
|
|
+ err = fmt.Errorf("resp body is empty")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ result := new(BaseEtaHubDataResp)
|
|
|
+ if e = json.Unmarshal(bResult, &result); e != nil {
|
|
|
+ err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(bResult))
|
|
|
+ utils.FileLog.Info("桥接服务get请求失败:\n" + string(bResult))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if result.Ret != 200 {
|
|
|
+ errMsg = result.Msg
|
|
|
+ err = fmt.Errorf("result: %s", string(bResult))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func GetSign(nonce, timestamp string) (sign string) {
|
|
|
+ signStrMap := map[string]string{
|
|
|
+ "nonce": nonce,
|
|
|
+ "timestamp": timestamp,
|
|
|
+ "appid": utils.APPID,
|
|
|
+ }
|
|
|
+ keys := make([]string, 0, len(signStrMap))
|
|
|
+ for k := range signStrMap {
|
|
|
+ keys = append(keys, k)
|
|
|
+ }
|
|
|
+ sort.Strings(keys)
|
|
|
+ var signStr string
|
|
|
+ for _, k := range keys {
|
|
|
+ signStr += k + "=" + signStrMap[k] + "&"
|
|
|
+ }
|
|
|
+ signStr = strings.Trim(signStr, "&")
|
|
|
+
|
|
|
+ sign = utils.HmacSha256ToBase64(utils.SECRET, signStr)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|