123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package chat
- import (
- "context"
- "errors"
- "wechat-api/ent"
- "wechat-api/ent/predicate"
- "wechat-api/ent/wx"
- "wechat-api/hook"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- "wechat-api/internal/utils/contextkey"
- "github.com/suyuan32/simple-admin-common/enum/errorcode"
- "github.com/suyuan32/simple-admin-common/msg/errormsg"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type SendTextMsgLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewSendTextMsgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendTextMsgLogic {
- return &SendTextMsgLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx}
- }
- func (l *SendTextMsgLogic) SendTextMsg(req *types.SendTextMsgReq) (resp *types.BaseMsgResp, err error) {
- // todo: add your logic here and delete this line
- var (
- apiKeyObj *ent.ApiKey
- ok bool
- )
- //从上下文中获取鉴权中间件埋下的apiAuthInfo
- apiKeyObj, ok = contextkey.AuthTokenInfoKey.GetValue(l.ctx)
- if !ok {
- return nil, errors.New("content get auth info err")
- }
- //根据wx实体的wxid查询
- var predicates []predicate.Wx
- if req.WxWxid != nil {
- predicates = append(predicates, wx.WxidContains(*req.WxWxid))
- }
- wxInfo, err := l.svcCtx.DB.Wx.Query().Where(predicates...).Only(l.ctx)
- //根据wx实体的主键ID查询
- //wxInfo, err := l.svcCtx.DB.Wx.Get(l.ctx, *req.AgentWxId)
- l.Infof("wxInfo = %v", wxInfo)
- if err != nil {
- l.Error("查询微信信息失败", err)
- return
- }
- if wxInfo.OrganizationID != apiKeyObj.OrganizationID {
- return nil, errors.New("OID不一致")
- }
- privateIP := ""
- adminPort := ""
- port := ""
- if wxInfo.ServerID != 0 {
- serverInfo, err := l.svcCtx.DB.Server.Get(l.ctx, wxInfo.ServerID)
- if err != nil {
- l.Error("查询服务器信息失败", err)
- return nil, err
- }
- privateIP = serverInfo.PrivateIP
- adminPort = serverInfo.AdminPort
- port = wxInfo.Port
- }
- var ctype uint64
- if req.Ctype != nil && *req.Ctype != 0 {
- ctype = *req.Ctype
- }
- var hookClient *hook.Hook
- if ctype == 3 {
- hookClient = hook.NewWecomHook("", adminPort, port)
- } else {
- hookClient = hook.NewHook(privateIP, adminPort, port)
- }
- err = hookClient.SendTextMsg(*req.Wxid, *req.Msg, wxInfo.Wxid)
- if err != nil {
- l.Errorf("发送微信文本消息失败:%v\n", err)
- return nil, err
- }
- resp = &types.BaseMsgResp{
- Msg: errormsg.Success,
- Code: errorcode.OK,
- }
- return resp, nil
- }
|