init: init refactor
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/model"
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GetUserByIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetUserByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserByIdLogic {
|
||||
return &GetUserByIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 根据ID获取用户信息
|
||||
func (l *GetUserByIdLogic) GetUserById(in *user.GetUserByIdReq) (*user.GetUserByIdResp, error) {
|
||||
var u model.SundynixUser
|
||||
err := l.svcCtx.DB.Where("id = ?", in.Id).First(&u).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, status.Error(codes.NotFound, "用户不存在")
|
||||
}
|
||||
l.Errorf("查询用户失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询用户失败")
|
||||
}
|
||||
|
||||
return &user.GetUserByIdResp{
|
||||
User: convertUserToProto(&u),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// convertUserToProto 将GORM模型转换为proto消息
|
||||
func convertUserToProto(u *model.SundynixUser) *user.UserInfo {
|
||||
info := &user.UserInfo{
|
||||
Id: u.ID,
|
||||
TenantId: u.TenantID,
|
||||
ClientId: u.ClientID,
|
||||
Name: u.Name,
|
||||
Account: u.Account,
|
||||
NickName: u.NickName,
|
||||
Phone: u.Phone,
|
||||
SessionKey: u.SessionKey,
|
||||
UnionId: u.UnionID,
|
||||
OpenId: u.OpenID,
|
||||
SaOpenId: u.SaOpenID,
|
||||
AvatarId: u.AvatarID,
|
||||
Gender: int32(u.Gender),
|
||||
CreatedAt: u.CreatedAt.Unix(),
|
||||
UpdatedAt: u.UpdatedAt.Unix(),
|
||||
}
|
||||
if u.LastLoginAt != nil {
|
||||
info.LastLoginAt = u.LastLoginAt.Unix()
|
||||
}
|
||||
return info
|
||||
}
|
||||
Reference in New Issue
Block a user