init: init refactor
This commit is contained in:
Executable
+10
@@ -0,0 +1,10 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/zrpc"
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
DB struct {
|
||||
DataSource string
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.10.1
|
||||
// Source: system.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/system/rpc/internal/logic"
|
||||
"sundynix-micro-go/app/system/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/system/rpc/system"
|
||||
)
|
||||
|
||||
type SystemServiceServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
system.UnimplementedSystemServiceServer
|
||||
}
|
||||
|
||||
func NewSystemServiceServer(svcCtx *svc.ServiceContext) *SystemServiceServer {
|
||||
return &SystemServiceServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 获取用户角色列表
|
||||
func (s *SystemServiceServer) GetRolesByUserId(ctx context.Context, in *system.GetRolesByUserIdReq) (*system.GetRolesByUserIdResp, error) {
|
||||
l := logic.NewGetRolesByUserIdLogic(ctx, s.svcCtx)
|
||||
return l.GetRolesByUserId(in)
|
||||
}
|
||||
|
||||
// 获取角色菜单列表
|
||||
func (s *SystemServiceServer) GetMenusByRoleId(ctx context.Context, in *system.GetMenusByRoleIdReq) (*system.GetMenusByRoleIdResp, error) {
|
||||
l := logic.NewGetMenusByRoleIdLogic(ctx, s.svcCtx)
|
||||
return l.GetMenusByRoleId(in)
|
||||
}
|
||||
|
||||
// 获取客户端信息
|
||||
func (s *SystemServiceServer) GetClientById(ctx context.Context, in *system.GetClientByIdReq) (*system.GetClientByIdResp, error) {
|
||||
l := logic.NewGetClientByIdLogic(ctx, s.svcCtx)
|
||||
return l.GetClientById(in)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
sysModel "sundynix-micro-go/app/system/model"
|
||||
"sundynix-micro-go/app/system/rpc/internal/config"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
db, err := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{})
|
||||
if err != nil {
|
||||
logx.Errorf("连接数据库失败: %v", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 自动迁移
|
||||
if err := db.AutoMigrate(
|
||||
&sysModel.SundynixClient{},
|
||||
&sysModel.SundynixRole{},
|
||||
&sysModel.SundynixMenu{},
|
||||
&sysModel.SundynixRoleMenu{},
|
||||
&sysModel.SundynixOperationRecord{},
|
||||
&sysModel.SundynixDict{},
|
||||
); err != nil {
|
||||
logx.Errorf("数据库迁移失败: %v", err)
|
||||
}
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user