chatroom_push_notice.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. }