123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- package eta_bridge
- import (
- "encoding/json"
- "errors"
- "eta/eta_task/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "io"
- "net/http"
- "strings"
- )
- // BaseEtaBridgeDataResp 基础返回参数
- type BaseEtaBridgeDataResp struct {
- Code int `json:"code" description:"状态码"`
- Msg string `json:"msg" description:"提示信息"`
- //Data GetCrmTokenData `json:"data" description:"返回数据"`
- }
- // HttpEtaBridgePost
- // @Description: eta-bridge的post请求
- // @author: Roc
- // @datetime 2024-02-28 16:55:02
- // @param urlStr string
- // @param postData string
- // @param param interface{}
- // @return bResult []byte
- // @return err error
- // @return errMsg string
- func HttpEtaBridgePost(urlStr string, param interface{}) (bResult []byte, err error, errMsg string) {
- if utils.EtaBridgeUrl == "" {
- 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.EtaBridgeUrl + "/api" + 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
- }
- contentType := "application/json;charset=utf-8"
- req.Header.Set("Content-Type", contentType)
- checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
- req.Header.Set("Authorization", checkToken)
- 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
- }
- // 生产环境解密, 注意有个坑前后的双引号
- if utils.RunMode == "release" {
- str := string(bResult)
- str = strings.Trim(str, `"`)
- bResult = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
- }
- result := new(BaseEtaBridgeDataResp)
- 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.Code != 200 {
- errMsg = result.Msg
- err = fmt.Errorf("result: %s", string(bResult))
- return
- }
- return
- }
- // HttpEtaBridgeGet
- // @Description: eta-bridge的get请求
- // @author: Roc
- // @datetime 2024-02-28 16:55:02
- // @param urlStr string
- // @param postData string
- // @return bResult []byte
- // @return err error
- // @return errMsg string
- func HttpEtaBridgeGet(urlStr string) (bResult []byte, err error, errMsg string) {
- if utils.EtaBridgeUrl == "" {
- errMsg = `未配置第三方的桥接服务地址`
- err = errors.New(errMsg)
- return
- }
- getUrl := utils.EtaBridgeUrl + "/api" + urlStr
- client := &http.Client{}
- req, e := http.NewRequest("GET", getUrl, nil)
- if e != nil {
- err = fmt.Errorf("http create request err: %s", e.Error())
- return
- }
- contentType := "application/json;charset=utf-8"
- req.Header.Set("Content-Type", contentType)
- checkToken := utils.MD5(utils.EtaBridgeAppNameEn + utils.EtaBridgeMd5Key)
- req.Header.Set("Authorization", checkToken)
- 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
- }
- // 生产环境解密, 注意有个坑前后的双引号
- if utils.RunMode == "release" {
- str := string(bResult)
- str = strings.Trim(str, `"`)
- bResult = utils.DesBase64Decrypt([]byte(str), utils.EtaBridgeDesKey)
- }
- result := new(BaseEtaBridgeDataResp)
- 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请求失败,url", getUrl, ":\n", string(bResult))
- return
- }
- if result.Code != 200 {
- errMsg = result.Msg
- err = fmt.Errorf("result: %s", string(bResult))
- return
- }
- return
- }
- // EtaBridgeDataRespAndBusinessData 基础返回参数
- type EtaBridgeDataRespAndBusinessData struct {
- Code int `json:"code" description:"状态码"`
- Msg string `json:"msg" description:"提示信息"`
- Data GetBusinessDataResp `json:"data" description:"返回数据"`
- }
- type GetBusinessDataResp struct {
- List []PushBusinessIndex
- Paging *paging.PagingItem `description:"分页数据"`
- }
- // PushBusinessIndex
- // @Description: 添加外部指标(商家)请求
- type PushBusinessIndex struct {
- IndexCode string `description:"指标编码"`
- IndexName string `description:"指标名称"`
- Unit string `description:"单位"`
- Frequency string `description:"频度"`
- SourceName string `description:"数据来源名称"`
- Remark string `description:"备注字段"`
- DetailDataCreateTime string `description:"明细数据入库时间"`
- DataList []AddBusinessData `description:"指标数据"`
- }
- // AddBusinessData
- // @Description: 外部指标(商家系统)数据
- type AddBusinessData struct {
- Value float64 `description:"值"`
- Date string `description:"日期"`
- }
|