69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"muyu-apiserver/model"
|
|
"muyu-apiserver/rpc/system/internal/svc"
|
|
"muyu-apiserver/rpc/system/pb"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type GetUserLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserLogic {
|
|
return &GetUserLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *GetUserLogic) GetUser(in *pb.GetUserReq) (*pb.UserInfo, error) {
|
|
user, err := l.svcCtx.UserModel.FindOneByUserId(l.ctx, in.UserId)
|
|
if err != nil {
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
return nil, status.Error(codes.NotFound, "user not found")
|
|
}
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
}
|
|
|
|
var roleId, roleName string
|
|
userRole, _ := l.svcCtx.UserRoleModel.FindByUserId(l.ctx, user.UserId)
|
|
if userRole != nil {
|
|
roleId = userRole.RoleId
|
|
role, _ := l.svcCtx.RoleModel.FindOneByRoleId(l.ctx, userRole.RoleId)
|
|
if role != nil {
|
|
roleName = role.RoleName
|
|
}
|
|
}
|
|
|
|
var lastLoginTime string
|
|
if user.LastLoginTime.Valid {
|
|
lastLoginTime = user.LastLoginTime.Time.Format("2006-01-02 15:04:05")
|
|
}
|
|
|
|
return &pb.UserInfo{
|
|
UserId: user.UserId,
|
|
Username: user.Username,
|
|
RealName: user.RealName,
|
|
Phone: user.Phone,
|
|
Email: user.Email,
|
|
Avatar: user.Avatar,
|
|
Status: user.Status,
|
|
RoleId: roleId,
|
|
RoleName: roleName,
|
|
LastLoginTime: lastLoginTime,
|
|
CreatedAt: user.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
UpdatedAt: user.UpdatedAt.Format("2006-01-02 15:04:05"),
|
|
}, nil
|
|
}
|