You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.9 KiB
81 lines
1.9 KiB
package auth
|
|
|
|
import (
|
|
"context"
|
|
"crypto/md5"
|
|
"fmt"
|
|
|
|
"github.com/youruser/base/internal/svc"
|
|
"github.com/youruser/base/internal/types"
|
|
"github.com/youruser/base/model"
|
|
|
|
"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.RegisterRequest) (resp *types.UserInfo, err error) {
|
|
_, err = model.FindOneByUsername(l.ctx, l.svcCtx.DB, req.Username)
|
|
if err == nil {
|
|
return nil, fmt.Errorf("用户名已被注册")
|
|
}
|
|
if err != model.ErrNotFound {
|
|
return nil, fmt.Errorf("检查用户名失败: %v", err)
|
|
}
|
|
|
|
_, err = model.FindOneByPhone(l.ctx, l.svcCtx.DB, req.Phone)
|
|
if err == nil {
|
|
return nil, fmt.Errorf("手机号已被注册")
|
|
}
|
|
if err != model.ErrNotFound {
|
|
return nil, fmt.Errorf("检查手机号失败: %v", err)
|
|
}
|
|
|
|
user := &model.User{
|
|
Username: req.Username,
|
|
Email: req.Email,
|
|
Password: fmt.Sprintf("%x", md5.Sum([]byte(req.Password))),
|
|
Phone: req.Phone,
|
|
Role: model.RoleUser,
|
|
Source: model.SourceRegister,
|
|
Status: 1,
|
|
}
|
|
|
|
id, err := model.Insert(l.ctx, l.svcCtx.DB, user)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("创建用户失败: %v", err)
|
|
}
|
|
|
|
user, err = model.FindOne(l.ctx, l.svcCtx.DB, id)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("查询用户失败: %v", err)
|
|
}
|
|
|
|
resp = &types.UserInfo{
|
|
Id: user.Id,
|
|
Username: user.Username,
|
|
Email: user.Email,
|
|
Phone: user.Phone,
|
|
Role: user.Role,
|
|
Source: user.Source,
|
|
Remark: user.Remark,
|
|
Status: int(user.Status),
|
|
CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
UpdatedAt: user.UpdatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
l.Infof("注册成功,userId=%d", user.Id)
|
|
return resp, nil
|
|
}
|
|
|