|
@@ -3,6 +3,7 @@ package MessageHandlers
|
|
|
import (
|
|
|
"context"
|
|
|
"encoding/json"
|
|
|
+ "fmt"
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
"strconv"
|
|
|
"time"
|
|
@@ -23,54 +24,136 @@ func NewTaskResultNoticeHandler(svcCtx *svc.ServiceContext) *TaskResultNoticeHan
|
|
|
|
|
|
// Handle 实现 MessageHandlerStrategy 接口
|
|
|
func (f *TaskResultNoticeHandler) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error {
|
|
|
+ logx.Infof("收到任务回执消息: %s", msg.Message)
|
|
|
|
|
|
- var messageTask TaskResultNoticeMessage
|
|
|
- logx.Infof("TaskResultNoticeMessage的内容是:%s", msg.Message)
|
|
|
- if err := json.Unmarshal([]byte(msg.Message), &messageTask); err != nil {
|
|
|
- logx.Errorf("预处理 JSON 失败: %v", err)
|
|
|
+ // 统一解析兼容不同结构体格式
|
|
|
+ common, err := ParseTaskResultMessage(msg.Message)
|
|
|
+ if err != nil {
|
|
|
+ logx.Errorf("解析任务消息失败: %v", err)
|
|
|
return err
|
|
|
}
|
|
|
- // 如果 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
|
|
|
- }
|
|
|
+
|
|
|
+ taskIdInt, err := common.GetTaskIdInt64()
|
|
|
+ if err != nil {
|
|
|
+ logx.Errorf("TaskId 非法,无法转换为数字: %v", err)
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
- if messageTask.TaskType != "AddFriendsTask" && messageTask.TaskType != "AddCustomerFromSearchTask" {
|
|
|
- logx.Infof("不是添加好友任务的回执")
|
|
|
+ logx.Infof("处理任务:TaskId=%d,WeChatId=%s,TaskType=%s,Success=%v", taskIdInt, common.WeChatId, common.TaskType, common.Success)
|
|
|
+
|
|
|
+ // 只处理添加好友相关任务
|
|
|
+ if common.TaskType != "AddFriendsTask" && common.TaskType != "AddCustomerFromSearchTask" {
|
|
|
+ logx.Infof("忽略非添加好友任务类型: %s", common.TaskType)
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+ // 将消息解析成通用 map 以用于存储结果
|
|
|
m, err := ParseJSONStringToMap(msg.Message)
|
|
|
if err != nil {
|
|
|
logx.Errorf("解析 JSON 失败: %v", err)
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
+ // 更新数据库记录
|
|
|
update := svcCtx.DB.AddWechatFriendLog.Update().
|
|
|
- Where(addwechatfriendlog.OwnerWxIDEQ(*messageTask.WeChatId)).
|
|
|
+ Where(addwechatfriendlog.OwnerWxIDEQ(common.WeChatId)).
|
|
|
Where(addwechatfriendlog.TaskIDEQ(taskIdInt)).
|
|
|
- //SetFindResult(m).
|
|
|
SetAddResult(m).
|
|
|
SetUpdatedAt(time.Now().Unix())
|
|
|
- if messageTask.Success {
|
|
|
- update.SetIsCanAdd(2)
|
|
|
+
|
|
|
+ if common.ErrMsg == "该用户不存在" {
|
|
|
+ update.SetIsCanAdd(0) // 重置添加为不能添加好友
|
|
|
}
|
|
|
+
|
|
|
+ if common.Success {
|
|
|
+ update.SetIsCanAdd(2) // 表示任务成功
|
|
|
+ }
|
|
|
+
|
|
|
if _, err := update.Save(ctx); err != nil {
|
|
|
- logx.Errorf("更新AddWechatFriendLog-field-add-result失败: %v", err)
|
|
|
+ logx.Errorf("更新 AddWechatFriendLog 表失败: %v", err)
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-type TaskResultNoticeMessage struct {
|
|
|
- Success bool `json:"Success,omitempty"` // 指针,便于判断字段是否存在
|
|
|
- Code *string `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"` // 微信号
|
|
|
+//type TaskResultNoticeMessage struct {
|
|
|
+// Success bool `json:"Success,omitempty"` // 指针,便于判断字段是否存在
|
|
|
+// Code *string `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"` // 微信号
|
|
|
+//}
|
|
|
+//
|
|
|
+//type WecomTaskResultNoticeMessage struct {
|
|
|
+// Success bool `json:"Success,omitempty"` // 指针,便于判断字段是否存在
|
|
|
+// Code *string `json:"Code,omitempty"` // 错误码
|
|
|
+// ErrMsg *string `json:"ErrMsg,omitempty"` // 错误描述
|
|
|
+// TaskId *string `json:"TaskId,omitempty"` // 任务ID(因前面问题,保持 string 类型)
|
|
|
+// TaskType string `json:"TaskType,omitempty"` // 枚举类型
|
|
|
+// WeChatId *string `json:"WxId,omitempty"` // 微信号
|
|
|
+//}
|
|
|
+
|
|
|
+type TaskResultMessageCommon struct {
|
|
|
+ Success bool
|
|
|
+ Code string
|
|
|
+ ErrMsg string
|
|
|
+ TaskId string
|
|
|
+ TaskType string
|
|
|
+ WeChatId string
|
|
|
+}
|
|
|
+
|
|
|
+func ParseTaskResultMessage(jsonStr string) (*TaskResultMessageCommon, error) {
|
|
|
+ var data map[string]interface{}
|
|
|
+ if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
|
|
|
+ return nil, fmt.Errorf("JSON 解析失败: %w", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ var result TaskResultMessageCommon
|
|
|
+
|
|
|
+ // Success
|
|
|
+ if val, ok := data["Success"].(bool); ok {
|
|
|
+ result.Success = val
|
|
|
+ }
|
|
|
+
|
|
|
+ // Code
|
|
|
+ if val, ok := data["Code"]; ok {
|
|
|
+ result.Code = fmt.Sprintf("%v", val)
|
|
|
+ }
|
|
|
+
|
|
|
+ // ErrMsg
|
|
|
+ if val, ok := data["ErrMsg"]; ok {
|
|
|
+ result.ErrMsg = fmt.Sprintf("%v", val)
|
|
|
+ }
|
|
|
+
|
|
|
+ // TaskType
|
|
|
+ if val, ok := data["TaskType"]; ok {
|
|
|
+ result.TaskType = fmt.Sprintf("%v", val)
|
|
|
+ }
|
|
|
+
|
|
|
+ // TaskId(统一转成 string)
|
|
|
+ if val, ok := data["TaskId"]; ok {
|
|
|
+ switch v := val.(type) {
|
|
|
+ case string:
|
|
|
+ result.TaskId = v
|
|
|
+ case float64:
|
|
|
+ result.TaskId = strconv.FormatInt(int64(v), 10)
|
|
|
+ default:
|
|
|
+ result.TaskId = fmt.Sprintf("%v", v)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // WeChatId 或 WxId(统一成 string)
|
|
|
+ if val, ok := data["WeChatId"]; ok {
|
|
|
+ result.WeChatId = fmt.Sprintf("%v", val)
|
|
|
+ } else if val, ok := data["WxId"]; ok {
|
|
|
+ result.WeChatId = fmt.Sprintf("%v", val)
|
|
|
+ }
|
|
|
+
|
|
|
+ return &result, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (t *TaskResultMessageCommon) GetTaskIdInt64() (int64, error) {
|
|
|
+ return strconv.ParseInt(t.TaskId, 10, 64)
|
|
|
}
|