task_result_notice.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.Code == "InternalError" {
  54. if common.ErrMsg == "timeout" {
  55. update.SetIsCanAdd(3) // 重置添加为不能添加好友
  56. } else if common.ErrMsg == "用户不存在" {
  57. update.SetIsCanAdd(4) // 重置添加为不能添加好友
  58. } else {
  59. update.SetIsCanAdd(3)
  60. } // 重置添加为不能添加好友
  61. }
  62. if common.Success {
  63. update.SetIsCanAdd(2) // 表示任务成功
  64. }
  65. if _, err := update.Save(ctx); err != nil {
  66. logx.Errorf("更新 AddWechatFriendLog 表失败: %v", err)
  67. return err
  68. }
  69. return nil
  70. }
  71. //type TaskResultNoticeMessage struct {
  72. // Success bool `json:"Success,omitempty"` // 指针,便于判断字段是否存在
  73. // Code *string `json:"Code,omitempty"` // 错误码
  74. // ErrMsg *string `json:"ErrMsg,omitempty"` // 错误描述
  75. // TaskId *string `json:"TaskId,omitempty"` // 任务ID(因前面问题,保持 string 类型)
  76. // TaskType string `json:"TaskType,omitempty"` // 枚举类型
  77. // WeChatId *string `json:"WeChatId,omitempty"` // 微信号
  78. //}
  79. //
  80. //type WecomTaskResultNoticeMessage struct {
  81. // Success bool `json:"Success,omitempty"` // 指针,便于判断字段是否存在
  82. // Code *string `json:"Code,omitempty"` // 错误码
  83. // ErrMsg *string `json:"ErrMsg,omitempty"` // 错误描述
  84. // TaskId *string `json:"TaskId,omitempty"` // 任务ID(因前面问题,保持 string 类型)
  85. // TaskType string `json:"TaskType,omitempty"` // 枚举类型
  86. // WeChatId *string `json:"WxId,omitempty"` // 微信号
  87. //}
  88. type TaskResultMessageCommon struct {
  89. Success bool
  90. Code string
  91. ErrMsg string
  92. TaskId string
  93. TaskType string
  94. WeChatId string
  95. }
  96. func ParseTaskResultMessage(jsonStr string) (*TaskResultMessageCommon, error) {
  97. var data map[string]interface{}
  98. if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
  99. return nil, fmt.Errorf("JSON 解析失败: %w", err)
  100. }
  101. var result TaskResultMessageCommon
  102. // Success
  103. if val, ok := data["Success"].(bool); ok {
  104. result.Success = val
  105. }
  106. // Code
  107. if val, ok := data["Code"]; ok {
  108. result.Code = fmt.Sprintf("%v", val)
  109. }
  110. // ErrMsg
  111. if val, ok := data["ErrMsg"]; ok {
  112. result.ErrMsg = fmt.Sprintf("%v", val)
  113. }
  114. // TaskType
  115. if val, ok := data["TaskType"]; ok {
  116. result.TaskType = fmt.Sprintf("%v", val)
  117. }
  118. // TaskId(统一转成 string)
  119. if val, ok := data["TaskId"]; ok {
  120. switch v := val.(type) {
  121. case string:
  122. result.TaskId = v
  123. case float64:
  124. result.TaskId = strconv.FormatInt(int64(v), 10)
  125. default:
  126. result.TaskId = fmt.Sprintf("%v", v)
  127. }
  128. }
  129. // WeChatId 或 WxId(统一成 string)
  130. if val, ok := data["WeChatId"]; ok {
  131. result.WeChatId = fmt.Sprintf("%v", val)
  132. } else if val, ok := data["WxId"]; ok {
  133. result.WeChatId = fmt.Sprintf("%v", val)
  134. }
  135. return &result, nil
  136. }
  137. func (t *TaskResultMessageCommon) GetTaskIdInt64() (int64, error) {
  138. return strconv.ParseInt(t.TaskId, 10, 64)
  139. }