send_text_msg_logic.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package chat
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "time"
  7. "wechat-api/ent"
  8. "wechat-api/ent/predicate"
  9. "wechat-api/ent/wx"
  10. "wechat-api/hook"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. "wechat-api/internal/utils/contextkey"
  14. "github.com/suyuan32/simple-admin-common/enum/errorcode"
  15. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  16. "github.com/zeromicro/go-zero/core/logx"
  17. )
  18. type SendTextMsgLogic struct {
  19. logx.Logger
  20. ctx context.Context
  21. svcCtx *svc.ServiceContext
  22. }
  23. func NewSendTextMsgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendTextMsgLogic {
  24. return &SendTextMsgLogic{
  25. Logger: logx.WithContext(ctx),
  26. ctx: ctx,
  27. svcCtx: svcCtx}
  28. }
  29. func (l *SendTextMsgLogic) SendTextMsg(req *types.SendTextMsgReq) (resp *types.BaseMsgResp, err error) {
  30. // todo: add your logic here and delete this line
  31. var (
  32. apiKeyObj *ent.ApiKey
  33. ok bool
  34. )
  35. //从上下文中获取鉴权中间件埋下的apiAuthInfo
  36. apiKeyObj, ok = contextkey.AuthTokenInfoKey.GetValue(l.ctx)
  37. if !ok {
  38. return nil, errors.New("content get auth info err")
  39. }
  40. //根据wx实体的wxid查询
  41. var predicates []predicate.Wx
  42. if req.WxWxid != nil {
  43. predicates = append(predicates, wx.WxidContains(*req.WxWxid))
  44. }
  45. wxInfo, err := l.svcCtx.DB.Wx.Query().Where(predicates...).Only(l.ctx)
  46. //根据wx实体的主键ID查询
  47. //wxInfo, err := l.svcCtx.DB.Wx.Get(l.ctx, *req.AgentWxId)
  48. l.Infof("wxInfo = %v", wxInfo)
  49. if err != nil {
  50. l.Error("查询微信信息失败", err)
  51. return
  52. }
  53. if wxInfo.OrganizationID != apiKeyObj.OrganizationID {
  54. return nil, errors.New("OID不一致")
  55. }
  56. privateIP := ""
  57. adminPort := ""
  58. port := ""
  59. if wxInfo.ServerID != 0 {
  60. serverInfo, err := l.svcCtx.DB.Server.Get(l.ctx, wxInfo.ServerID)
  61. if err != nil {
  62. l.Error("查询服务器信息失败", err)
  63. return nil, err
  64. }
  65. privateIP = serverInfo.PrivateIP
  66. adminPort = serverInfo.AdminPort
  67. port = wxInfo.Port
  68. }
  69. var ctype uint64
  70. if req.Ctype != nil && *req.Ctype != 0 {
  71. ctype = *req.Ctype
  72. }
  73. var hookClient *hook.Hook
  74. if ctype == 3 {
  75. hookClient = hook.NewWecomHook("", adminPort, port)
  76. } else {
  77. hookClient = hook.NewHook(privateIP, adminPort, port)
  78. }
  79. msgId := time.Now().UnixNano() / int64(time.Microsecond)
  80. l.svcCtx.Rds.Set(l.ctx, fmt.Sprintf("MsgId_FriendId:%d", msgId), *req.Wxid, 10*time.Minute)
  81. err = hookClient.SendTextMsg(*req.Wxid, *req.Msg, wxInfo.Wxid, msgId)
  82. if err != nil {
  83. l.Errorf("发送微信文本消息失败:%v\n", err)
  84. return nil, err
  85. }
  86. resp = &types.BaseMsgResp{
  87. Msg: errormsg.Success,
  88. Code: errorcode.OK,
  89. }
  90. return resp, nil
  91. }