50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
sysModel "sundynix-micro-go/app/system/model"
|
|
"sundynix-micro-go/app/system/rpc/internal/svc"
|
|
"sundynix-micro-go/app/system/rpc/system"
|
|
"sundynix-micro-go/common/utils/hash"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type CreateUserLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic {
|
|
return &CreateUserLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
|
|
}
|
|
|
|
func (l *CreateUserLogic) CreateUser(in *system.CreateUserReq) (*system.CreateUserResp, error) {
|
|
// 如果有 Account,检查是否重复(后台创建用户场景:禁止重复)
|
|
if in.Account != "" {
|
|
var count int64
|
|
l.svcCtx.DB.Model(&sysModel.SundynixUser{}).Where("account = ?", in.Account).Count(&count)
|
|
if count > 0 {
|
|
return nil, fmt.Errorf("账号 %s 已存在", in.Account)
|
|
}
|
|
}
|
|
|
|
u := sysModel.SundynixUser{
|
|
Name: in.Name,
|
|
Account: in.Account,
|
|
Phone: in.Phone,
|
|
NickName: in.NickName,
|
|
ClientID: in.ClientId,
|
|
}
|
|
if in.Password != "" {
|
|
u.Password = hash.BcryptHash(in.Password)
|
|
}
|
|
if err := l.svcCtx.DB.Create(&u).Error; err != nil {
|
|
return nil, fmt.Errorf("创建用户失败: %w", err)
|
|
}
|
|
return &system.CreateUserResp{User: convertUserToProto(&u)}, nil
|
|
}
|