init: init refactor
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
sysModel "sundynix-micro-go/app/system/model"
|
||||
"sundynix-micro-go/app/system/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GetClientByIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetClientByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetClientByIdLogic {
|
||||
return &GetClientByIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 获取客户端信息
|
||||
func (l *GetClientByIdLogic) GetClientById(in *system.GetClientByIdReq) (*system.GetClientByIdResp, error) {
|
||||
var client sysModel.SundynixClient
|
||||
err := l.svcCtx.DB.Where("client_id = ?", in.ClientId).First(&client).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 &system.GetClientByIdResp{
|
||||
Client: &system.ClientInfo{
|
||||
Id: client.ID,
|
||||
ClientId: client.ClientID,
|
||||
Name: client.Name,
|
||||
GrantType: client.GrantType,
|
||||
AdditionalInfo: client.AdditionalInfo,
|
||||
ActiveTimeout: client.ActiveTimeout,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
sysModel "sundynix-micro-go/app/system/model"
|
||||
"sundynix-micro-go/app/system/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type GetMenusByRoleIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetMenusByRoleIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMenusByRoleIdLogic {
|
||||
return &GetMenusByRoleIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 获取角色菜单列表
|
||||
func (l *GetMenusByRoleIdLogic) GetMenusByRoleId(in *system.GetMenusByRoleIdReq) (*system.GetMenusByRoleIdResp, error) {
|
||||
// 查询角色菜单关联
|
||||
var roleMenus []sysModel.SundynixRoleMenu
|
||||
if err := l.svcCtx.DB.Where("role_id = ?", in.RoleId).Find(&roleMenus).Error; err != nil {
|
||||
l.Errorf("查询角色菜单失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询角色菜单失败")
|
||||
}
|
||||
|
||||
if len(roleMenus) == 0 {
|
||||
return &system.GetMenusByRoleIdResp{Menus: []*system.MenuInfo{}}, nil
|
||||
}
|
||||
|
||||
menuIds := make([]string, 0, len(roleMenus))
|
||||
for _, rm := range roleMenus {
|
||||
menuIds = append(menuIds, rm.MenuID)
|
||||
}
|
||||
|
||||
// 查询菜单信息
|
||||
var menus []sysModel.SundynixMenu
|
||||
if err := l.svcCtx.DB.Where("id IN ?", menuIds).Order("sort").Find(&menus).Error; err != nil {
|
||||
l.Errorf("查询菜单信息失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询菜单信息失败")
|
||||
}
|
||||
|
||||
// 构建树形结构
|
||||
menuMap := make(map[string]*system.MenuInfo)
|
||||
var rootMenus []*system.MenuInfo
|
||||
|
||||
for _, m := range menus {
|
||||
menuInfo := &system.MenuInfo{
|
||||
Id: m.ID,
|
||||
ParentId: m.ParentID,
|
||||
Category: int32(m.Category),
|
||||
Name: m.Name,
|
||||
Title: m.Title,
|
||||
Code: m.Code,
|
||||
Path: m.Path,
|
||||
Permission: m.Permission,
|
||||
Locale: m.Locale,
|
||||
Icon: m.Icon,
|
||||
Sort: int32(m.Sort),
|
||||
Children: []*system.MenuInfo{},
|
||||
}
|
||||
menuMap[m.ID] = menuInfo
|
||||
}
|
||||
|
||||
for _, menuInfo := range menuMap {
|
||||
if menuInfo.ParentId == "0" || menuInfo.ParentId == "" {
|
||||
rootMenus = append(rootMenus, menuInfo)
|
||||
} else {
|
||||
if parent, ok := menuMap[menuInfo.ParentId]; ok {
|
||||
parent.Children = append(parent.Children, menuInfo)
|
||||
} else {
|
||||
rootMenus = append(rootMenus, menuInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &system.GetMenusByRoleIdResp{Menus: rootMenus}, nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
sysModel "sundynix-micro-go/app/system/model"
|
||||
"sundynix-micro-go/app/system/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type GetRolesByUserIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetRolesByUserIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRolesByUserIdLogic {
|
||||
return &GetRolesByUserIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 获取用户角色列表
|
||||
func (l *GetRolesByUserIdLogic) GetRolesByUserId(in *system.GetRolesByUserIdReq) (*system.GetRolesByUserIdResp, error) {
|
||||
// 查询用户角色关联
|
||||
var userRoles []sysModel.SundynixUserRole
|
||||
if err := l.svcCtx.DB.Where("user_id = ?", in.UserId).Find(&userRoles).Error; err != nil {
|
||||
l.Errorf("查询用户角色失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询用户角色失败")
|
||||
}
|
||||
|
||||
if len(userRoles) == 0 {
|
||||
return &system.GetRolesByUserIdResp{Roles: []*system.RoleInfo{}}, nil
|
||||
}
|
||||
|
||||
roleIds := make([]string, 0, len(userRoles))
|
||||
for _, ur := range userRoles {
|
||||
roleIds = append(roleIds, ur.RoleID)
|
||||
}
|
||||
|
||||
var roles []sysModel.SundynixRole
|
||||
if err := l.svcCtx.DB.Where("id IN ?", roleIds).Find(&roles).Error; err != nil {
|
||||
l.Errorf("查询角色信息失败: %v", err)
|
||||
return nil, status.Error(codes.Internal, "查询角色信息失败")
|
||||
}
|
||||
|
||||
roleInfos := make([]*system.RoleInfo, 0, len(roles))
|
||||
for _, r := range roles {
|
||||
roleInfos = append(roleInfos, &system.RoleInfo{
|
||||
Id: r.ID,
|
||||
Name: r.Name,
|
||||
Code: r.Code,
|
||||
Sort: int32(r.Sort),
|
||||
})
|
||||
}
|
||||
|
||||
return &system.GetRolesByUserIdResp{Roles: roleInfos}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user