task_result_notice.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package MessageHandlers
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "strconv"
  8. "time"
  9. "wechat-api/ent/addwechatfriendlog"
  10. "wechat-api/internal/pkg/wechat_ws"
  11. "wechat-api/internal/svc"
  12. )
  13. type TaskResultNoticeHandler struct {
  14. svcCtx *svc.ServiceContext
  15. }
  16. func NewTaskResultNoticeHandler(svcCtx *svc.ServiceContext) *TaskResultNoticeHandler {
  17. return &TaskResultNoticeHandler{
  18. svcCtx: svcCtx,
  19. }
  20. }
  21. // Handle 实现 MessageHandlerStrategy 接口
  22. func (f *TaskResultNoticeHandler) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error {
  23. logx.Infof("收到任务回执消息: %s", msg.Message)
  24. // 统一解析兼容不同结构体格式
  25. common, err := ParseTaskResultMessage(msg.Message)
  26. if err != nil {
  27. logx.Errorf("解析任务消息失败: %v", err)
  28. return err
  29. }
  30. taskIdInt, err := common.GetTaskIdInt64()
  31. if err != nil {
  32. logx.Errorf("TaskId 非法,无法转换为数字: %v", err)
  33. return err
  34. }
  35. logx.Infof("处理任务:TaskId=%d,WeChatId=%s,TaskType=%s,Success=%v", taskIdInt, common.WeChatId, common.TaskType, common.Success)
  36. // 只处理添加好友相关任务
  37. if common.TaskType != "AddFriendsTask" && common.TaskType != "AddCustomerFromSearchTask" {
  38. logx.Infof("忽略非添加好友任务类型: %s", common.TaskType)
  39. return nil
  40. }
  41. // 将消息解析成通用 map 以用于存储结果
  42. m, err := ParseJSONStringToMap(msg.Message)
  43. if err != nil {
  44. logx.Errorf("解析 JSON 失败: %v", err)
  45. return err
  46. }
  47. // 更新数据库记录
  48. update := svcCtx.DB.AddWechatFriendLog.Update().
  49. Where(addwechatfriendlog.OwnerWxIDEQ(common.WeChatId)).
  50. Where(addwechatfriendlog.TaskIDEQ(taskIdInt)).
  51. SetAddResult(m).
  52. SetUpdatedAt(time.Now().Unix())
  53. if common.ErrMsg == "该用户不存在" {
  54. update.SetIsCanAdd(0) // 重置添加为不能添加好友
  55. }
  56. if common.Success {
  57. update.SetIsCanAdd(2) // 表示任务成功
  58. }
  59. if _, err := update.Save(ctx); err != nil {
  60. logx.Errorf("更新 AddWechatFriendLog 表失败: %v", err)
  61. return err
  62. }
  63. return nil
  64. }
  65. //type TaskResultNoticeMessage struct {
  66. // Success bool `json:"Success,omitempty"` // 指针,便于判断字段是否存在
  67. // Code *string `json:"Code,omitempty"` // 错误码
  68. // ErrMsg *string `json:"ErrMsg,omitempty"` // 错误描述
  69. // TaskId *string `json:"TaskId,omitempty"` // 任务ID(因前面问题,保持 string 类型)
  70. // TaskType string `json:"TaskType,omitempty"` // 枚举类型
  71. // WeChatId *string `json:"WeChatId,omitempty"` // 微信号
  72. //}
  73. //
  74. //type WecomTaskResultNoticeMessage struct {
  75. // Success bool `json:"Success,omitempty"` // 指针,便于判断字段是否存在
  76. // Code *string `json:"Code,omitempty"` // 错误码
  77. // ErrMsg *string `json:"ErrMsg,omitempty"` // 错误描述
  78. // TaskId *string `json:"TaskId,omitempty"` // 任务ID(因前面问题,保持 string 类型)
  79. // TaskType string `json:"TaskType,omitempty"` // 枚举类型
  80. // WeChatId *string `json:"WxId,omitempty"` // 微信号
  81. //}
  82. type TaskResultMessageCommon struct {
  83. Success bool
  84. Code string
  85. ErrMsg string
  86. TaskId string
  87. TaskType string
  88. WeChatId string
  89. }
  90. func ParseTaskResultMessage(jsonStr string) (*TaskResultMessageCommon, error) {
  91. var data map[string]interface{}
  92. if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
  93. return nil, fmt.Errorf("JSON 解析失败: %w", err)
  94. }
  95. var result TaskResultMessageCommon
  96. // Success
  97. if val, ok := data["Success"].(bool); ok {
  98. result.Success = val
  99. }
  100. // Code
  101. if val, ok := data["Code"]; ok {
  102. result.Code = fmt.Sprintf("%v", val)
  103. }
  104. // ErrMsg
  105. if val, ok := data["ErrMsg"]; ok {
  106. result.ErrMsg = fmt.Sprintf("%v", val)
  107. }
  108. // TaskType
  109. if val, ok := data["TaskType"]; ok {
  110. result.TaskType = fmt.Sprintf("%v", val)
  111. }
  112. // TaskId(统一转成 string)
  113. if val, ok := data["TaskId"]; ok {
  114. switch v := val.(type) {
  115. case string:
  116. result.TaskId = v
  117. case float64:
  118. result.TaskId = strconv.FormatInt(int64(v), 10)
  119. default:
  120. result.TaskId = fmt.Sprintf("%v", v)
  121. }
  122. }
  123. // WeChatId 或 WxId(统一成 string)
  124. if val, ok := data["WeChatId"]; ok {
  125. result.WeChatId = fmt.Sprintf("%v", val)
  126. } else if val, ok := data["WxId"]; ok {
  127. result.WeChatId = fmt.Sprintf("%v", val)
  128. }
  129. return &result, nil
  130. }
  131. func (t *TaskResultMessageCommon) GetTaskIdInt64() (int64, error) {
  132. return strconv.ParseInt(t.TaskId, 10, 64)
  133. }