123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package MessageHandlers
- import (
- "context"
- "encoding/json"
- "fmt"
- "github.com/zeromicro/go-zero/core/logx"
- "strconv"
- "time"
- "wechat-api/ent/addwechatfriendlog"
- "wechat-api/internal/pkg/wechat_ws"
- "wechat-api/internal/svc"
- )
- type TaskResultNoticeHandler struct {
- svcCtx *svc.ServiceContext
- }
- func NewTaskResultNoticeHandler(svcCtx *svc.ServiceContext) *TaskResultNoticeHandler {
- return &TaskResultNoticeHandler{
- svcCtx: svcCtx,
- }
- }
- // Handle 实现 MessageHandlerStrategy 接口
- func (f *TaskResultNoticeHandler) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error {
- logx.Infof("收到任务回执消息: %s", msg.Message)
- // 统一解析兼容不同结构体格式
- common, err := ParseTaskResultMessage(msg.Message)
- if err != nil {
- logx.Errorf("解析任务消息失败: %v", err)
- return err
- }
- taskIdInt, err := common.GetTaskIdInt64()
- if err != nil {
- logx.Errorf("TaskId 非法,无法转换为数字: %v", err)
- return err
- }
- 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(common.WeChatId)).
- Where(addwechatfriendlog.TaskIDEQ(taskIdInt)).
- SetAddResult(m).
- SetUpdatedAt(time.Now().Unix())
- if common.ErrMsg == "该用户不存在" {
- update.SetIsCanAdd(0) // 重置添加为不能添加好友
- }
- if common.Success {
- update.SetIsCanAdd(2) // 表示任务成功
- }
- if _, err := update.Save(ctx); err != nil {
- 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 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)
- }
|