123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package MessageHandlers
- import (
- "context"
- "encoding/json"
- "github.com/zeromicro/go-zero/core/logx"
- "wechat-api/ent/wx"
- "wechat-api/hook"
- "wechat-api/internal/pkg/wechat_ws"
- "wechat-api/internal/svc"
- "wechat-api/workphone"
- )
- type ChatroomPushNoticeHandler struct {
- svcCtx *svc.ServiceContext
- }
- func NewChatroomPushNoticeHandler(svcCtx *svc.ServiceContext) *ChatroomPushNoticeHandler {
- return &ChatroomPushNoticeHandler{
- svcCtx: svcCtx,
- }
- }
- func (f *ChatroomPushNoticeHandler) Handler(msg *wechat_ws.MsgJsonObject) error {
- if msg.MsgType == "ChatroomPushNotice" {
- message := workphone.ChatRoomPushNoticeMessage{}
- err := json.Unmarshal([]byte(msg.Message), &message)
- if err != nil {
- return err
- }
- // 拿到租户 id
- wx_info, err := f.svcCtx.DB.Wx.Query().
- Where(
- wx.WxidEQ(message.WeChatId), // Additional filter by organizationId
- ).
- Only(context.TODO())
- hookClient := hook.NewHook("", "", "")
- for _, friend := range message.ChatRooms {
- friendType := 2
- //if friend.Type == 1 {
- // friendType = 2
- // //_ = hookClient.RequestChatRoomInfo(friend.FriendId, message.WeChatId)
- //} else {
- // friendType = 1
- //}
- _ = hookClient.RequestChatRoomInfo(friend.UserName, message.WeChatId)
- _, err = f.svcCtx.DB.Contact.Create().
- SetWxWxid(message.WeChatId).
- SetType(friendType).
- SetWxid(friend.UserName).
- SetNickname(friend.NickName).
- SetHeadimg(friend.Avatar).
- //SetSex(cast.ToInt(friend.Gender)).
- SetOrganizationID(wx_info.OrganizationID).
- OnConflict().
- UpdateNewValues().
- SetOrganizationID(wx_info.OrganizationID).
- ID(context.TODO())
- if err != nil {
- logx.Error("Contact.Create: ", wx_info.OrganizationID)
- return err
- }
- }
- }
- return nil
- }
- type ChatroomPushNoticeTypeHandler struct {
- svcCtx *svc.ServiceContext
- }
- func NewChatroomPushNoticeTypeHandler(svcCtx *svc.ServiceContext) *ChatroomPushNoticeTypeHandler {
- return &ChatroomPushNoticeTypeHandler{
- svcCtx: svcCtx,
- }
- }
- // Handle 实现 MessageHandlerStrategy 接口
- func (c *ChatroomPushNoticeTypeHandler) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error {
- message := workphone.ChatRoomPushNoticeMessage{}
- logx.Infof("msg.Message 的内容是:%s", msg.Message)
- err := json.Unmarshal([]byte(msg.Message), &message)
- if err != nil {
- return err
- }
- // 拿到租户 id
- wxInfo, err := svcCtx.DB.Wx.Query().
- Where(
- wx.WxidEQ(message.WeChatId), // Additional filter by organizationId
- ).
- Only(ctx)
- hookClient := hook.NewHook("", "", "")
- for _, friend := range message.ChatRooms {
- friendType := 2
- _ = hookClient.RequestChatRoomInfo(friend.UserName, message.WeChatId)
- _, err = svcCtx.DB.Contact.Create().
- SetWxWxid(message.WeChatId).
- SetType(friendType).
- SetWxid(friend.UserName).
- SetNickname(friend.NickName).
- SetHeadimg(friend.Avatar).
- SetOrganizationID(wxInfo.OrganizationID).
- OnConflict().
- UpdateNewValues().
- SetOrganizationID(wxInfo.OrganizationID).
- ID(ctx)
- if err != nil {
- logx.Error("Contact.Create: ", wxInfo.OrganizationID)
- return err
- }
- }
- return nil
- }
|