diff --git a/internal/logic/ping/ping_logic.go b/internal/logic/ping/ping_logic.go index 87acd16..aad113d 100644 --- a/internal/logic/ping/ping_logic.go +++ b/internal/logic/ping/ping_logic.go @@ -1,8 +1,11 @@ package ping import ( - "backend/internal/svc" + "backend/pkg/notification/domain/usecase" "context" + "fmt" + + "backend/internal/svc" "github.com/zeromicro/go-zero/core/logx" ) @@ -23,5 +26,16 @@ func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLogic { } func (l *PingLogic) Ping() error { + + err := l.svcCtx.DeliveryUC.SendEmail(l.ctx, usecase.MailReq{ + To: []string{"igs170911@gmail.com"}, + From: l.svcCtx.Config.SMTPConfig.Sender, + SenderName: "t", + Subject: "test", + Body: "good", + }) + + fmt.Println(err) + return nil } diff --git a/internal/logic/user/submit_verification_code_logic.go b/internal/logic/user/submit_verification_code_logic.go index 77cdc46..0adb44c 100644 --- a/internal/logic/user/submit_verification_code_logic.go +++ b/internal/logic/user/submit_verification_code_logic.go @@ -1,10 +1,13 @@ package user import ( - "context" - "backend/internal/svc" "backend/internal/types" + errs "backend/pkg/library/errors" + mbr "backend/pkg/member/domain/member" + member "backend/pkg/member/domain/usecase" + "backend/pkg/permission/domain/token" + "context" "github.com/zeromicro/go-zero/core/logx" ) @@ -24,8 +27,32 @@ func NewSubmitVerificationCodeLogic(ctx context.Context, svcCtx *svc.ServiceCont } } -func (l *SubmitVerificationCodeLogic) SubmitVerificationCode(req *types.SubmitVerificationCodeReq) (resp *types.RespOK, err error) { - // todo: add your logic here and delete this line +func (l *SubmitVerificationCodeLogic) SubmitVerificationCode(req *types.SubmitVerificationCodeReq) (*types.RespOK, error) { + var ct mbr.GenerateCodeType + switch req.Purpose { + case "email_verification": + ct = mbr.GenerateCodeTypeEmail + case "phone_verification": + ct = mbr.GenerateCodeTypePhone + default: + return &types.RespOK{}, errs.InputInvalidRangeError("failed to get correct verification type") + } - return + info, err := l.svcCtx.AccountUC.GetUserAccountInfo(l.ctx, member.GetUIDByAccountRequest{Account: token.LoginID(l.ctx)}) + if err != nil { + return nil, err + } + + // 先驗證,不刪除 + if err = l.svcCtx.AccountUC.CheckRefreshCode(l.ctx, member.VerifyRefreshCodeRequest{ + VerifyCode: req.VerifyCode, + LoginID: info.Data.LoginID, + CodeType: ct, + }); err != nil { + e := errs.AuthForbiddenError("failed to get verify code").Wrap(err) + + return nil, e + } + + return &types.RespOK{}, nil } diff --git a/internal/logic/user/update_password_logic.go b/internal/logic/user/update_password_logic.go index 5e187b8..2b5b2f5 100644 --- a/internal/logic/user/update_password_logic.go +++ b/internal/logic/user/update_password_logic.go @@ -1,10 +1,14 @@ package user import ( - "context" - "backend/internal/svc" "backend/internal/types" + errs "backend/pkg/library/errors" + mbr "backend/pkg/member/domain/member" + member "backend/pkg/member/domain/usecase" + tokeneEntity "backend/pkg/permission/domain/entity" + "backend/pkg/permission/domain/token" + "context" "github.com/zeromicro/go-zero/core/logx" ) @@ -15,7 +19,7 @@ type UpdatePasswordLogic struct { svcCtx *svc.ServiceContext } -// 修改當前登入使用者的密碼 +// NewUpdatePasswordLogic 修改當前登入使用者的密碼 func NewUpdatePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePasswordLogic { return &UpdatePasswordLogic{ Logger: logx.WithContext(ctx), @@ -24,8 +28,41 @@ func NewUpdatePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Up } } -func (l *UpdatePasswordLogic) UpdatePassword(req *types.UpdatePasswordReq) (resp *types.RespOK, err error) { - // todo: add your logic here and delete this line +func (l *UpdatePasswordLogic) UpdatePassword(req *types.UpdatePasswordReq) (*types.RespOK, error) { + if req.NewPassword != req.NewPasswordConfirm { + return nil, errs.InputInvalidFormatError("failed to check token") + } + loginID := token.LoginID(l.ctx) + // 驗證是否本地平台 + info, err := l.svcCtx.AccountUC.GetUserAccountInfo(l.ctx, member.GetUIDByAccountRequest{ + Account: loginID, + }) + if err != nil { + return nil, err + } - return + if info.Data.Platform != mbr.Digimon { + return nil, errs.InputInvalidFormatError("failed th change password via third party login") + } + + // 驗證舊密碼是否正確 + if _, err := l.svcCtx.AccountUC.VerifyPlatformAuthResult(l.ctx, member.VerifyAuthResultRequest{ + Account: loginID, + Token: req.CurrentPassword, + }); err != nil { + return nil, errs.AuthForbiddenError("failed to verify correct password") + } + + // 更新 + err = l.svcCtx.AccountUC.UpdateUserToken(l.ctx, member.UpdateTokenRequest{ + Account: loginID, + Token: req.NewPassword, + Platform: mbr.Digimon.ToInt64(), + }) + + _ = l.svcCtx.TokenUC.CancelToken(l.ctx, tokeneEntity.CancelTokenReq{ + Token: req.Authorization.Authorization, + }) + + return &types.RespOK{}, nil } diff --git a/pkg/notification/repository/smtp_mailer.go b/pkg/notification/repository/smtp_mailer.go index 02b2339..27ec6a8 100644 --- a/pkg/notification/repository/smtp_mailer.go +++ b/pkg/notification/repository/smtp_mailer.go @@ -4,7 +4,6 @@ import ( "backend/pkg/notification/config" "backend/pkg/notification/domain/repository" "context" - "fmt" "gopkg.in/gomail.v2" ) @@ -33,14 +32,14 @@ func (repo *SMTPMailRepository) SendMail(ctx context.Context, req repository.Mai return ctx.Err() } - sender := req.From - if req.SenderName != "" { - sender = fmt.Sprintf("%s <%s>", req.SenderName, req.From) - } - // 構建郵件 m := gomail.NewMessage() - m.SetHeader("From", sender) + + if req.SenderName != "" { + m.SetAddressHeader("From", req.From, req.SenderName) + } else { + m.SetHeader("From", req.From) + } m.SetHeader("To", req.To...) m.SetHeader("Subject", req.Subject) m.SetBody("text/html", req.Body)