wechat.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "go-common/app/admin/ep/saga/conf"
  7. "go-common/app/admin/ep/saga/model"
  8. "go-common/app/admin/ep/saga/service/wechat"
  9. "go-common/library/log"
  10. "github.com/pkg/errors"
  11. )
  12. const qyWechatURL = "https://qyapi.weixin.qq.com"
  13. // CollectWachatUsers send required wechat visible users stored in memcache by email
  14. func (s *Service) CollectWachatUsers(c context.Context) (err error) {
  15. var (
  16. contactInfo *model.ContactInfo
  17. userMap = make(map[string]model.RequireVisibleUser)
  18. user string
  19. )
  20. if err = s.dao.RequireVisibleUsersRedis(c, &userMap); err != nil {
  21. log.Error("get require visible user error(%v)", err)
  22. return
  23. }
  24. for k, v := range userMap {
  25. if contactInfo, err = s.dao.QueryUserByID(k); err == nil {
  26. if contactInfo.VisibleSaga {
  27. continue
  28. }
  29. }
  30. user += v.UserName + " , " + v.NickName + "\n"
  31. }
  32. /*content := fmt.Sprintf("\n\n邮箱前缀 昵称\n\n%s", user)
  33. for _, addr := range conf.Conf.Property.ReportRequiredVisible.AlertAddrs {
  34. if err = mail.SendMail2(addr, "需添加的企业微信名单", content); err != nil {
  35. return
  36. }
  37. }*/
  38. if err = s.dao.DeleteRequireVisibleUsersRedis(c); err != nil {
  39. log.Error("Delete require visible user error(%v)", err)
  40. return
  41. }
  42. return
  43. }
  44. // SyncContacts sync the wechat contacts 更新企业微信列表(用户信息和saga信息)
  45. func (s *Service) SyncContacts(c context.Context) (err error) {
  46. var (
  47. w = wechat.New(s.dao)
  48. )
  49. if err = w.SyncContacts(c); err != nil {
  50. return
  51. }
  52. return
  53. }
  54. // QueryContacts query machine logs.
  55. func (s *Service) QueryContacts(c context.Context, queryRequest *model.Pagination) (p *model.PaginateContact, err error) {
  56. var (
  57. total int64
  58. contacts []*model.ContactInfo
  59. )
  60. fmt.Print(queryRequest.PageNum)
  61. if total, contacts, err = s.dao.FindContacts(queryRequest.PageNum, queryRequest.PageSize); err != nil {
  62. return
  63. }
  64. fmt.Print(queryRequest.PageNum)
  65. p = &model.PaginateContact{
  66. PageNum: queryRequest.PageNum,
  67. PageSize: queryRequest.PageSize,
  68. Total: total,
  69. Contacts: contacts,
  70. }
  71. return
  72. }
  73. // QueryContactLogs query contact logs.
  74. func (s *Service) QueryContactLogs(c context.Context, queryRequest *model.QueryContactLogRequest) (p *model.PaginateContactLog, err error) {
  75. var (
  76. total int64
  77. machineLogs []*model.AboundContactLog
  78. )
  79. if total, machineLogs, err = s.dao.FindMachineLogs(queryRequest); err != nil {
  80. return
  81. }
  82. p = &model.PaginateContactLog{
  83. PageNum: queryRequest.PageNum,
  84. PageSize: queryRequest.PageSize,
  85. Total: total,
  86. MachineLogs: machineLogs,
  87. }
  88. return
  89. }
  90. // Wechat ...
  91. func (s *Service) Wechat() *wechat.Wechat {
  92. return wechat.New(s.dao)
  93. }
  94. // CreateWechat ...
  95. func (s *Service) CreateWechat(c context.Context, req *model.CreateChatReq, username string) (resp *model.CreateChatResp, err error) {
  96. var (
  97. token string
  98. userIDs []string
  99. ownerInfo *model.ContactInfo
  100. w = wechat.New(s.dao)
  101. )
  102. u := qyWechatURL + "/cgi-bin/appchat/create"
  103. params := url.Values{}
  104. wechatInfo := &model.WechatCreateLog{
  105. Name: req.Name,
  106. Owner: req.Owner,
  107. ChatID: req.ChatID,
  108. Cuser: username,
  109. Status: 1,
  110. }
  111. //获取企业token
  112. if token, err = w.AccessToken(c, conf.Conf.Property.Wechat); err != nil {
  113. return
  114. }
  115. params.Set("access_token", token)
  116. //get owner and users id
  117. if ownerInfo, err = s.dao.QueryUserByUserName(req.Owner); err != nil {
  118. return
  119. }
  120. if userIDs, err = s.QueryUserIds(req.UserList); err != nil {
  121. return
  122. }
  123. req.Owner = ownerInfo.UserID
  124. req.UserList = userIDs
  125. if err = s.dao.PostJSON(c, u, "", params, &resp, req); err != nil {
  126. return
  127. }
  128. //add create wechat info to database
  129. if err = s.dao.AddWechatCreateLog(wechatInfo); err != nil {
  130. return
  131. }
  132. resp = &model.CreateChatResp{
  133. ChatID: wechatInfo.ChatID,
  134. }
  135. return
  136. }
  137. // QueryUserIds ...
  138. func (s *Service) QueryUserIds(userNames []string) (userIds []string, err error) {
  139. var (
  140. userName string
  141. contactInfo *model.ContactInfo
  142. )
  143. if len(userNames) == 0 {
  144. err = errors.Errorf("UserIds: userNames is empty!")
  145. return
  146. }
  147. for _, userName = range userNames {
  148. if contactInfo, err = s.dao.QueryUserByUserName(userName); err != nil {
  149. err = errors.Wrapf(err, "UserIds: no such user (%s) in db, err (%s)", userName, err.Error())
  150. return
  151. }
  152. log.Info("UserIds: username (%s), userid (%s)", userName, contactInfo.UserID)
  153. if contactInfo.UserID != "" {
  154. userIds = append(userIds, contactInfo.UserID)
  155. }
  156. }
  157. return
  158. }
  159. // QueryWechatCreateLog ...
  160. func (s *Service) QueryWechatCreateLog(c context.Context, req *model.Pagination, username string) (resp *model.CreateChatLogResp, err error) {
  161. var (
  162. logs []*model.WechatCreateLog
  163. logsResp []*model.CreateChatLog
  164. total int
  165. wechatCreateInfo *model.WechatCreateLog
  166. )
  167. if logs, total, err = s.dao.QueryWechatCreateLog(true, req, wechatCreateInfo); err != nil {
  168. return
  169. }
  170. for _, log := range logs {
  171. createChatlog := &model.CreateChatLog{}
  172. if log.Cuser == username {
  173. createChatlog.Buttons = append(createChatlog.Buttons, "WECHAT_TEST")
  174. }
  175. createChatlog.WechatCreateLog = log
  176. logsResp = append(logsResp, createChatlog)
  177. }
  178. resp = &model.CreateChatLogResp{
  179. Total: total,
  180. Pagination: req,
  181. Logs: logsResp,
  182. }
  183. return
  184. }
  185. // WechatParams ...
  186. func (s *Service) WechatParams(c context.Context, chatid string) (resp *model.GetChatResp, err error) {
  187. var (
  188. w = wechat.New(s.dao)
  189. token string
  190. )
  191. if token, err = w.AccessToken(c, conf.Conf.Property.Wechat); err != nil {
  192. return
  193. }
  194. u := qyWechatURL + "/cgi-bin/appchat/get"
  195. params := url.Values{}
  196. params.Set("access_token", token)
  197. params.Set("chatid", chatid)
  198. err = s.dao.WechatParams(c, u, params, &resp)
  199. return
  200. }
  201. // SendGroupWechat ...
  202. func (s *Service) SendGroupWechat(c context.Context, req *model.SendChatReq) (resp *model.ChatResp, err error) {
  203. var (
  204. token string
  205. w = wechat.New(s.dao)
  206. total int
  207. getChatResp *model.GetChatResp
  208. owner string
  209. contentDB = req.Text.Content
  210. )
  211. u := qyWechatURL + "/cgi-bin/appchat/send"
  212. params := url.Values{}
  213. if token, err = w.AccessToken(c, conf.Conf.Property.Wechat); err != nil {
  214. return
  215. }
  216. params.Set("access_token", token)
  217. if err = s.dao.PostJSON(c, u, "", params, &resp, req); err != nil {
  218. return
  219. }
  220. if len(contentDB) > model.MaxWechatLen {
  221. contentDB = contentDB[:model.MaxWechatLen]
  222. }
  223. chatLog := &model.WechatChatLog{
  224. ChatID: req.ChatID,
  225. MsgType: req.MsgType,
  226. Content: contentDB,
  227. Safe: req.Safe,
  228. Status: 1,
  229. }
  230. if err = s.dao.CreateChatLog(chatLog); err != nil {
  231. return
  232. }
  233. info := &model.WechatCreateLog{
  234. ChatID: req.ChatID,
  235. }
  236. if _, total, err = s.dao.QueryWechatCreateLog(false, nil, info); err != nil {
  237. return
  238. }
  239. if total == 0 {
  240. getChatResp, _ = s.WechatParams(c, req.ChatID)
  241. owner = getChatResp.ChatInfo.Owner
  242. contactInfo, _ := s.dao.QueryUserByID(owner)
  243. wechatInfo := &model.WechatCreateLog{
  244. Name: getChatResp.ChatInfo.Name,
  245. Owner: contactInfo.UserName,
  246. ChatID: req.ChatID,
  247. Status: 2,
  248. }
  249. if err = s.dao.AddWechatCreateLog(wechatInfo); err != nil {
  250. return
  251. }
  252. }
  253. return
  254. }
  255. // SendWechat ...
  256. func (s *Service) SendWechat(c context.Context, req *model.SendMessageReq) (resp *model.ChatResp, err error) {
  257. var (
  258. w = wechat.New(s.dao)
  259. )
  260. err = w.PushMsg(c, req.Touser, req.Content)
  261. return
  262. }
  263. // UpdateWechat ...
  264. func (s *Service) UpdateWechat(c context.Context, req *model.UpdateChatReq) (resp *model.ChatResp, err error) {
  265. var (
  266. token string
  267. w = wechat.New(s.dao)
  268. )
  269. u := qyWechatURL + "/cgi-bin/appchat/update"
  270. params := url.Values{}
  271. if token, err = w.AccessToken(c, conf.Conf.Property.Wechat); err != nil {
  272. return
  273. }
  274. params.Set("access_token", token)
  275. if err = s.dao.PostJSON(c, u, "", params, &resp, req); err != nil {
  276. return
  277. }
  278. return
  279. }
  280. // SyncWechatContacts ...
  281. func (s *Service) SyncWechatContacts(c context.Context) (message string, err error) {
  282. var (
  283. w = wechat.New(s.dao)
  284. )
  285. if err = w.AnalysisContacts(c); err != nil {
  286. return
  287. }
  288. message = "同步完成"
  289. return
  290. }