123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package service
- import (
- "app.yhyue.com/moapp/jy_docs/rpc/partnerlib/entity"
- IC "app.yhyue.com/moapp/jy_docs/rpc/partnerlib/init"
- "app.yhyue.com/moapp/jy_docs/services/partner"
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/date"
- "bytes"
- "encoding/json"
- "fmt"
- "github.com/gogf/gf/v2/crypto/gmd5"
- "github.com/gogf/gf/v2/crypto/gsha1"
- "io"
- "log"
- "net/http"
- "net/url"
- "strings"
- )
- type HH struct {
- Host string
- Pathname string
- Method string
- Params *url.Values
- Name string
- ApiName string
- }
- // new
- func NewHH(name, host, apiName, pathname, method string, params url.Values) *HH {
- return &HH{
- Name: name,
- Host: host,
- ApiName: apiName,
- Pathname: pathname,
- Method: method,
- Params: ¶ms,
- }
- }
- // 豆丁api 请求
- func (h *HH) HttpFunc() (body []byte, err error, isJson bool) {
- entity.DocsLock.Lock()
- defer entity.DocsLock.Unlock()
- if h.Params == nil || h.Params.Encode() == "" {
- err = fmt.Errorf("参数异常")
- return
- }
- var (
- href string
- req *http.Request
- )
- switch h.Method {
- case "POST":
- href = fmt.Sprintf("%s%s", h.Host, h.Pathname)
- // 创建 请求并添加查询参数
- req, err = http.NewRequest(h.Method, href, strings.NewReader(h.Params.Encode()))
- // 添加请求头
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- case "GET":
- href = fmt.Sprintf("%s%s?%s", h.Host, h.Pathname, h.Params.Encode())
- // 创建 请求并添加查询参数
- req, err = http.NewRequest(h.Method, href, nil)
- // 添加请求头
- req.Header.Add("Content-Type", "application/json")
- }
- if err != nil {
- err = fmt.Errorf("error creating GET request:%v", err)
- return
- }
- //时间
- req.Header.Add("Date", h.GetDate())
- //Content-Md5
- req.Header.Add("Content-Md5", h.GetContentMd5())
- //Authorization
- req.Header.Add("Authorization", h.GetAuthorization())
- client := &http.Client{}
- // 使用 Do 方法处理请求
- resp, _err := client.Do(req)
- if _err != nil {
- err = fmt.Errorf("failed to create client:%v,err:%s", http.StatusInternalServerError, _err.Error())
- return
- }
- defer resp.Body.Close()
- body, err = io.ReadAll(resp.Body)
- isJson = strings.Contains(strings.ToLower(resp.Header.Get("Content-Type")), "text/json")
- if err != nil {
- log.Println(fmt.Sprintf("Failed to read response body:%v,err:%s", http.StatusInternalServerError, err.Error()))
- return
- }
- return
- }
- // 时间
- func (h *HH) GetDate() string {
- return date.NowFormat(entity.DateFormat)
- }
- // Content-Md5
- func (h *HH) GetContentMd5() string {
- //hash := md5.Sum([]byte(h.Pathname))
- //return strings.ToLower(hex.EncodeToString(hash[:]))
- m5, _ := gmd5.EncryptString(h.Pathname)
- return strings.ToLower(m5)
- }
- // Authorization
- func (h *HH) GetAuthorization() string {
- // 计算 SHA1 值
- //hm := hmac.New(sha1.New, []byte(IC.I.Docin.AppSecret))
- //hm.Write([]byte(IC.I.Docin.AppSecret + h.GetContentMd5() + h.GetDate()))
- //return fmt.Sprintf("WPS-2:%s:%s", IC.I.Docin.AppId, strings.ToLower(fmt.Sprintf("%x", hm.Sum(nil))))
- 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()))))
- }
- // 豆丁api请求日志 存mongo
- func (h *HH) SaveDocinLogger(b []byte, err error, mark string) {
- //var body = map[string]interface{}{}
- //if err := json.Unmarshal(b, &body); err != nil {
- // log.Println("json b to body err:", err.Error())
- //}
- var body map[string]interface{}
- dec := json.NewDecoder(bytes.NewReader(b))
- if err := dec.Decode(&body); err != nil {
- log.Println("json b to body err:", err.Error())
- }
- partner.SaveDocinLogger(map[string]interface{}{
- "name": h.Name,
- "host": h.Host,
- "apiName": h.ApiName,
- "params": h.Params,
- "method": h.Method,
- "pathname": h.Pathname,
- "createDate": date.NowFormat(date.Date_Full_Layout),
- "content": common.If(body != nil, body, string(b)),
- "ok": common.If(err == nil, true, false).(bool),
- "httpType": mark,
- })
- }
|