chatroom_push_notice.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package MessageHandlers
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. "wechat-api/ent/wx"
  7. "wechat-api/hook"
  8. "wechat-api/internal/pkg/wechat_ws"
  9. "wechat-api/internal/svc"
  10. "wechat-api/workphone"
  11. )
  12. type ChatroomPushNoticeHandler struct {
  13. svcCtx *svc.ServiceContext
  14. }
  15. func NewChatroomPushNoticeHandler(svcCtx *svc.ServiceContext) *ChatroomPushNoticeHandler {
  16. return &ChatroomPushNoticeHandler{
  17. svcCtx: svcCtx,
  18. }
  19. }
  20. func (f *ChatroomPushNoticeHandler) Handler(msg *wechat_ws.MsgJsonObject) error {
  21. if msg.MsgType == "ChatroomPushNotice" {
  22. message := workphone.ChatRoomPushNoticeMessage{}
  23. err := json.Unmarshal([]byte(msg.Message), &message)
  24. if err != nil {
  25. return err
  26. }
  27. // 拿到租户 id
  28. wx_info, err := f.svcCtx.DB.Wx.Query().
  29. Where(
  30. wx.WxidEQ(message.WeChatId), // Additional filter by organizationId
  31. ).
  32. Only(context.TODO())
  33. hookClient := hook.NewHook("", "", "")
  34. for _, friend := range message.ChatRooms {
  35. friendType := 2
  36. //if friend.Type == 1 {
  37. // friendType = 2
  38. // //_ = hookClient.RequestChatRoomInfo(friend.FriendId, message.WeChatId)
  39. //} else {
  40. // friendType = 1
  41. //}
  42. _ = hookClient.RequestChatRoomInfo(friend.UserName, message.WeChatId)
  43. _, err = f.svcCtx.DB.Contact.Create().
  44. SetWxWxid(message.WeChatId).
  45. SetType(friendType).
  46. SetWxid(friend.UserName).
  47. SetNickname(friend.NickName).
  48. SetHeadimg(friend.Avatar).
  49. //SetSex(cast.ToInt(friend.Gender)).
  50. SetOrganizationID(wx_info.OrganizationID).
  51. OnConflict().
  52. UpdateNewValues().
  53. SetOrganizationID(wx_info.OrganizationID).
  54. ID(context.TODO())
  55. if err != nil {
  56. logx.Error("Contact.Create: ", wx_info.OrganizationID)
  57. return err
  58. }
  59. }
  60. }
  61. return nil
  62. }
  63. type ChatroomPushNoticeTypeHandler struct {
  64. svcCtx *svc.ServiceContext
  65. }
  66. func NewChatroomPushNoticeTypeHandler(svcCtx *svc.ServiceContext) *ChatroomPushNoticeTypeHandler {
  67. return &ChatroomPushNoticeTypeHandler{
  68. svcCtx: svcCtx,
  69. }
  70. }
  71. // Handle 实现 MessageHandlerStrategy 接口
  72. func (c *ChatroomPushNoticeTypeHandler) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error {
  73. message := workphone.ChatRoomPushNoticeMessage{}
  74. logx.Infof("msg.Message 的内容是:%s", msg.Message)
  75. err := json.Unmarshal([]byte(msg.Message), &message)
  76. if err != nil {
  77. return err
  78. }
  79. // 拿到租户 id
  80. wxInfo, err := svcCtx.DB.Wx.Query().
  81. Where(
  82. wx.WxidEQ(message.WeChatId), // Additional filter by organizationId
  83. ).
  84. Only(ctx)
  85. hookClient := hook.NewHook("", "", "")
  86. for _, friend := range message.ChatRooms {
  87. friendType := 2
  88. _ = hookClient.RequestChatRoomInfo(friend.UserName, message.WeChatId)
  89. _, err = svcCtx.DB.Contact.Create().
  90. SetWxWxid(message.WeChatId).
  91. SetType(friendType).
  92. SetWxid(friend.UserName).
  93. SetNickname(friend.NickName).
  94. SetHeadimg(friend.Avatar).
  95. SetOrganizationID(wxInfo.OrganizationID).
  96. OnConflict().
  97. UpdateNewValues().
  98. SetOrganizationID(wxInfo.OrganizationID).
  99. ID(ctx)
  100. if err != nil {
  101. logx.Error("Contact.Create: ", wxInfo.OrganizationID)
  102. return err
  103. }
  104. }
  105. return nil
  106. }