wechat.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/tool/saga/conf"
  6. "go-common/app/tool/saga/model"
  7. "go-common/app/tool/saga/service/mail"
  8. "go-common/app/tool/saga/service/wechat"
  9. "go-common/library/log"
  10. "github.com/pkg/errors"
  11. )
  12. // CollectWachatUsers send required wechat visible users stored in memcache by email
  13. func (s *Service) CollectWachatUsers(c context.Context) (err error) {
  14. var (
  15. contactInfo *model.ContactInfo
  16. userMap = make(map[string]model.RequireVisibleUser)
  17. user string
  18. )
  19. if err = s.d.RequireVisibleUsers(c, &userMap); err != nil {
  20. log.Error("get require visible user error(%v)", err)
  21. return
  22. }
  23. for k, v := range userMap {
  24. if contactInfo, err = s.d.QueryUserByID(k); err == nil {
  25. if contactInfo.VisibleSaga {
  26. continue
  27. }
  28. }
  29. user += v.UserName + " , " + v.NickName + "\n"
  30. }
  31. content := fmt.Sprintf("\n\n邮箱前缀 昵称\n\n%s", user)
  32. for _, addr := range conf.Conf.Property.ReportRequiredVisible.AlertAddrs {
  33. if err = mail.SendMail2(addr, "需添加的企业微信名单", content); err != nil {
  34. return
  35. }
  36. }
  37. if err = s.d.DeleteRequireVisibleUsers(c); err != nil {
  38. log.Error("Delete require visible user error(%v)", err)
  39. return
  40. }
  41. return
  42. }
  43. // SyncContacts sync the wechat contacts
  44. func (s *Service) SyncContacts(c context.Context) (err error) {
  45. var (
  46. w = wechat.New(s.d)
  47. )
  48. if err = w.SyncContacts(c); err != nil {
  49. return
  50. }
  51. return
  52. }
  53. // synccontactsproc sync wechat contact procedure
  54. func (s *Service) synccontactsproc() {
  55. defer func() {
  56. if x := recover(); x != nil {
  57. log.Error("synccontactsproc panic(%v)", errors.WithStack(fmt.Errorf("%v", x)))
  58. go s.synccontactsproc()
  59. log.Info("synccontactsproc recover")
  60. }
  61. }()
  62. var err error
  63. if err = s.SyncContacts(context.TODO()); err != nil {
  64. log.Error("s.SyncContacts err (%+v)", err)
  65. }
  66. }