2025-10-02 06:43:57 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"backend/internal/svc"
|
|
|
|
|
"backend/internal/types"
|
2025-11-09 08:49:34 +00:00
|
|
|
errs "backend/pkg/library/errors"
|
|
|
|
|
mbr "backend/pkg/member/domain/member"
|
|
|
|
|
member "backend/pkg/member/domain/usecase"
|
|
|
|
|
"backend/pkg/permission/domain/token"
|
|
|
|
|
"context"
|
2025-10-02 06:43:57 +00:00
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SubmitVerificationCodeLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 06:37:41 +00:00
|
|
|
// NewSubmitVerificationCodeLogic 交驗證碼以完成驗證
|
2025-10-02 06:43:57 +00:00
|
|
|
func NewSubmitVerificationCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SubmitVerificationCodeLogic {
|
|
|
|
|
return &SubmitVerificationCodeLogic{
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-09 08:49:34 +00:00
|
|
|
func (l *SubmitVerificationCodeLogic) SubmitVerificationCode(req *types.SubmitVerificationCodeReq) (*types.RespOK, error) {
|
2025-11-09 09:27:08 +00:00
|
|
|
acc := ""
|
|
|
|
|
ct := mbr.GenerateCodeTypeEmail
|
2025-11-09 08:49:34 +00:00
|
|
|
switch req.Purpose {
|
|
|
|
|
case "email_verification":
|
2025-11-09 09:27:08 +00:00
|
|
|
if !isValidEmail(req.Account) {
|
|
|
|
|
return nil, errs.InputInvalidFormatError("email is invalid")
|
|
|
|
|
}
|
|
|
|
|
acc = req.Account
|
2025-11-09 08:49:34 +00:00
|
|
|
case "phone_verification":
|
2025-11-09 09:27:08 +00:00
|
|
|
phone, isPhone := normalizeTaiwanMobile(req.Account)
|
|
|
|
|
if !isPhone {
|
|
|
|
|
return nil, errs.InputInvalidFormatError("phone number is invalid")
|
|
|
|
|
}
|
|
|
|
|
acc = phone
|
2025-11-09 08:49:34 +00:00
|
|
|
ct = mbr.GenerateCodeTypePhone
|
|
|
|
|
default:
|
2025-11-09 09:27:08 +00:00
|
|
|
return &types.RespOK{}, errs.InputInvalidRangeError("")
|
2025-11-09 08:49:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 先驗證,不刪除
|
2025-11-09 09:27:08 +00:00
|
|
|
if err := l.svcCtx.AccountUC.CheckRefreshCode(l.ctx, member.VerifyRefreshCodeRequest{
|
2025-11-09 08:49:34 +00:00
|
|
|
VerifyCode: req.VerifyCode,
|
2025-11-09 09:27:08 +00:00
|
|
|
LoginID: acc,
|
2025-11-09 08:49:34 +00:00
|
|
|
CodeType: ct,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
e := errs.AuthForbiddenError("failed to get verify code").Wrap(err)
|
|
|
|
|
|
|
|
|
|
return nil, e
|
|
|
|
|
}
|
2025-11-09 09:27:08 +00:00
|
|
|
uid := token.UID(l.ctx)
|
|
|
|
|
switch req.Purpose {
|
|
|
|
|
case "email_verification":
|
|
|
|
|
err := l.svcCtx.AccountUC.BindVerifyEmail(l.ctx, uid, acc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
case "phone_verification":
|
|
|
|
|
err := l.svcCtx.AccountUC.BindVerifyPhone(l.ctx, uid, acc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return &types.RespOK{}, errs.InputInvalidRangeError("")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := l.svcCtx.AccountUC.UpdateStatus(l.ctx, member.UpdateStatusRequest{
|
|
|
|
|
UID: uid,
|
|
|
|
|
Status: mbr.AccountStatusActive,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 真的刪除
|
|
|
|
|
_ = l.svcCtx.AccountUC.VerifyRefreshCode(l.ctx, member.VerifyRefreshCodeRequest{
|
|
|
|
|
VerifyCode: req.VerifyCode,
|
|
|
|
|
LoginID: acc,
|
|
|
|
|
CodeType: ct,
|
|
|
|
|
})
|
2025-10-02 06:43:57 +00:00
|
|
|
|
2025-11-09 08:49:34 +00:00
|
|
|
return &types.RespOK{}, nil
|
2025-10-02 06:43:57 +00:00
|
|
|
}
|