contact_push_notice.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package wecom
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. "sync"
  7. "wechat-api/ent/wx"
  8. "wechat-api/internal/pkg/wechat_ws"
  9. "wechat-api/internal/svc"
  10. "wechat-api/workphone/wecom"
  11. )
  12. type ContactPushNoticeHandler struct {
  13. svcCtx *svc.ServiceContext
  14. lockMap sync.Map // 微信号 -> *sync.Mutex
  15. }
  16. func NewContactPushNoticeHandler(svcCtx *svc.ServiceContext) *ContactPushNoticeHandler {
  17. return &ContactPushNoticeHandler{
  18. svcCtx: svcCtx,
  19. }
  20. }
  21. // Handle 实现 MessageHandlerStrategy 接口
  22. func (f *ContactPushNoticeHandler) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error {
  23. message := wecom.ContactPushNoticeMessage{}
  24. err := json.Unmarshal([]byte(msg.Message), &message)
  25. logx.Infof("ContactPushNoticeMessage.Message 的内容是:%s", msg.Message)
  26. if err != nil {
  27. logx.Errorf("Unmarshal.fail")
  28. return err
  29. }
  30. wxInfo, err := svcCtx.DB.Wx.Query().
  31. Where(
  32. wx.WxidEQ(message.WxId), // Additional filter by organizationId
  33. ).Only(ctx)
  34. if err != nil {
  35. return err
  36. }
  37. var ctype uint64
  38. ctype = 3
  39. for _, friend := range message.Contacts {
  40. //Wxid := strconv.FormatInt(friend.RemoteId, 10)
  41. friendType := 5
  42. var sex int
  43. if friend.Gender == "Male" {
  44. sex = 1
  45. } else if friend.Gender == "Female" {
  46. sex = 2
  47. } else {
  48. sex = 0
  49. }
  50. //修改拉黑后的状态被重置
  51. _, err = svcCtx.DB.Contact.Create().
  52. SetWxWxid(message.WxId).
  53. SetType(friendType).
  54. SetWxid(friend.RemoteId).
  55. SetNickname(friend.Name).
  56. SetMarkname(friend.Alias).
  57. SetHeadimg(friend.Avatar).
  58. SetSex(sex).
  59. SetCtype(ctype).
  60. SetOrganizationID(wxInfo.OrganizationID).
  61. OnConflict().
  62. UpdateWxWxid().
  63. UpdateWxid().
  64. UpdateType().
  65. UpdateNickname().
  66. UpdateMarkname().
  67. UpdateHeadimg().
  68. UpdateOrganizationID().
  69. UpdateSex().
  70. UpdateCtype().
  71. ID(ctx)
  72. if err != nil {
  73. logx.Errorf("Contact.Create 失败, OrgID=%d, err=%v", wxInfo.OrganizationID, err)
  74. return err
  75. }
  76. }
  77. return nil
  78. }