send_msg_logic.go 969 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package wp_wecom
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/json"
  6. "wechat-api/internal/svc"
  7. "wechat-api/internal/types"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. )
  10. type SendMsgLogic struct {
  11. logx.Logger
  12. ctx context.Context
  13. svcCtx *svc.ServiceContext
  14. }
  15. func NewSendMsgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendMsgLogic {
  16. return &SendMsgLogic{
  17. Logger: logx.WithContext(ctx),
  18. ctx: ctx,
  19. svcCtx: svcCtx}
  20. }
  21. func (l *SendMsgLogic) SendMsg(req *types.WpWecomMsgReq) (resp *types.BaseMsgResp, err error) {
  22. data := map[string]interface{}{
  23. "MsgType": "TalkToFriendTask",
  24. "Content": map[string]interface{}{
  25. "WxId": req.WxId,
  26. "ConvId": req.ConvId,
  27. "ContentType": req.ContentType,
  28. "Content": base64.StdEncoding.EncodeToString([]byte(*req.Content)),
  29. },
  30. }
  31. jsonStr, err := json.Marshal(data)
  32. err = l.svcCtx.WechatWs["wecom"].SendMsg([]byte(jsonStr))
  33. if err != nil {
  34. return nil, err
  35. }
  36. return
  37. }