add_friend_by_phone_logic.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 AppendWechaFriendAddLog(ctx context.Context, svcCtx *svc.ServiceContext, addLog *types.AddFriendByPhoneReq) (int64, error) {
  49. isCanAdd := 0
  50. if addLog.Type != 1 {
  51. isCanAdd = 1
  52. }
  53. res, err := svcCtx.DB.AddWechatFriendLog.Create().
  54. SetNotNilOwnerWxID(&addLog.WeChatId).
  55. SetNotNilOwnerWxType(&addLog.Type).
  56. SetNotNilFindContent(&addLog.Phone).
  57. SetNotNilMessage(&addLog.Message).
  58. SetIsCanAdd(isCanAdd).
  59. Save(ctx)
  60. id := int64(0)
  61. if err == nil {
  62. id = res.ID
  63. }
  64. return id, err
  65. }
  66. func (l *AddFriendByPhoneLogic) AppendWechaFriendAddReq(apiKeyObj *ent.ApiKey, req *types.AddFriendByPhoneReq) error {
  67. id, err := AppendWechaFriendAddLog(l.ctx, l.svcCtx, req)
  68. if err == nil {
  69. logx.Infof("AppendWechaFriendAddReq succ,get id:%d", id)
  70. }
  71. return err
  72. }