http.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "net/http"
  7. xhttp "net/http"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "go-common/app/admin/ep/saga/model"
  12. "go-common/library/ecode"
  13. "go-common/library/log"
  14. "github.com/pkg/errors"
  15. )
  16. const (
  17. qyWechatURL = "https://qyapi.weixin.qq.com"
  18. corpID = "wx0833ac9926284fa5" // 企业微信:Bilibili的企业ID
  19. departmentID = "12" // 公司统一用部门ID
  20. //corpID = "wwa24b497e5efdd78c" // 香满庭
  21. //departmentID = "2" // 香满庭
  22. _ajSessionID = "_AJSESSIONID"
  23. )
  24. // WechatPushMsg wechat push text message to specified user 发送企业微信信息
  25. func (d *Dao) WechatPushMsg(c context.Context, token string, txtMsg *model.TxtNotification) (invalidUser string, err error) {
  26. var (
  27. u string
  28. params = url.Values{}
  29. res struct {
  30. ErrCode int `json:"errcode"`
  31. ErrMsg string `json:"errmsg"`
  32. InvalidUser string `json:"invaliduser"`
  33. InvalidParty string `json:"invalidparty"`
  34. InvalidTag string `json:"invalidtag"`
  35. }
  36. )
  37. u = qyWechatURL + "/cgi-bin/message/send"
  38. params.Set("access_token", token)
  39. if err = d.PostJSON(c, u, "", params, &res, txtMsg); err != nil {
  40. log.Info("WechatPushMsg PostJSON err (%+v)", err)
  41. return
  42. }
  43. if res.ErrCode != 0 || res.InvalidUser != "" || res.InvalidParty != "" || res.InvalidTag != "" {
  44. invalidUser = res.InvalidUser
  45. err = errors.Errorf("WechatPushMsg: errcode: %d, errmsg: %s, invalidUser: %s, invalidParty: %s, invalidTag: %s",
  46. res.ErrCode, res.ErrMsg, res.InvalidUser, res.InvalidParty, res.InvalidTag)
  47. log.Info("WechatPushMsg err (%+v)", err)
  48. return
  49. }
  50. return
  51. }
  52. // PostJSON post http request with json params.
  53. func (d *Dao) PostJSON(c context.Context, uri, ip string, params url.Values, res interface{}, v interface{}) (err error) {
  54. var (
  55. body = &bytes.Buffer{}
  56. req *xhttp.Request
  57. url string
  58. en string
  59. )
  60. if err = json.NewEncoder(body).Encode(v); err != nil {
  61. return
  62. }
  63. url = uri
  64. if en = params.Encode(); en != "" {
  65. url += "?" + en
  66. }
  67. if req, err = xhttp.NewRequest(xhttp.MethodPost, url, body); err != nil {
  68. return
  69. }
  70. if err = d.httpClient.Do(c, req, &res); err != nil {
  71. return
  72. }
  73. return
  74. }
  75. // WechatAccessToken query access token with the specified secret 企业微信api获取公司token
  76. func (d *Dao) WechatAccessToken(c context.Context, secret string) (token string, expire int32, err error) {
  77. var (
  78. u string
  79. params = url.Values{}
  80. res struct {
  81. ErrCode int `json:"errcode"`
  82. ErrMsg string `json:"errmsg"`
  83. AccessToken string `json:"access_token"`
  84. ExpiresIn int32 `json:"expires_in"`
  85. }
  86. )
  87. u = qyWechatURL + "/cgi-bin/gettoken"
  88. params.Set("corpid", corpID)
  89. params.Set("corpsecret", secret)
  90. if err = d.httpClient.Get(c, u, "", params, &res); err != nil {
  91. return
  92. }
  93. if res.ErrCode != 0 {
  94. err = errors.Errorf("WechatAccessToken: errcode: %d, errmsg: %s", res.ErrCode, res.ErrMsg)
  95. return
  96. }
  97. token = res.AccessToken
  98. expire = res.ExpiresIn
  99. return
  100. }
  101. // WechatContacts query all the contacts
  102. func (d *Dao) WechatContacts(c context.Context, accessToken string) (contacts []*model.ContactInfo, err error) {
  103. var (
  104. u string
  105. params = url.Values{}
  106. res struct {
  107. ErrCode int `json:"errcode"`
  108. ErrMsg string `json:"errmsg"`
  109. UserList []*model.ContactInfo `json:"userlist"`
  110. }
  111. )
  112. u = qyWechatURL + "/cgi-bin/user/list"
  113. params.Set("access_token", accessToken)
  114. params.Set("department_id", departmentID)
  115. params.Set("fetch_child", "1")
  116. if err = d.httpClient.Get(c, u, "", params, &res); err != nil {
  117. return
  118. }
  119. if res.ErrCode != 0 {
  120. err = errors.Errorf("WechatContacts: errcode: %d, errmsg: %s", res.ErrCode, res.ErrMsg)
  121. return
  122. }
  123. contacts = res.UserList
  124. return
  125. }
  126. // WechatSagaVisible get all the user ids who can visiable saga 获取用户ID列表
  127. func (d *Dao) WechatSagaVisible(c context.Context, accessToken string, agentID int) (users []*model.UserInfo, err error) {
  128. var (
  129. u string
  130. params = url.Values{}
  131. res struct {
  132. ErrCode int `json:"errcode"`
  133. ErrMsg string `json:"errmsg"`
  134. VisibleUsers model.AllowUserInfo `json:"allow_userinfos"`
  135. }
  136. )
  137. u = qyWechatURL + "/cgi-bin/agent/get"
  138. params.Set("access_token", accessToken)
  139. params.Set("agentid", strconv.Itoa(agentID))
  140. if err = d.httpClient.Get(c, u, "", params, &res); err != nil {
  141. return
  142. }
  143. if res.ErrCode != 0 {
  144. err = errors.Errorf("WechatSagaVisible: errcode: %d, errmsg: %s", res.ErrCode, res.ErrMsg)
  145. return
  146. }
  147. users = res.VisibleUsers.Users
  148. return
  149. }
  150. // WechatParams ...
  151. func (d *Dao) WechatParams(c context.Context, u string, params url.Values, resp interface{}) (err error) {
  152. var (
  153. req *xhttp.Request
  154. en string
  155. )
  156. if en = params.Encode(); en != "" {
  157. u += "?" + en
  158. }
  159. if req, err = xhttp.NewRequest(xhttp.MethodGet, u, nil); err != nil {
  160. return
  161. }
  162. return d.httpClient.Do(c, req, &resp)
  163. }
  164. // NewRequest ...
  165. func (d *Dao) NewRequest(method, url string, v interface{}) (req *http.Request, err error) {
  166. body := &bytes.Buffer{}
  167. if method != http.MethodGet {
  168. if err = json.NewEncoder(body).Encode(v); err != nil {
  169. log.Error("json encode value(%s), error(%v) ", v, err)
  170. return
  171. }
  172. }
  173. if req, err = http.NewRequest(method, url, body); err != nil {
  174. log.Error("http new request url(%s), error(%v)", url, err)
  175. }
  176. return
  177. }
  178. // QueryAllConfigFile ...
  179. func (d *Dao) QueryAllConfigFile(c context.Context, sessionID, url string) (resp *model.ConfigData, err error) {
  180. var (
  181. req *http.Request
  182. respValue = &model.SvenResp{}
  183. )
  184. log.Info("QueryAllConfigFile: sessionID: %s, url: %s", sessionID, url)
  185. if req, err = d.NewRequest(http.MethodGet, url, nil); err != nil {
  186. return
  187. }
  188. req.Header.Set("Cookie", _ajSessionID+"="+sessionID)
  189. if err = d.httpClient.Do(c, req, &respValue); err != nil {
  190. return
  191. }
  192. if respValue.Code != ecode.OK.Code() {
  193. err = errors.Wrapf(ecode.Int(respValue.Code), "QueryAllConfigFile failed, sessionID(%s), url(%s)", sessionID, url)
  194. return
  195. }
  196. resp = respValue.Data
  197. return
  198. }
  199. // QueryConfigFileContent ...
  200. func (d *Dao) QueryConfigFileContent(c context.Context, sessionID, url string) (content string, err error) {
  201. var (
  202. req *http.Request
  203. respValue = &model.ConfigValueResp{}
  204. )
  205. if req, err = d.NewRequest(http.MethodGet, url, nil); err != nil {
  206. return
  207. }
  208. req.Header.Set("Cookie", _ajSessionID+"="+sessionID)
  209. if err = d.httpClient.Do(c, req, respValue); err != nil {
  210. return
  211. }
  212. if respValue.Code != ecode.OK.Code() {
  213. err = errors.Wrapf(ecode.Int(respValue.Code), "QueryConfigFileContent failed, sessionID(%s), url(%s)", sessionID, url)
  214. return
  215. }
  216. if respValue.Data != nil {
  217. content = respValue.Data.Comment
  218. }
  219. return
  220. }
  221. // RequestConfig ...
  222. func (d *Dao) RequestConfig(c context.Context, sessionID, reqUrl string, params url.Values) (resp *model.CommonResp, err error) {
  223. var req *http.Request
  224. if req, err = http.NewRequest("POST", reqUrl, strings.NewReader(params.Encode())); err != nil {
  225. log.Error("http.NewRequest error(%v) | uri(%s) params(%s)", err, reqUrl, params.Encode())
  226. return
  227. }
  228. log.Info("RequestConfig url: %v", req.URL)
  229. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  230. req.Header.Set("Cookie", _ajSessionID+"="+sessionID)
  231. if err = d.httpClient.Do(c, req, &resp); err != nil {
  232. log.Error("RequestConfig err:%+v", err)
  233. return
  234. }
  235. if resp.Code != ecode.OK.Code() {
  236. err = errors.Wrapf(ecode.Int(resp.Code), "RequestConfig failed, sessionID(%s), url(%s)", sessionID, reqUrl)
  237. return
  238. }
  239. return
  240. }