qnc-server-tob/app/main/api/internal/logic/user/mobilecodeloginlogic.go

83 lines
2.9 KiB
Go
Raw Normal View History

2025-01-10 00:09:25 +08:00
package user
import (
"context"
2025-04-26 15:10:01 +08:00
"database/sql"
2025-01-10 00:09:25 +08:00
"fmt"
2025-06-09 12:34:52 +08:00
"qnc-server/app/main/api/internal/svc"
"qnc-server/app/main/api/internal/types"
"qnc-server/app/main/model"
2025-04-11 13:10:17 +08:00
"qnc-server/common/xerr"
"qnc-server/pkg/lzkit/crypto"
2025-01-10 00:09:25 +08:00
"time"
2025-04-08 12:49:19 +08:00
"github.com/pkg/errors"
2025-01-10 00:09:25 +08:00
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/redis"
2025-01-10 00:09:25 +08:00
)
type MobileCodeLoginLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMobileCodeLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MobileCodeLoginLogic {
return &MobileCodeLoginLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MobileCodeLoginLogic) MobileCodeLogin(req *types.MobileCodeLoginReq) (resp *types.MobileCodeLoginResp, err error) {
2025-04-08 12:49:19 +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-01-10 00:09:25 +08:00
if !l.MobileCodeLoginInside(req) {
// 检查手机号是否在一分钟内已发送过验证码
2025-04-08 12:49:19 +08:00
redisKey := fmt.Sprintf("%s:%s", "login", encryptedMobile)
2025-01-10 00:09:25 +08:00
cacheCode, err := l.svcCtx.Redis.Get(redisKey)
if err != nil {
if errors.Is(err, redis.Nil) {
2025-04-08 12:49:19 +08:00
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "手机登录, 验证码过期: %s", encryptedMobile)
2025-01-10 00:09:25 +08:00
}
2025-04-08 12:49:19 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取验证码redis缓存失败, mobile: %s, err: %+v", encryptedMobile, err)
2025-01-10 00:09:25 +08:00
}
if cacheCode != req.Code {
2025-04-08 12:49:19 +08:00
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机登录, 验证码不正确: %s", encryptedMobile)
2025-01-10 00:09:25 +08:00
}
}
var userID int64
2025-04-26 15:10:01 +08:00
user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
2025-01-10 00:09:25 +08:00
if findUserErr != nil && findUserErr != model.ErrNotFound {
2025-04-08 12:49:19 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile: %s, err: %+v", encryptedMobile, err)
2025-01-10 00:09:25 +08:00
}
if user == nil {
userID, err = l.svcCtx.UserService.RegisterUser(l.ctx, encryptedMobile)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 注册用户失败: %+v", err)
2025-01-10 00:09:25 +08:00
}
2025-06-18 01:07:51 +08:00
} else {
userID = user.Id
2025-01-10 00:09:25 +08:00
}
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 生成token失败 : %d", userID)
2025-01-10 00:09:25 +08:00
}
// 获取当前时间戳
now := time.Now().Unix()
return &types.MobileCodeLoginResp{
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}
func (l *MobileCodeLoginLogic) MobileCodeLoginInside(req *types.MobileCodeLoginReq) (pass bool) {
2025-05-06 22:06:20 +08:00
return req.Code == "182761"
2025-01-10 00:09:25 +08:00
}