first commit
This commit is contained in:
60
apps/user/internal/logic/auth/loginuserlogic.go
Normal file
60
apps/user/internal/logic/auth/loginuserlogic.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package authlogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
jwtx "tianyuan-api/pkg/jwt"
|
||||
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type LoginUserLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewLoginUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginUserLogic {
|
||||
return &LoginUserLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 登录接口
|
||||
func (l *LoginUserLogic) LoginUser(in *user.LoginReq) (*user.LoginResp, error) {
|
||||
if in.Username == "" || in.Password == "" {
|
||||
return nil, errors.New("用户名或密码不能为空")
|
||||
}
|
||||
// 使用 FindOneByUsername 查找用户
|
||||
users, err := l.svcCtx.UserModel.FindOneByUsername(l.ctx, in.Username)
|
||||
if err != nil {
|
||||
return nil, errors.New("用户未注册")
|
||||
}
|
||||
|
||||
// 验证密码
|
||||
if hashPassword(in.Password) != users.Password {
|
||||
return nil, errors.New("密码错误")
|
||||
}
|
||||
|
||||
// 生成 JWT token,调用封装好的函数
|
||||
token, err := jwtx.GenerateJwtToken(users.Id, l.svcCtx.Config.AuthJWT.AccessSecret, l.svcCtx.Config.AuthJWT.AccessExpire)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &user.LoginResp{
|
||||
Token: token,
|
||||
}, nil
|
||||
}
|
||||
func hashPassword(password string) string {
|
||||
h := sha256.New()
|
||||
h.Write([]byte(password))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
62
apps/user/internal/logic/auth/phoneloginuserlogic.go
Normal file
62
apps/user/internal/logic/auth/phoneloginuserlogic.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package authlogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
jwtx "tianyuan-api/pkg/jwt"
|
||||
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type PhoneLoginUserLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewPhoneLoginUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PhoneLoginUserLogic {
|
||||
return &PhoneLoginUserLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 手机登录接口
|
||||
func (l *PhoneLoginUserLogic) PhoneLoginUser(in *user.PhoneLoginReq) (*user.LoginResp, error) {
|
||||
// 从 Redis 获取验证码
|
||||
savedCode, err := l.svcCtx.Redis.Get(fmt.Sprintf("login:%s", in.Phone))
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, errors.New("验证码已过期")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 验证码不匹配
|
||||
if savedCode != in.Code {
|
||||
return nil, errors.New("验证码不正确")
|
||||
}
|
||||
// 查询用户是否存在,如果不存在则注册新用户
|
||||
users, err := l.svcCtx.UserModel.FindOneByPhone(l.ctx, in.Phone)
|
||||
if errors.Is(err, sqlx.ErrNotFound) {
|
||||
return nil, errors.New("手机号未注册")
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
token, err := jwtx.GenerateJwtToken(users.Id, l.svcCtx.Config.AuthJWT.AccessSecret, l.svcCtx.Config.AuthJWT.AccessExpire)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &user.LoginResp{
|
||||
Token: token,
|
||||
}, nil
|
||||
}
|
||||
136
apps/user/internal/logic/auth/registeruserlogic.go
Normal file
136
apps/user/internal/logic/auth/registeruserlogic.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package authlogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/redis"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"tianyuan-api/apps/user/internal/model"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"tianyuan-api/apps/user/internal/svc"
|
||||
"tianyuan-api/apps/user/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RegisterUserLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewRegisterUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterUserLogic {
|
||||
return &RegisterUserLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 注册接口
|
||||
func (l *RegisterUserLogic) RegisterUser(in *user.RegisterReq) (*user.EmptyResponse, error) {
|
||||
// 检查密码是否一致
|
||||
if in.Password != in.ConfirmPassword {
|
||||
return nil, errors.New("密码不一致")
|
||||
}
|
||||
// 检查密码强度
|
||||
if err := checkPasswordStrength(in.Password); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 校验手机号码
|
||||
err := validatePhoneNumber(in.Phone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 从 Redis 获取验证码
|
||||
savedCode, err := l.svcCtx.Redis.Get(fmt.Sprintf("register:%s", in.Phone))
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, errors.New("验证码已过期")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 验证码不匹配
|
||||
if savedCode != in.Code {
|
||||
return nil, errors.New("验证码不正确")
|
||||
}
|
||||
// 检查用户名是否已经存在
|
||||
existingUser, err := l.svcCtx.UserModel.FindOneByUsername(l.ctx, in.Username)
|
||||
if err != nil && err != sqlc.ErrNotFound {
|
||||
// 如果发生其他错误,返回错误
|
||||
return nil, err
|
||||
}
|
||||
if existingUser != nil {
|
||||
// 用户名已经存在,返回错误
|
||||
return nil, errors.New("用户名已存在,请选择其他用户名")
|
||||
}
|
||||
|
||||
// 检查手机号是否已经存在
|
||||
existingPhone, err := l.svcCtx.UserModel.FindOneByPhone(l.ctx, in.Phone)
|
||||
if err != nil && err != sqlc.ErrNotFound {
|
||||
// 如果发生其他错误,返回错误
|
||||
return nil, err
|
||||
}
|
||||
if existingPhone != nil {
|
||||
// 用户名已经存在,返回错误
|
||||
return nil, errors.New("手机号码已存在,请选择其他用户名")
|
||||
}
|
||||
// 加密密码
|
||||
hashedPassword := hashPassword(in.Password)
|
||||
|
||||
// 构建 Users 结构体
|
||||
users := &model.Users{
|
||||
Username: in.Username,
|
||||
Password: hashedPassword,
|
||||
Phone: in.Phone,
|
||||
AuthStatus: "unverified",
|
||||
}
|
||||
|
||||
// 调用 Insert 方法插入用户数据
|
||||
_, err = l.svcCtx.UserModel.Insert(l.ctx, users)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &user.EmptyResponse{}, nil
|
||||
}
|
||||
|
||||
// 密码强度检查
|
||||
func checkPasswordStrength(password string) error {
|
||||
// 检查密码长度是否不少于8位
|
||||
if len(password) < 8 {
|
||||
return errors.New("密码长度不能少于8位")
|
||||
}
|
||||
|
||||
// 检查密码是否为简单重复的字符(如"11111111" 或 "aaaaaaaa"等)
|
||||
firstChar := password[0]
|
||||
if strings.Count(password, string(firstChar)) == len(password) {
|
||||
return errors.New("密码不能是重复的字符")
|
||||
}
|
||||
|
||||
// 正则表达式:密码必须包含数字或字母,不能是全符号
|
||||
var passwordRegex = `^[A-Za-z0-9]+$`
|
||||
match, _ := regexp.MatchString(passwordRegex, password)
|
||||
if !match {
|
||||
return errors.New("密码只能包含字母和数字")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 校验手机号码的函数
|
||||
func validatePhoneNumber(phone string) error {
|
||||
// 定义正则表达式,匹配中国大陆的手机号格式
|
||||
var phoneRegex = `^1[3-9]\d{9}$`
|
||||
|
||||
// 检查手机号是否匹配正则表达式
|
||||
match, _ := regexp.MatchString(phoneRegex, phone)
|
||||
if !match {
|
||||
return errors.New("手机号码格式不正确")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user