service.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 *url.Values
  24. Name string
  25. ApiName string
  26. }
  27. // new
  28. func NewHH(name, host, apiName, pathname, method string, params url.Values) *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 || h.Params.Encode() == "" {
  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. href = fmt.Sprintf("%s%s", h.Host, h.Pathname)
  53. // 创建 请求并添加查询参数
  54. req, err = http.NewRequest(h.Method, href, strings.NewReader(h.Params.Encode()))
  55. // 添加请求头
  56. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  57. case "GET":
  58. href = fmt.Sprintf("%s%s?%s", h.Host, h.Pathname, h.Params.Encode())
  59. // 创建 请求并添加查询参数
  60. req, err = http.NewRequest(h.Method, href, nil)
  61. // 添加请求头
  62. req.Header.Add("Content-Type", "application/json")
  63. }
  64. if err != nil {
  65. err = fmt.Errorf("error creating GET request:%v", err)
  66. return
  67. }
  68. //时间
  69. req.Header.Add("Date", h.GetDate())
  70. //Content-Md5
  71. req.Header.Add("Content-Md5", h.GetContentMd5())
  72. //Authorization
  73. req.Header.Add("Authorization", h.GetAuthorization())
  74. client := &http.Client{}
  75. // 使用 Do 方法处理请求
  76. resp, _err := client.Do(req)
  77. if _err != nil {
  78. err = fmt.Errorf("failed to create client:%v,err:%s", http.StatusInternalServerError, _err.Error())
  79. return
  80. }
  81. defer resp.Body.Close()
  82. body, err = io.ReadAll(resp.Body)
  83. isJson = strings.Contains(strings.ToLower(resp.Header.Get("Content-Type")), "text/json")
  84. if err != nil {
  85. log.Println(fmt.Sprintf("Failed to read response body:%v,err:%s", http.StatusInternalServerError, err.Error()))
  86. return
  87. }
  88. return
  89. }
  90. // 时间
  91. func (h *HH) GetDate() string {
  92. return date.NowFormat(entity.DateFormat)
  93. }
  94. // Content-Md5
  95. func (h *HH) GetContentMd5() string {
  96. //hash := md5.Sum([]byte(h.Pathname))
  97. //return strings.ToLower(hex.EncodeToString(hash[:]))
  98. m5, _ := gmd5.EncryptString(h.Pathname)
  99. return strings.ToLower(m5)
  100. }
  101. // Authorization
  102. func (h *HH) GetAuthorization() string {
  103. // 计算 SHA1 值
  104. //hm := hmac.New(sha1.New, []byte(IC.I.Docin.AppSecret))
  105. //hm.Write([]byte(IC.I.Docin.AppSecret + h.GetContentMd5() + h.GetDate()))
  106. //return fmt.Sprintf("WPS-2:%s:%s", IC.I.Docin.AppId, strings.ToLower(fmt.Sprintf("%x", hm.Sum(nil))))
  107. 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()))))
  108. }
  109. // 豆丁api请求日志 存mongo
  110. func (h *HH) SaveDocinLogger(b []byte, err error, mark string) {
  111. //var body = map[string]interface{}{}
  112. //if err := json.Unmarshal(b, &body); err != nil {
  113. // log.Println("json b to body err:", err.Error())
  114. //}
  115. var body map[string]interface{}
  116. dec := json.NewDecoder(bytes.NewReader(b))
  117. if err := dec.Decode(&body); err != nil {
  118. log.Println("json b to body err:", err.Error())
  119. }
  120. partner.SaveDocinLogger(map[string]interface{}{
  121. "name": h.Name,
  122. "host": h.Host,
  123. "apiName": h.ApiName,
  124. "params": h.Params,
  125. "method": h.Method,
  126. "pathname": h.Pathname,
  127. "createDate": date.NowFormat(date.Date_Full_Layout),
  128. "content": common.If(body != nil, body, string(b)),
  129. "ok": common.If(err == nil, true, false).(bool),
  130. "httpType": mark,
  131. })
  132. }