Преглед изворни кода

联系人通过好友添加

lichangdong пре 1 дан
родитељ
комит
d677d1676c

+ 2 - 2
internal/pkg/wechat_ws/wechat_ws_client.go

@@ -300,8 +300,8 @@ func (c *WechatWsClient) DeviceAuth() error {
 		"AccessToken": "",
 		"MsgType":     "DeviceAuthReq",
 		"Content": map[string]interface{}{
-			"AuthType":   3,
-			"Credential": "",
+			"AuthType":   2,
+			"Credential": "bGNka2Y6clFSd0NTT21wbFgzVHRMSg==",
 		},
 	}
 	transportMessageJSON, err := json.Marshal(message)

+ 50 - 2
internal/service/MessageHandlers/find_contact_task_result_notice.go

@@ -9,7 +9,6 @@ import (
 	"wechat-api/ent/addwechatfriendlog"
 	"wechat-api/internal/pkg/wechat_ws"
 	"wechat-api/internal/svc"
-	"wechat-api/workphone"
 )
 
 type FindContactTaskResultNoticeHandler struct {
@@ -22,8 +21,57 @@ func NewFindContactTaskResultNoticeHandler(svcCtx *svc.ServiceContext) *FindCont
 	}
 }
 
+type EnumGender int
+
+const (
+	UnknownGender EnumGender = 0
+	Male          EnumGender = 1
+	Female        EnumGender = 2
+)
+
+// 自定义 JSON 解析,支持字符串 -> 枚举
+func (e *EnumGender) UnmarshalJSON(b []byte) error {
+	var s string
+	if err := json.Unmarshal(b, &s); err == nil {
+		switch s {
+		case "Male":
+			*e = Male
+		case "Female":
+			*e = Female
+		default:
+			*e = UnknownGender
+		}
+		return nil
+	}
+
+	var i int
+	if err := json.Unmarshal(b, &i); err == nil {
+		*e = EnumGender(i)
+		return nil
+	}
+	return errors.New("invalid gender value")
+}
+
+// 用于反序列化的消息结构体(扩展 Gender 类型)
+type FindContactTaskResultNoticeMessage struct {
+	WeChatId   string      `json:"WeChatId"`
+	SearchText string      `json:"SearchText"`
+	Success    bool        `json:"Success"`
+	IsFriend   bool        `json:"IsFriend"`
+	UserName   *string     `json:"UserName"`
+	Alias      *string     `json:"Alias"`
+	NickName   *string     `json:"NickName"`
+	Gender     *EnumGender `json:"Gender"` // 也是指针
+	Country    *string     `json:"Country"`
+	Province   *string     `json:"Province"`
+	City       *string     `json:"City"`
+	Avatar     *string     `json:"Avatar"`
+	ErrMsg     *string     `json:"ErrMsg"`
+}
+
 func (f *FindContactTaskResultNoticeHandler) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error {
-	var message workphone.FindContactTaskResultNoticeMessage
+	var message FindContactTaskResultNoticeMessage
+
 	logx.Infof("msg.Message 的内容是:%s", msg.Message)
 	if err := json.Unmarshal([]byte(msg.Message), &message); err != nil {
 		return err

+ 3 - 3
internal/service/MessageHandlers/register_strategy.go

@@ -42,9 +42,9 @@ func (h *MessageHandler) RegisterStrategies() {
 	strategyMap := map[string]func(*svc.ServiceContext) MessageHandlerStrategy{
 		"FriendPushNotice": toStrategy(NewFriendPushNoticeTypeHandler),
 		//"ChatroomPushNotice":     toStrategy(NewChatroomPushNoticeTypeHandler),
-		"ContactLabelInfoNotice":      toStrategy(NewContactLabelInfoNotice),
-		"FindContactTaskResultNotice": toStrategy(NewFindContactTaskResultNoticeHandler),
-		"TaskResultNotice":            toStrategy(NewTaskResultNoticeHandler),
+		"ContactLabelInfoNotice": toStrategy(NewContactLabelInfoNotice),
+		"FindContactTaskResult":  toStrategy(NewFindContactTaskResultNoticeHandler),
+		"TaskResultNotice":       toStrategy(NewTaskResultNoticeHandler),
 	}
 	for msgType, strategyFunc := range strategyMap {
 		h.RegisterStrategy(msgType, strategyFunc(h.svcCtx))

+ 29 - 10
internal/service/MessageHandlers/task_result_notice.go

@@ -4,11 +4,11 @@ import (
 	"context"
 	"encoding/json"
 	"github.com/zeromicro/go-zero/core/logx"
+	"strconv"
 	"time"
 	"wechat-api/ent/addwechatfriendlog"
 	"wechat-api/internal/pkg/wechat_ws"
 	"wechat-api/internal/svc"
-	"wechat-api/workphone"
 )
 
 type TaskResultNoticeHandler struct {
@@ -23,14 +23,24 @@ func NewTaskResultNoticeHandler(svcCtx *svc.ServiceContext) *TaskResultNoticeHan
 
 // Handle 实现 MessageHandlerStrategy 接口
 func (f *TaskResultNoticeHandler) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error {
-	message := workphone.TaskResultNoticeMessage{}
-	err := json.Unmarshal([]byte(msg.Message), &message)
-	logx.Infof("msg.Message 的内容是:%s", msg.Message)
-	if err != nil {
-		logx.Errorf("Unmarshal.fail")
+
+	var messageTask TaskResultNoticeMessage
+	if err := json.Unmarshal([]byte(msg.Message), &messageTask); err != nil {
+		logx.Errorf("预处理 JSON 失败: %v", err)
 		return err
 	}
-	if message.TaskType != workphone.EnumMsgType_AddFriendsTask {
+	// 如果 TaskId 是字符串,就转换成数字
+	var taskIdInt int64
+	if messageTask.TaskId != nil {
+		var err error
+		taskIdInt, err = strconv.ParseInt(*messageTask.TaskId, 10, 64)
+		if err != nil {
+			logx.Errorf("Unmarshal.fail")
+			return err
+		}
+	}
+
+	if messageTask.TaskType != "AddFriendsTask" {
 		logx.Infof("不是添加好友任务的回执")
 		return nil
 	}
@@ -40,12 +50,12 @@ func (f *TaskResultNoticeHandler) Handle(ctx context.Context, msg *wechat_ws.Msg
 		return err
 	}
 	update := svcCtx.DB.AddWechatFriendLog.Update().
-		Where(addwechatfriendlog.OwnerWxIDEQ(message.WeChatId)).
-		Where(addwechatfriendlog.TaskIDEQ(message.TaskId)).
+		Where(addwechatfriendlog.OwnerWxIDEQ(*messageTask.WeChatId)).
+		Where(addwechatfriendlog.TaskIDEQ(taskIdInt)).
 		//SetFindResult(m).
 		SetAddResult(m).
 		SetUpdatedAt(time.Now().Unix())
-	if message.Success {
+	if *messageTask.Success {
 		update.SetIsCanAdd(2)
 	}
 	if _, err := update.Save(ctx); err != nil {
@@ -54,3 +64,12 @@ func (f *TaskResultNoticeHandler) Handle(ctx context.Context, msg *wechat_ws.Msg
 	}
 	return nil
 }
+
+type TaskResultNoticeMessage struct {
+	Success  *bool   `json:"Success,omitempty"`  // 指针,便于判断字段是否存在
+	Code     *int32  `json:"Code,omitempty"`     // 错误码
+	ErrMsg   *string `json:"ErrMsg,omitempty"`   // 错误描述
+	TaskId   *string `json:"TaskId,omitempty"`   // 任务ID(因前面问题,保持 string 类型)
+	TaskType string  `json:"TaskType,omitempty"` // 枚举类型
+	WeChatId *string `json:"WeChatId,omitempty"` // 微信号
+}

+ 3 - 2
internal/service/addfriend/add_wechat_friend_log.go

@@ -42,7 +42,7 @@ func (l *AddWechatFriendService) FindFriendByContent(wechatId, content string) b
 	update := l.svcCtx.DB.AddWechatFriendLog.Update().
 		Where(addwechatfriendlog.OwnerWxIDEQ(wechatId)).
 		Where(addwechatfriendlog.FindContentEQ(content)).
-		SetFindResult(result).
+		SetFindRequest(result).
 		SetUpdatedAt(time.Now().Unix())
 	_, err = update.Save(l.ctx)
 	if err != nil || result["sendResult"] != "success" {
@@ -75,7 +75,8 @@ func (l *AddWechatFriendService) AddNewFriend(wechatId, content, message string)
 	update := l.svcCtx.DB.AddWechatFriendLog.Update().
 		Where(addwechatfriendlog.OwnerWxIDEQ(wechatId)).
 		Where(addwechatfriendlog.FindContentEQ(content)).
-		SetAddResult(result).
+		SetAddRequest(result).
+		SetTaskID(taskId.Int64()).
 		AddTaskCount(1).
 		SetUpdatedAt(time.Now().Unix())
 	_, err = update.Save(l.ctx)

+ 11 - 0
internal/types/workphone.go

@@ -29,3 +29,14 @@ type WorkPhoneGetWeChatsResp struct {
 	Data []WorkPhoneWeChat `json:"data"`
 	Msg  string            `json:"msg"`
 }
+type FindContactTaskResultNoticeMessageWrapper struct {
+	WeChatId   string `json:"WeChatId"`
+	SearchText string `json:"SearchText"`
+	Success    bool   `json:"Success"`
+	UserName   string `json:"UserName"`
+	NickName   string `json:"NickName"`
+	Gender     string `json:"Gender"`
+	Province   string `json:"Province"`
+	City       string `json:"City"`
+	Avatar     string `json:"Avatar"`
+}