56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
// Code scaffolded by goctl. Safe to edit.
|
|
// goctl 1.9.2
|
|
|
|
package user
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"juwan-backend/app/users/api/internal/svc"
|
|
"juwan-backend/app/users/api/internal/types"
|
|
"juwan-backend/app/users/rpc/pb"
|
|
|
|
"github.com/google/uuid"
|
|
"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.RegisterReq) (resp *types.RegisterResp, err error) {
|
|
// todo: add your logic here and delete this line
|
|
user, err := l.svcCtx.UserRpc.GetUserByUsername(l.ctx, &pb.GetUserByUsernameReq{
|
|
Username: req.Username,
|
|
})
|
|
if err == nil || user != nil {
|
|
return nil, errors.New("User is exisit")
|
|
}
|
|
id, err := uuid.NewRandom()
|
|
if err != nil {
|
|
return nil, errors.New("Register is failed")
|
|
}
|
|
|
|
_, err = l.svcCtx.UserRpc.AddUsers(l.ctx, &pb.AddUsersReq{
|
|
UserId: id.String(),
|
|
Username: req.Username,
|
|
Passwd: req.Password,
|
|
Phone: req.Phone,
|
|
State: true,
|
|
})
|
|
|
|
return
|
|
}
|