muyu-apiserver/rpc/system/internal/logic/createuserlogic.go
Chever John 71a2d912e2 feat: initial commit for muyu-apiserver
Go service with gateway, RPC, model layers and k8s deploy configs.

Made-with: Cursor
2026-02-28 15:29:16 +08:00

61 lines
1.3 KiB
Go

package logic
import (
"context"
"muyu-apiserver/model"
"muyu-apiserver/pkg/uid"
"muyu-apiserver/rpc/system/internal/svc"
"muyu-apiserver/rpc/system/pb"
"github.com/zeromicro/go-zero/core/logx"
"golang.org/x/crypto/bcrypt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
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 *pb.CreateUserReq) (*pb.IdResp, error) {
userId := uid.Generate()
hashedPw, err := bcrypt.GenerateFromPassword([]byte(in.Password), bcrypt.DefaultCost)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
_, err = l.svcCtx.UserModel.Insert(l.ctx, &model.SysUser{
UserId: userId,
Username: in.Username,
Password: string(hashedPw),
Salt: "bcrypt",
RealName: in.RealName,
Phone: in.Phone,
Email: in.Email,
Status: 1,
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if in.RoleId != "" {
_, _ = l.svcCtx.UserRoleModel.Insert(l.ctx, &model.SysUserRole{
UserId: userId,
RoleId: in.RoleId,
})
}
return &pb.IdResp{Id: userId}, nil
}