12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package MessageHandlers
- import (
- "context"
- "encoding/json"
- "errors"
- "github.com/zeromicro/go-zero/core/logx"
- "time"
- "wechat-api/ent/addwechatfriendlog"
- "wechat-api/internal/pkg/wechat_ws"
- "wechat-api/internal/svc"
- "wechat-api/workphone"
- )
- type FindContactTaskResultNoticeHandler struct {
- svcCtx *svc.ServiceContext
- }
- func NewFindContactTaskResultNoticeHandler(svcCtx *svc.ServiceContext) *FindContactTaskResultNoticeHandler {
- return &FindContactTaskResultNoticeHandler{
- svcCtx: svcCtx,
- }
- }
- func (f *FindContactTaskResultNoticeHandler) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error {
- var message workphone.FindContactTaskResultNoticeMessage
- logx.Infof("msg.Message 的内容是:%s", msg.Message)
- if err := json.Unmarshal([]byte(msg.Message), &message); err != nil {
- return err
- }
- //logx.Infof("msg.Message 的内容是:%s", message)
- //var isCanAdd int
- m, err := ParseJSONStringToMap(msg.Message)
- if err != nil {
- logx.Errorf("解析 JSON 失败: %v", err)
- return err
- }
- update := svcCtx.DB.AddWechatFriendLog.Update().
- Where(addwechatfriendlog.OwnerWxIDEQ(message.WeChatId)).
- Where(addwechatfriendlog.FindContentEQ(message.SearchText)).
- SetFindResult(m).
- SetUpdatedAt(time.Now().Unix())
- if message.Success {
- if message.IsFriend {
- update = update.SetIsCanAdd(0)
- } else {
- update = update.SetIsCanAdd(1)
- }
- }
- if _, err := update.Save(ctx); err != nil {
- logx.Errorf("更新 AddWechatFriendLog 失败: %v", err)
- return err
- }
- return nil
- }
- func ParseJSONStringToMap(jsonStr string) (map[string]interface{}, error) {
- if !json.Valid([]byte(jsonStr)) {
- return nil, errors.New("非法 JSON 字符串")
- }
- var result map[string]interface{}
- err := json.Unmarshal([]byte(jsonStr), &result)
- return result, err
- }
|