Ver código fonte

提供查找好友+好友的服务方法

lichangdong 1 semana atrás
pai
commit
a2c9433287
2 arquivos alterados com 103 adições e 9 exclusões
  1. 17 9
      hook/contact.go
  2. 86 0
      internal/service/addfriend/add_wechat_friend_log.go

+ 17 - 9
hook/contact.go

@@ -265,11 +265,11 @@ type MessagePayload[T any] struct {
 }
 
 // SendMessage 函数用于发送消息
-func SendMessage[T any](hook *Hook, msgType workphone.EnumMsgType, msgTypeStr string, content T) (MessagePayload[T], error) {
+func SendMessage[T any](hook *Hook, msgType workphone.EnumMsgType, msgTypeStr string, content T) (map[string]interface{}, error) {
 	conn, err := hook.connWorkPhone()
 	if err != nil {
 		logx.Errorf("WebSocket 连接失败: %v", err)
-		return MessagePayload[T]{}, err
+		return MessagePayload[T]{}.ToMap(), err
 	}
 	defer func() {
 		if cerr := conn.Close(); cerr != nil {
@@ -285,13 +285,13 @@ func SendMessage[T any](hook *Hook, msgType workphone.EnumMsgType, msgTypeStr st
 
 	data, err := json.Marshal(message)
 	if err != nil {
-		return message, fmt.Errorf("JSON 序列化失败: %w", err)
+		return message.ToMap(), fmt.Errorf("JSON 序列化失败: %w", err)
 	}
 
 	if err := conn.WriteMessage(websocket.TextMessage, data); err != nil {
-		return message, fmt.Errorf("发送消息失败: %w", err)
+		return message.ToMap(), fmt.Errorf("发送消息失败: %w", err)
 	}
-	return message, nil
+	return message.ToMap(), nil
 }
 
 // AddFriendTask 函数用于创建并发送一个添加好友的任务
@@ -306,9 +306,9 @@ func SendMessage[T any](hook *Hook, msgType workphone.EnumMsgType, msgTypeStr st
 //
 //	成功时返回包含添加好友任务信息的消息负载和nil错误
 //	失败时返回空的消息负载和相应的错误
-func (h *Hook) AddFriendTask(ownerWxId, phone, msg string, taskId int64) (MessagePayload[AddFriendsTask], error) {
+func (h *Hook) AddFriendTask(ownerWxId, phone, msg string, taskId int64) (map[string]interface{}, error) {
 	if ownerWxId == "" || phone == "" || msg == "" {
-		return MessagePayload[AddFriendsTask]{}, fmt.Errorf("invalid input parameters")
+		return MessagePayload[AddFriendsTask]{}.ToMap(), fmt.Errorf("invalid input parameters")
 	}
 	content := AddFriendsTask{
 		WeChatId: ownerWxId,
@@ -330,9 +330,9 @@ func (h *Hook) AddFriendTask(ownerWxId, phone, msg string, taskId int64) (Messag
 //
 //	成功时返回包含查找联系人任务信息的消息负载和nil错误
 //	失败时返回空的消息负载和相应的错误
-func (h *Hook) FindContactByContent(ownerWxId, contentMessage string) (MessagePayload[FindContactTask], error) {
+func (h *Hook) FindContactByContent(ownerWxId, contentMessage string) (map[string]interface{}, error) {
 	if ownerWxId == "" || contentMessage == "" {
-		return MessagePayload[FindContactTask]{}, fmt.Errorf("invalid input parameters")
+		return MessagePayload[FindContactTask]{}.ToMap(), fmt.Errorf("invalid input parameters")
 	}
 	content := FindContactTask{
 		WeChatId: ownerWxId,
@@ -340,3 +340,11 @@ func (h *Hook) FindContactByContent(ownerWxId, contentMessage string) (MessagePa
 	}
 	return SendMessage(h, workphone.EnumMsgType_FindContactTask, "FindContactTask", content)
 }
+
+func (m MessagePayload[T]) ToMap() map[string]interface{} {
+	return map[string]interface{}{
+		"Id":      m.Id,
+		"MsgType": m.MsgType,
+		"Content": m.Content,
+	}
+}

+ 86 - 0
internal/service/addfriend/add_wechat_friend_log.go

@@ -0,0 +1,86 @@
+package addfriend
+
+import (
+	"context"
+	"github.com/bwmarrin/snowflake"
+	"github.com/zeromicro/go-zero/core/logx"
+	"log"
+	"time"
+	"wechat-api/ent/addwechatfriendlog"
+	"wechat-api/hook"
+	"wechat-api/internal/svc"
+)
+
+type AddWechatFriendService struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewAddWechatFriendService(ctx context.Context, svcCtx *svc.ServiceContext) *AddWechatFriendService {
+	return &AddWechatFriendService{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+// FindFriendByContent 查找好友的服务
+// 参数  wechatId   微信ID
+// content  手机号 或者其他
+
+func (l *AddWechatFriendService) FindFriendByContent(wechatId, content string) bool {
+	//先记录数据 --- 避免 发送出请求,立马返回 导致 数据 还没入库
+	hookClient := hook.NewHook("", "", "")
+	result, err := hookClient.FindContactByContent(wechatId, content)
+
+	if err != nil {
+		result["sendResult"] = "success"
+	} else {
+		result["sendResult"] = err
+	}
+	//发送第三方查找
+	update := l.svcCtx.DB.AddWechatFriendLog.Update().
+		Where(addwechatfriendlog.OwnerWxIDEQ(wechatId)).
+		Where(addwechatfriendlog.FindContentEQ(content)).
+		SetFindResult(result).
+		SetUpdatedAt(time.Now().Unix())
+	_, err = update.Save(l.ctx)
+	if err != nil || result["sendResult"] != "success" {
+		logx.Errorf("更新 AddWechatFriendLog 失败 或者: %v", err)
+		return false
+	}
+	return true
+}
+
+// AddNewFriend 添加好友的服务
+// 参数  wechatId   微信ID
+// content  手机号 或者其他
+// message  备注
+
+func (l *AddWechatFriendService) AddNewFriend(wechatId, content, message string) bool {
+	//先记录请求
+	hookClient := hook.NewHook("", "", "")
+	node, err := snowflake.NewNode(1) // 1 是节点 ID,根据需要设置
+	if err != nil {
+		log.Fatal(err)
+	}
+	taskId := node.Generate()
+	result, err := hookClient.AddFriendTask(wechatId, content, message, taskId.Int64())
+
+	if err != nil {
+		result["sendResult"] = "success"
+	} else {
+		result["sendResult"] = err
+	}
+	update := l.svcCtx.DB.AddWechatFriendLog.Update().
+		Where(addwechatfriendlog.OwnerWxIDEQ(wechatId)).
+		Where(addwechatfriendlog.FindContentEQ(content)).
+		SetAddResult(result).
+		SetUpdatedAt(time.Now().Unix())
+	_, err = update.Save(l.ctx)
+	if err != nil || result["sendResult"] != "success" {
+		logx.Errorf("更新 AddWechatFriendLog 失败 或者: %v", err)
+		return false
+	}
+	return true //获取好友列表
+}