123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package wecom
- import (
- "context"
- "encoding/json"
- "github.com/zeromicro/go-zero/core/logx"
- "sync"
- "wechat-api/ent/wx"
- "wechat-api/internal/pkg/wechat_ws"
- "wechat-api/internal/svc"
- "wechat-api/workphone/wecom"
- )
- type ContactPushNoticeHandler struct {
- svcCtx *svc.ServiceContext
- lockMap sync.Map // 微信号 -> *sync.Mutex
- }
- func NewContactPushNoticeHandler(svcCtx *svc.ServiceContext) *ContactPushNoticeHandler {
- return &ContactPushNoticeHandler{
- svcCtx: svcCtx,
- }
- }
- // Handle 实现 MessageHandlerStrategy 接口
- func (f *ContactPushNoticeHandler) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error {
- message := wecom.ContactPushNoticeMessage{}
- err := json.Unmarshal([]byte(msg.Message), &message)
- logx.Infof("ContactPushNoticeMessage.Message 的内容是:%s", msg.Message)
- if err != nil {
- logx.Errorf("Unmarshal.fail")
- return err
- }
- wxInfo, err := svcCtx.DB.Wx.Query().
- Where(
- wx.WxidEQ(message.WxId), // Additional filter by organizationId
- ).Only(ctx)
- if err != nil {
- return err
- }
- var ctype uint64
- ctype = 3
- for _, friend := range message.Contacts {
- //Wxid := strconv.FormatInt(friend.RemoteId, 10)
- friendType := 5
- var sex int
- if friend.Gender == "Male" {
- sex = 1
- } else if friend.Gender == "Female" {
- sex = 2
- } else {
- sex = 0
- }
- //修改拉黑后的状态被重置
- _, err = svcCtx.DB.Contact.Create().
- SetWxWxid(message.WxId).
- SetType(friendType).
- SetWxid(friend.RemoteId).
- SetNickname(friend.Name).
- SetMarkname(friend.Alias).
- SetHeadimg(friend.Avatar).
- SetSex(sex).
- SetCtype(ctype).
- SetOrganizationID(wxInfo.OrganizationID).
- OnConflict().
- UpdateWxWxid().
- UpdateWxid().
- UpdateType().
- UpdateNickname().
- UpdateMarkname().
- UpdateHeadimg().
- UpdateOrganizationID().
- UpdateSex().
- UpdateCtype().
- ID(ctx)
- if err != nil {
- logx.Errorf("Contact.Create 失败, OrgID=%d, err=%v", wxInfo.OrganizationID, err)
- return err
- }
- }
- return nil
- }
|