add_friend_by_phone_logic.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package chat
  2. import (
  3. "context"
  4. "errors"
  5. "wechat-api/ent"
  6. "wechat-api/internal/service/addfriend"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "wechat-api/internal/utils/contextkey"
  10. "github.com/suyuan32/simple-admin-common/enum/errorcode"
  11. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type AddFriendByPhoneLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewAddFriendByPhoneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddFriendByPhoneLogic {
  20. return &AddFriendByPhoneLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx}
  24. }
  25. func (l *AddFriendByPhoneLogic) AddFriendByPhone(req *types.AddFriendByPhoneReq) (resp *types.BaseMsgResp, err error) {
  26. // todo: add your logic here and delete this line
  27. var (
  28. apiKeyObj *ent.ApiKey
  29. ok bool
  30. )
  31. //从上下文中获取鉴权中间件埋下的apiAuthInfo
  32. apiKeyObj, ok = contextkey.AuthTokenInfoKey.GetValue(l.ctx)
  33. if !ok {
  34. return nil, errors.New("content get auth info err")
  35. }
  36. err = l.AppendWechaFriendAddReq(apiKeyObj, req)
  37. if err != nil {
  38. return nil, err
  39. }
  40. //addfriend.NewAddWechatFriendService(l.ctx, l.svcCtx).AddNewFriend(req.WeChatId, req.Phone, req.Message)
  41. addfriend.NewAddWechatFriendService(l.ctx, l.svcCtx).FindFriendByContent(req.WeChatId, req.Phone)
  42. resp = &types.BaseMsgResp{
  43. Msg: errormsg.Success,
  44. Code: errorcode.OK,
  45. }
  46. return resp, nil
  47. }
  48. func (l *AddFriendByPhoneLogic) AppendWechaFriendAddReq(apiKeyObj *ent.ApiKey, req *types.AddFriendByPhoneReq) error {
  49. res, err := l.svcCtx.DB.AddWechatFriendLog.Create().
  50. SetNotNilOwnerWxID(&req.WeChatId).
  51. SetNotNilOwnerWxType(&req.Type).
  52. SetNotNilFindContent(&req.Phone).
  53. SetNotNilMessage(&req.Message).
  54. Save(l.ctx)
  55. if err == nil {
  56. logx.Infof("AppendWechaFriendAddReq succ,get id:%d", res.ID)
  57. }
  58. return err
  59. }