tyc-server/app/main/api/internal/logic/user/registerlogic.go

110 lines
4.1 KiB
Go
Raw Normal View History

2024-11-03 15:28:10 +08:00
package user
import (
"context"
2025-04-09 17:27:40 +08:00
"database/sql"
"fmt"
2025-04-09 15:58:06 +08:00
"time"
2025-04-27 12:17:18 +08:00
"tyc-server/app/main/api/internal/svc"
"tyc-server/app/main/api/internal/types"
"tyc-server/app/main/model"
2025-04-09 15:58:06 +08:00
jwtx "tyc-server/common/jwt"
"tyc-server/common/tool"
"tyc-server/common/xerr"
2025-04-09 17:27:40 +08:00
"tyc-server/pkg/lzkit/crypto"
2025-04-09 15:58:06 +08:00
"tyc-server/pkg/lzkit/lzUtils"
2024-11-03 15:28:10 +08:00
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/stores/redis"
2024-11-03 15:28:10 +08:00
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/logx"
)
type RegisterLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
return &RegisterLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterResp, err error) {
2025-04-09 17:27:40 +08:00
secretKey := l.svcCtx.Config.Encrypt.SecretKey
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, secretKey)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机注册, 加密手机号失败: %+v", err)
}
// 检查手机号是否在一分钟内已发送过验证码
2025-04-09 17:27:40 +08:00
redisKey := fmt.Sprintf("%s:%s", "register", encryptedMobile)
cacheCode, err := l.svcCtx.Redis.Get(redisKey)
if err != nil {
if errors.Is(err, redis.Nil) {
2025-04-09 17:27:40 +08:00
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "手机注册, 验证码过期: %s", encryptedMobile)
}
2025-04-09 17:27:40 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机注册, 读取验证码redis缓存失败, mobile: %s, err: %+v", encryptedMobile, err)
}
if cacheCode != req.Code {
2025-04-09 17:27:40 +08:00
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机注册, 验证码不正确: %s", encryptedMobile)
}
2025-04-09 17:27:40 +08:00
hasUser, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, encryptedMobile)
2024-11-03 15:28:10 +08:00
if findUserErr != nil && findUserErr != model.ErrNotFound {
2025-04-09 17:27:40 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机注册, 读取数据库获取用户失败, mobile%s, err: %+v", encryptedMobile, err)
2024-11-03 15:28:10 +08:00
}
if hasUser != nil {
2025-04-09 17:27:40 +08:00
return nil, errors.Wrapf(xerr.NewErrMsg("该手机号码已注册"), "手机注册, 手机号码已注册, mobile:%s", encryptedMobile)
2024-11-03 15:28:10 +08:00
}
var userId int64
if transErr := l.svcCtx.UserModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error {
user := new(model.User)
2025-04-09 17:27:40 +08:00
user.Mobile = encryptedMobile
if user.Nickname.Valid && user.Nickname.String != "" {
user.Nickname = sql.NullString{
String: encryptedMobile,
Valid: true,
}
2024-11-03 15:28:10 +08:00
}
if len(req.Password) > 0 {
2024-11-21 12:14:34 +08:00
user.Password = lzUtils.StringToNullString(tool.Md5ByString(req.Password))
2024-11-03 15:28:10 +08:00
}
insertResult, userInsertErr := l.svcCtx.UserModel.Insert(ctx, session, user)
if userInsertErr != nil {
2025-04-09 17:27:40 +08:00
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机注册, 数据库插入新用户失败, mobile%s, err: %+v", encryptedMobile, err)
2024-11-03 15:28:10 +08:00
}
lastId, lastInsertIdErr := insertResult.LastInsertId()
if lastInsertIdErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机注册, 获取新用户ID失败, err:%+v, user:%+v", lastInsertIdErr, user)
2024-11-03 15:28:10 +08:00
}
userId = lastId
userAuth := new(model.UserAuth)
userAuth.UserId = lastId
2025-04-09 17:27:40 +08:00
userAuth.AuthKey = encryptedMobile
2025-05-27 18:35:01 +08:00
userAuth.AuthType = model.UserAuthTypeMobile
if _, userAuthInsertErr := l.svcCtx.UserAuthModel.Insert(ctx, session, userAuth); userAuthInsertErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机注册, 数据库插入用户认证失败, err:%+v", userAuthInsertErr)
2024-11-03 15:28:10 +08:00
}
return nil
}); transErr != nil {
return nil, transErr
}
token, generaErr := jwtx.GenerateJwtToken(userId, l.svcCtx.Config.JwtAuth.AccessSecret, l.svcCtx.Config.JwtAuth.AccessExpire)
if generaErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机注册, 生成jwt token失败, userid: %d, err:%+v", userId, generaErr)
2024-11-03 15:28:10 +08:00
}
// 获取当前时间戳
now := time.Now().Unix()
2024-11-03 15:28:10 +08:00
return &types.RegisterResp{
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
2024-11-03 15:28:10 +08:00
}, nil
}