Browse Source

临时提交

boweniac 1 week ago
parent
commit
76e55b0b08
4 changed files with 212 additions and 30 deletions
  1. 22 17
      crontask/init.go
  2. 104 0
      crontask/sync_wx.go
  3. 69 0
      internal/service/MessageHandlers/friend_push_notice.go
  4. 17 13
      wechat.go

+ 22 - 17
crontask/init.go

@@ -23,23 +23,28 @@ func NewCronTask(ctx context.Context, svcCtx *svc.ServiceContext) *CronTask {
 }
 
 func ScheduleRun(c *cron.Cron, serverCtx *svc.ServiceContext) {
-	l := NewCronTask(context.Background(), serverCtx)
-	c.AddFunc("* * * * *", func() {
-		l.sendMsg()
-	})
-
-	sendWx := NewCronTask(context.Background(), serverCtx)
-	c.AddFunc("* * * * *", func() {
-		sendWx.sendWx()
-	})
-
-	sendWxOnTimeout := NewCronTask(context.Background(), serverCtx)
-	c.AddFunc("* * * * *", func() {
-		sendWxOnTimeout.sendWxOnTimeout()
-	})
-
-	computeStatistic := NewCronTask(context.Background(), serverCtx)
+	//l := NewCronTask(context.Background(), serverCtx)
+	//c.AddFunc("* * * * *", func() {
+	//	l.sendMsg()
+	//})
+	//
+	//sendWx := NewCronTask(context.Background(), serverCtx)
+	//c.AddFunc("* * * * *", func() {
+	//	sendWx.sendWx()
+	//})
+	//
+	//sendWxOnTimeout := NewCronTask(context.Background(), serverCtx)
+	//c.AddFunc("* * * * *", func() {
+	//	sendWxOnTimeout.sendWxOnTimeout()
+	//})
+	//
+	//computeStatistic := NewCronTask(context.Background(), serverCtx)
+	//c.AddFunc("0 * * * *", func() {
+	//	computeStatistic.computeStatistic()
+	//})
+
+	syncWx := NewCronTask(context.Background(), serverCtx)
 	c.AddFunc("0 * * * *", func() {
-		computeStatistic.computeStatistic()
+		syncWx.syncWx()
 	})
 }

+ 104 - 0
crontask/sync_wx.go

@@ -0,0 +1,104 @@
+package crontask
+
+import (
+	"encoding/json"
+	"github.com/imroc/req/v3"
+	"strconv"
+	"time"
+	"wechat-api/ent"
+	"wechat-api/ent/wx"
+	"wechat-api/internal/types"
+)
+
+func (l *CronTask) syncWx() {
+	// 获取微信列表
+	var result types.WorkPhoneGetWeChatsResp
+	client := req.C().DevMode()
+	client.SetCommonRetryCount(2).
+		SetCommonRetryBackoffInterval(1*time.Second, 5*time.Second).
+		SetCommonRetryFixedInterval(2 * time.Second).SetTimeout(30 * time.Second)
+	res, err := client.R().SetSuccessResult(&result).Post("http://chat.gkscrm.com:13086/pc/GetWeChatsReq?id=13")
+	if err != nil {
+		l.Error("syncWx: ", err)
+		return
+	}
+	if !res.IsSuccessState() {
+		l.Error("GetWeChats failed with status code: ", res.StatusCode)
+		return
+	}
+
+	// 遍历微信列表
+	for _, account := range result.Data {
+		if account.Wechatid == "" {
+			continue
+		}
+		wxinfo, err := l.svcCtx.DB.Wx.Query().
+			Where(
+				wx.And(
+					wx.Or(
+						wx.WxidEQ(account.Wechatid),
+						wx.PortEQ(account.Deviceid),
+					),
+					wx.CtypeEQ(1),
+				),
+			).
+			Only(l.ctx)
+
+		if err != nil && !ent.IsNotFound(err) {
+			l.Error("syncWx: ", err)
+			return
+		}
+		var status uint8
+		if account.Isonline == 0 {
+			status = 1
+		} else {
+			status = 0
+		}
+		if wxinfo != nil {
+			err = l.svcCtx.DB.Wx.UpdateOneID(wxinfo.ID).
+				SetServerID(0).
+				SetPort(account.Deviceid).
+				SetProcessID(strconv.FormatInt(account.Cid, 10)).
+				SetAccount(account.Wechatno).
+				SetNickname(account.Wechatnick).
+				SetHeadBig(account.Avatar).
+				SetStatus(status).
+				Exec(l.ctx)
+
+			if err != nil {
+				l.Error("syncWx: ", err)
+				return
+			}
+		} else {
+			l.Debug("wxinfo is nil")
+			_, err := l.svcCtx.DB.Wx.Create().
+				SetServerID(0).
+				SetPort(account.Deviceid).
+				SetProcessID(strconv.FormatInt(account.Cid, 10)).
+				SetWxid(account.Wechatid).
+				SetAccount(account.Wechatno).
+				SetHeadBig(account.Avatar).
+				SetNickname(account.Wechatnick).
+				SetStatus(status).
+				SetAllowList([]string{}).SetBlockList([]string{}).SetGroupAllowList([]string{}).SetGroupBlockList([]string{}).
+				Save(l.ctx)
+			if err != nil {
+				l.Error("syncWx: ", err)
+				return
+			}
+		}
+
+		data := map[string]interface{}{
+			"MsgType": "TriggerFriendPushTask",
+			"Content": map[string]interface{}{
+				"WeChatId": account.Wechatid,
+			},
+		}
+		jsonStr, err := json.Marshal(data)
+		err = l.svcCtx.WechatWs["default"].SendMsg([]byte(jsonStr))
+		if err != nil {
+			l.Error("syncWx: ", err)
+			return
+		}
+	}
+}

+ 69 - 0
internal/service/MessageHandlers/friend_push_notice.go

@@ -0,0 +1,69 @@
+package MessageHandlers
+
+import (
+	"context"
+	"encoding/json"
+	"github.com/spf13/cast"
+	"wechat-api/ent/wx"
+	"wechat-api/hook"
+	"wechat-api/internal/pkg/wechat_ws"
+	"wechat-api/internal/svc"
+	"wechat-api/workphone"
+)
+
+type FriendPushNoticeHandler struct {
+	svcCtx *svc.ServiceContext
+}
+
+func NewFriendPushNoticeHandler(svcCtx *svc.ServiceContext) *FriendPushNoticeHandler {
+	return &FriendPushNoticeHandler{
+		svcCtx: svcCtx,
+	}
+}
+
+func (f *FriendPushNoticeHandler) Handler(msg *wechat_ws.MsgJsonObject) error {
+	if msg.MsgType == "FriendPushNotice" {
+		message := workphone.FriendPushNoticeMessage{}
+		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.Friends {
+			friendType := 1
+			if friend.Type == 1 {
+				friendType = 2
+				_ = hookClient.RequestChatRoomInfo(friend.FriendId, message.WeChatId)
+			} else {
+				friendType = 1
+			}
+
+			_, err = f.svcCtx.DB.Contact.Create().
+				SetWxWxid(message.WeChatId).
+				SetType(friendType).
+				SetWxid(friend.FriendId).
+				SetAccount(friend.FriendNo).
+				SetNickname(friend.FriendNick).
+				SetMarkname(friend.Memo).
+				SetHeadimg(friend.Avatar).
+				SetSex(cast.ToInt(friend.Gender)).
+				SetOrganizationID(wx_info.OrganizationID).
+				OnConflict().
+				UpdateNewValues().
+				SetOrganizationID(wx_info.OrganizationID).
+				ID(context.TODO())
+
+			if err != nil {
+				return err
+			}
+		}
+	}
+	return nil
+}

+ 17 - 13
wechat.go

@@ -25,9 +25,11 @@ package main
 import (
 	"flag"
 	"fmt"
+	"github.com/zeromicro/go-zero/core/logx"
 	"wechat-api/crontask"
 	"wechat-api/internal/config"
 	"wechat-api/internal/handler"
+	"wechat-api/internal/service/MessageHandlers"
 	"wechat-api/internal/svc"
 
 	"github.com/robfig/cron/v3"
@@ -47,22 +49,24 @@ func main() {
 	defer server.Stop()
 
 	ctx := svc.NewServiceContext(c)
+
 	handler.RegisterHandlers(server, ctx)
 
 	//个微处理程序 没有彻底完成之前不能开,和cow重复了
-	//if len(ctx.WechatWs) == 0 {
-	//	fmt.Println("wechat ws is nil")
-	//} else if ctx.WechatWs["default"] != nil {
-	//	var ic channel.IChannel
-	//	for _, ws := range ctx.WechatWs {
-	//		switch ws.CTypes {
-	//		case "wechat":
-	//			ic = channel.NewWechatChannel(ws, ctx)
-	//		}
-	//		ws.RegisterMessageHandler(ic.OnMessage)
-	//	}
-	//	logx.Info("注册个微处理通道~")
-	//}
+	if len(ctx.WechatWs) == 0 {
+		fmt.Println("wechat ws is nil")
+	} else if ctx.WechatWs["default"] != nil {
+		//var ic channel.IChannel
+		for _, ws := range ctx.WechatWs {
+			//switch ws.CTypes {
+			//case "wechat":
+			//	ic = channel.NewWechatChannel(ws, ctx)
+			//}
+			//ws.RegisterMessageHandler(ic.OnMessage)
+			ws.RegisterMessageHandler(MessageHandlers.NewFriendPushNoticeHandler(ctx).Handler)
+		}
+		logx.Info("注册个微处理通道~")
+	}
 	//for {
 	//	time.Sleep(time.Second * 10)
 	//}