service.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jy_docs/rpc/partnerlib/entity"
  4. IC "app.yhyue.com/moapp/jy_docs/rpc/partnerlib/init"
  5. "app.yhyue.com/moapp/jy_docs/services/partner"
  6. "app.yhyue.com/moapp/jybase/common"
  7. "app.yhyue.com/moapp/jybase/date"
  8. "bytes"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/gogf/gf/v2/crypto/gmd5"
  12. "github.com/gogf/gf/v2/crypto/gsha1"
  13. "io"
  14. "log"
  15. "net/http"
  16. "net/url"
  17. "strings"
  18. )
  19. type HH struct {
  20. Host string
  21. Pathname string
  22. Method string
  23. Params map[string]interface{}
  24. Name string
  25. ApiName string
  26. }
  27. // new
  28. func NewHH(name, host, apiName, pathname, method string, params map[string]interface{}) *HH {
  29. return &HH{
  30. Name: name,
  31. Host: host,
  32. ApiName: apiName,
  33. Pathname: pathname,
  34. Method: method,
  35. Params: params,
  36. }
  37. }
  38. // 豆丁api 请求
  39. func (h *HH) HttpFunc() (body []byte, err error, isJson bool) {
  40. entity.DocsLock.Lock()
  41. defer entity.DocsLock.Unlock()
  42. if h.Params == nil || len(h.Params) == 0 {
  43. err = fmt.Errorf("参数异常")
  44. return
  45. }
  46. var (
  47. href string
  48. req *http.Request
  49. )
  50. switch h.Method {
  51. case "POST":
  52. var bb *bytes.Buffer
  53. href = fmt.Sprintf("%s%s", h.Host, h.Pathname)
  54. b, _err := json.Marshal(h.Params)
  55. if _err == nil {
  56. bb = bytes.NewBuffer(b)
  57. }
  58. // 创建 请求并添加查询参数
  59. req, err = http.NewRequest(h.Method, href, bb)
  60. case "GET":
  61. params := url.Values{}
  62. for k, v := range h.Params {
  63. params.Add(k, common.InterfaceToStr(v))
  64. }
  65. href = fmt.Sprintf("%s%s?%s", h.Host, h.Pathname, params.Encode())
  66. // 创建 请求并添加查询参数
  67. req, err = http.NewRequest(h.Method, href, nil)
  68. }
  69. if err != nil {
  70. err = fmt.Errorf("error creating GET request:%v", err)
  71. return
  72. }
  73. // 添加请求头
  74. req.Header.Add("Content-Type", "application/json")
  75. //时间
  76. req.Header.Add("Date", h.GetDate())
  77. //Content-Md5
  78. req.Header.Add("Content-Md5", h.GetContentMd5())
  79. //Authorization
  80. req.Header.Add("Authorization", h.GetAuthorization())
  81. client := &http.Client{}
  82. // 使用 Do 方法处理请求
  83. resp, _err := client.Do(req)
  84. if _err != nil {
  85. err = fmt.Errorf("failed to create client:%v,err:%s", http.StatusInternalServerError, _err.Error())
  86. return
  87. }
  88. defer resp.Body.Close()
  89. body, err = io.ReadAll(resp.Body)
  90. isJson = strings.Contains(strings.ToLower(resp.Header.Get("Content-Type")), "text/json")
  91. if err != nil {
  92. log.Println(fmt.Sprintf("Failed to read response body:%v,err:%s", http.StatusInternalServerError, err.Error()))
  93. return
  94. }
  95. return
  96. }
  97. // 时间
  98. func (h *HH) GetDate() string {
  99. return date.NowFormat(entity.DateFormat)
  100. }
  101. // Content-Md5
  102. func (h *HH) GetContentMd5() string {
  103. //hash := md5.Sum([]byte(h.Pathname))
  104. //return strings.ToLower(hex.EncodeToString(hash[:]))
  105. m5, _ := gmd5.EncryptString(h.Pathname)
  106. return strings.ToLower(m5)
  107. }
  108. // Authorization
  109. func (h *HH) GetAuthorization() string {
  110. // 计算 SHA1 值
  111. //hm := hmac.New(sha1.New, []byte(IC.I.Docin.AppSecret))
  112. //hm.Write([]byte(IC.I.Docin.AppSecret + h.GetContentMd5() + h.GetDate()))
  113. //return fmt.Sprintf("WPS-2:%s:%s", IC.I.Docin.AppId, strings.ToLower(fmt.Sprintf("%x", hm.Sum(nil))))
  114. return fmt.Sprintf("WPS-2:%s:%s", IC.I.Docin.AppId, strings.ToLower(gsha1.Encrypt(fmt.Sprintf("%s%s%s", IC.I.Docin.AppSecret, h.GetContentMd5(), h.GetDate()))))
  115. }
  116. // 豆丁api请求日志 存mongo
  117. func (h *HH) SaveDocinLogger(b []byte, err error, mark string) {
  118. //var body = map[string]interface{}{}
  119. //if err := json.Unmarshal(b, &body); err != nil {
  120. // log.Println("json b to body err:", err.Error())
  121. //}
  122. var body map[string]interface{}
  123. dec := json.NewDecoder(bytes.NewReader(b))
  124. if err := dec.Decode(&body); err != nil {
  125. log.Println("json b to body err:", err.Error())
  126. }
  127. partner.SaveDocinLogger(map[string]interface{}{
  128. "name": h.Name,
  129. "host": h.Host,
  130. "apiName": h.ApiName,
  131. "params": h.Params,
  132. "method": h.Method,
  133. "pathname": h.Pathname,
  134. "createDate": date.NowFormat(date.Date_Full_Layout),
  135. "content": common.If(body != nil, body, string(b)),
  136. "ok": common.If(err == nil, true, false).(bool),
  137. "httpType": mark,
  138. })
  139. }