151 lines
4.6 KiB
Go
151 lines
4.6 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
"mingyang-admin-app-rpc/internal/jwt_manager"
|
|
"mingyang-admin-app-rpc/internal/logic/cacherepo"
|
|
"mingyang-admin-app-rpc/internal/svc"
|
|
"mingyang-admin-app-rpc/internal/util"
|
|
"mingyang-admin-app-rpc/types/app"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type RegisterUserLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
jwtManager *jwt_manager.JWTManager
|
|
}
|
|
|
|
func NewRegisterUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterUserLogic {
|
|
return &RegisterUserLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
jwtManager: jwt_manager.NewJWTManager(&svcCtx.Config.JWTConf),
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
// RegisterUser 用户注册
|
|
func (s *RegisterUserLogic) RegisterUser(req *app.RegisterUserRequest) (*app.RegisterUserResponse, error) {
|
|
// 1. 验证输入
|
|
if err := s.validateRegisterRequest(req); err != nil {
|
|
return nil, status.Errorf(codes.InvalidArgument, "validation failed: %v", err)
|
|
}
|
|
|
|
// 2. 验证验证码
|
|
if err := s.verifyRegistrationCode(s.ctx, req); err != nil {
|
|
return nil, status.Errorf(codes.InvalidArgument, "verification failed: %v", err)
|
|
}
|
|
|
|
// 3. 验证密码强度
|
|
if err := util.ValidatePasswordStrength(req.GetPassword()); err != nil {
|
|
return nil, status.Errorf(codes.InvalidArgument, "password validation failed: %v", err)
|
|
}
|
|
// 4. 哈希密码
|
|
passwordHash, err := util.HashPassword(req.GetPassword())
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.Internal, "failed to hash password: %v", err)
|
|
}
|
|
|
|
// 5. 标准化手机号格式
|
|
var mobile string
|
|
if req.GetMobile() != "" {
|
|
normalizedPhone := util.NormalizePhone(req.GetMobile())
|
|
mobile = normalizedPhone
|
|
}
|
|
|
|
// 6. 准备用户数据
|
|
userData := CreateUserData{
|
|
Username: strings.ToLower(strings.TrimSpace(req.GetUsername())),
|
|
Email: strings.ToLower(strings.TrimSpace(req.GetEmail())),
|
|
Mobile: mobile,
|
|
PasswordHash: passwordHash,
|
|
Nickname: req.GetNickName(),
|
|
RegistrationSource: req.GetRegistrationSource(),
|
|
Metadata: map[string]interface{}{
|
|
"registered_via": req.RegistrationSource,
|
|
"registered_at": time.Now().Format(time.RFC3339),
|
|
},
|
|
}
|
|
|
|
userRepo := NewUser(s.ctx, s.svcCtx)
|
|
|
|
user, err := userRepo.CreateUser(s.ctx, &userData)
|
|
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "already exists") {
|
|
return nil, status.Errorf(codes.AlreadyExists, "user already exists")
|
|
}
|
|
return nil, status.Errorf(codes.Internal, "failed to create user: %v", err)
|
|
}
|
|
|
|
// 8. 生成JWT令牌
|
|
tokenPair, err := s.jwtManager.GenerateTokenPair(*user.Id)
|
|
if err != nil {
|
|
// 注意:这里不应该返回错误,因为用户已经创建成功
|
|
// 只是记录日志,继续处理
|
|
fmt.Printf("Failed to generate token for user %v: %v\n", user.Id, err)
|
|
}
|
|
|
|
// 10. 发送欢迎邮件(异步)
|
|
go func() {
|
|
//todo 发送欢迎邮件
|
|
|
|
}()
|
|
// 11. 构建响应
|
|
response := &app.RegisterUserResponse{
|
|
User: user,
|
|
}
|
|
if tokenPair != nil {
|
|
response.AuthToken = &app.AuthToken{
|
|
AccessToken: tokenPair.AccessToken,
|
|
RefreshToken: tokenPair.RefreshToken,
|
|
TokenType: tokenPair.TokenType,
|
|
ExpiresIn: int32(tokenPair.AccessTokenExpiresAt.Sub(time.Now()).Seconds()),
|
|
IssuedAt: timestamppb.New(time.Now()),
|
|
ExpiresAt: timestamppb.New(tokenPair.AccessTokenExpiresAt),
|
|
}
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
// validateRegisterRequest 验证注册请求
|
|
func (s *RegisterUserLogic) validateRegisterRequest(req *app.RegisterUserRequest) error {
|
|
return nil
|
|
}
|
|
|
|
// verifyRegistrationCode 验证注册验证码
|
|
func (s *RegisterUserLogic) verifyRegistrationCode(ctx context.Context, req *app.RegisterUserRequest) error {
|
|
key := req.GetVerificationId()
|
|
cacheRepository := cacherepo.NewCacheRepository(ctx, s.svcCtx)
|
|
// 获取验证码数据
|
|
codeData, err := cacheRepository.GetVerificationCode(ctx, key)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get verification code.proto: %w", err)
|
|
}
|
|
if codeData == nil {
|
|
return errors.New("verification code.proto expired or not found")
|
|
}
|
|
// 验证验证码
|
|
if codeData.Code != req.GetVerificationCode() {
|
|
// 增加尝试次数
|
|
if err := cacheRepository.IncrementVerificationAttempts(ctx, key); err != nil {
|
|
fmt.Printf("Failed to increment verification attempts: %v\n", err)
|
|
}
|
|
return errors.New("invalid verification code.proto")
|
|
}
|
|
// 验证成功,删除验证码
|
|
if err := cacheRepository.DeleteVerificationCode(ctx, key); err != nil {
|
|
fmt.Printf("Failed to delete verification code.proto: %v\n", err)
|
|
}
|
|
return nil
|
|
}
|