http.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. xhttp "net/http"
  8. "net/url"
  9. "strconv"
  10. "go-common/app/tool/saga/model"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. qyWechatURL = "https://qyapi.weixin.qq.com"
  15. corpID = "wx0833ac9926284fa5" // 企业微信:Bilibili的企业ID
  16. departmentID = "12" // 公司统一用部门ID
  17. )
  18. // WechatPushMsg wechat push text message to specified user
  19. func (d *Dao) WechatPushMsg(c context.Context, token string, txtMsg *model.TxtNotification) (invalidUser string, err error) {
  20. var (
  21. u string
  22. params = url.Values{}
  23. res struct {
  24. ErrCode int `json:"errcode"`
  25. ErrMsg string `json:"errmsg"`
  26. InvalidUser string `json:"invaliduser"`
  27. InvalidParty string `json:"invalidparty"`
  28. InvalidTag string `json:"invalidtag"`
  29. }
  30. )
  31. u = qyWechatURL + "/cgi-bin/message/send"
  32. params.Set("access_token", token)
  33. if err = d.PostJSON(c, u, "", params, &res, txtMsg); err != nil {
  34. return
  35. }
  36. if res.ErrCode != 0 || res.InvalidUser != "" || res.InvalidParty != "" || res.InvalidTag != "" {
  37. invalidUser = res.InvalidUser
  38. err = errors.Errorf("WechatPushMsg: errcode: %d, errmsg: %s, invalidUser: %s, invalidParty: %s, invalidTag: %s",
  39. res.ErrCode, res.ErrMsg, res.InvalidUser, res.InvalidParty, res.InvalidTag)
  40. return
  41. }
  42. return
  43. }
  44. // PostJSON post http request with json params.
  45. func (d *Dao) PostJSON(c context.Context, uri, ip string, params url.Values, res interface{}, v interface{}) (err error) {
  46. var (
  47. body = &bytes.Buffer{}
  48. req *xhttp.Request
  49. url string
  50. en string
  51. )
  52. if err = json.NewEncoder(body).Encode(v); err != nil {
  53. return
  54. }
  55. url = uri
  56. if en = params.Encode(); en != "" {
  57. url += "?" + en
  58. }
  59. if req, err = xhttp.NewRequest(xhttp.MethodPost, url, body); err != nil {
  60. return
  61. }
  62. if err = d.httpClient.Do(c, req, &res); err != nil {
  63. return
  64. }
  65. return
  66. }
  67. // WechatAccessToken query access token with the specified secret
  68. func (d *Dao) WechatAccessToken(c context.Context, secret string) (token string, expire int32, err error) {
  69. var (
  70. u string
  71. params = url.Values{}
  72. res struct {
  73. ErrCode int `json:"errcode"`
  74. ErrMsg string `json:"errmsg"`
  75. AccessToken string `json:"access_token"`
  76. ExpiresIn int32 `json:"expires_in"`
  77. }
  78. )
  79. u = qyWechatURL + "/cgi-bin/gettoken"
  80. params.Set("corpid", corpID)
  81. params.Set("corpsecret", secret)
  82. if err = d.httpClient.Get(c, u, "", params, &res); err != nil {
  83. return
  84. }
  85. if res.ErrCode != 0 {
  86. err = errors.Errorf("WechatAccessToken: errcode: %d, errmsg: %s", res.ErrCode, res.ErrMsg)
  87. return
  88. }
  89. token = res.AccessToken
  90. expire = res.ExpiresIn
  91. return
  92. }
  93. // WechatContacts query all the contacts
  94. func (d *Dao) WechatContacts(c context.Context, accessToken string) (contacts []*model.ContactInfo, err error) {
  95. var (
  96. u string
  97. params = url.Values{}
  98. res struct {
  99. ErrCode int `json:"errcode"`
  100. ErrMsg string `json:"errmsg"`
  101. UserList []*model.ContactInfo `json:"userlist"`
  102. }
  103. )
  104. u = qyWechatURL + "/cgi-bin/user/list"
  105. params.Set("access_token", accessToken)
  106. params.Set("department_id", departmentID)
  107. params.Set("fetch_child", "1")
  108. if err = d.httpClient.Get(c, u, "", params, &res); err != nil {
  109. return
  110. }
  111. if res.ErrCode != 0 {
  112. err = errors.Errorf("WechatContacts: errcode: %d, errmsg: %s", res.ErrCode, res.ErrMsg)
  113. return
  114. }
  115. contacts = res.UserList
  116. return
  117. }
  118. // WechatSagaVisible get all the user ids who can visiable saga
  119. func (d *Dao) WechatSagaVisible(c context.Context, accessToken string, agentID int) (users []*model.UserInfo, err error) {
  120. var (
  121. u string
  122. params = url.Values{}
  123. res struct {
  124. ErrCode int `json:"errcode"`
  125. ErrMsg string `json:"errmsg"`
  126. VisibleUsers model.AllowUserInfo `json:"allow_userinfos"`
  127. }
  128. )
  129. u = qyWechatURL + "/cgi-bin/agent/get"
  130. params.Set("access_token", accessToken)
  131. params.Set("agentid", strconv.Itoa(agentID))
  132. if err = d.httpClient.Get(c, u, "", params, &res); err != nil {
  133. return
  134. }
  135. if res.ErrCode != 0 {
  136. err = errors.Errorf("WechatSagaVisible: errcode: %d, errmsg: %s", res.ErrCode, res.ErrMsg)
  137. return
  138. }
  139. users = res.VisibleUsers.Users
  140. return
  141. }
  142. // RepoFiles ...TODO 该方法放在gitlab里比较好
  143. func (d *Dao) RepoFiles(c context.Context, Host string, token string, repo *model.RepoInfo) (files []string, err error) {
  144. var (
  145. u string
  146. req *xhttp.Request
  147. )
  148. u = fmt.Sprintf("http://%s/%s/%s/files/%s?format=json", Host, repo.Group, repo.Name, repo.Branch)
  149. if req, err = d.newRequest("GET", u, nil); err != nil {
  150. return
  151. }
  152. req.Header.Set("PRIVATE-TOKEN", token)
  153. req.Header.Set("Content-Type", "application/json")
  154. req.Header.Set("Accept", "application/json")
  155. if err = d.httpClient.Do(c, req, &files); err != nil {
  156. return
  157. }
  158. return
  159. }