122 lines
7.1 KiB
Go
122 lines
7.1 KiB
Go
package system
|
||
|
||
import (
|
||
"context"
|
||
|
||
. "git.echol.cn/loser/Go-Web-Template/server/model/system"
|
||
"git.echol.cn/loser/Go-Web-Template/server/service/system"
|
||
"github.com/pkg/errors"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
const initOrderMenu = initOrderAuthority + 1
|
||
|
||
type initMenu struct{}
|
||
|
||
// auto run
|
||
func init() {
|
||
system.RegisterInit(initOrderMenu, &initMenu{})
|
||
}
|
||
|
||
func (i *initMenu) InitializerName() string {
|
||
return SysBaseMenu{}.TableName()
|
||
}
|
||
|
||
func (i *initMenu) MigrateTable(ctx context.Context) (context.Context, error) {
|
||
db, ok := ctx.Value("db").(*gorm.DB)
|
||
if !ok {
|
||
return ctx, system.ErrMissingDBContext
|
||
}
|
||
return ctx, db.AutoMigrate(
|
||
&SysBaseMenu{},
|
||
&SysBaseMenuParameter{},
|
||
&SysBaseMenuBtn{},
|
||
)
|
||
}
|
||
|
||
func (i *initMenu) TableCreated(ctx context.Context) bool {
|
||
db, ok := ctx.Value("db").(*gorm.DB)
|
||
if !ok {
|
||
return false
|
||
}
|
||
m := db.Migrator()
|
||
return m.HasTable(&SysBaseMenu{}) &&
|
||
m.HasTable(&SysBaseMenuParameter{}) &&
|
||
m.HasTable(&SysBaseMenuBtn{})
|
||
}
|
||
|
||
func (i *initMenu) InitializeData(ctx context.Context) (next context.Context, err error) {
|
||
db, ok := ctx.Value("db").(*gorm.DB)
|
||
if !ok {
|
||
return ctx, system.ErrMissingDBContext
|
||
}
|
||
|
||
// 定义所有菜单
|
||
allMenus := []SysBaseMenu{
|
||
{MenuLevel: 0, Hidden: false, ParentId: 0, Path: "dashboard", Name: "dashboard", Component: "features/dashboard/DashboardPage", Sort: 1, Meta: Meta{Title: "仪表盘", Icon: "DashboardOutlined"}},
|
||
{MenuLevel: 0, Hidden: false, ParentId: 0, Path: "about", Name: "about", Component: "features/discovery/ModuleLandingPage:about", Sort: 9, Meta: Meta{Title: "关于系统", Icon: "InfoCircleOutlined"}},
|
||
{MenuLevel: 0, Hidden: false, ParentId: 0, Path: "admin", Name: "superAdmin", Component: "features/discovery/ModuleLandingPage:superAdmin", Sort: 3, Meta: Meta{Title: "超级管理员", Icon: "LockOutlined"}},
|
||
{MenuLevel: 0, Hidden: true, ParentId: 0, Path: "person", Name: "person", Component: "features/person/ProfilePage", Sort: 4, Meta: Meta{Title: "个人信息", Icon: "UserOutlined"}},
|
||
{MenuLevel: 0, Hidden: false, ParentId: 0, Path: "common", Name: "common", Component: "features/discovery/ModuleLandingPage:common", Sort: 6, Meta: Meta{Title: "公共能力", Icon: "AppstoreOutlined"}},
|
||
{MenuLevel: 0, Hidden: false, ParentId: 0, Path: "systemTools", Name: "systemTools", Component: "features/discovery/ModuleLandingPage:systemTools", Sort: 5, Meta: Meta{Title: "编程辅助", Icon: "ToolOutlined"}},
|
||
{MenuLevel: 0, Hidden: false, ParentId: 0, Path: "https://echol.cn", Name: "https://echol.cn", Component: "/", Sort: 0, Meta: Meta{Title: "官方网站", Icon: "customer-gva"}},
|
||
{MenuLevel: 0, Hidden: false, ParentId: 0, Path: "state", Name: "state", Component: "features/server/ServerStatePage", Sort: 8, Meta: Meta{Title: "服务器状态", Icon: "CloudServerOutlined"}},
|
||
}
|
||
|
||
// 先创建父级菜单(ParentId = 0 的菜单)
|
||
if err = db.Create(&allMenus).Error; err != nil {
|
||
return ctx, errors.Wrap(err, SysBaseMenu{}.TableName()+"父级菜单初始化失败!")
|
||
}
|
||
|
||
// 建立菜单映射 - 通过Name查找已创建的菜单及其ID
|
||
menuNameMap := make(map[string]uint)
|
||
for _, menu := range allMenus {
|
||
menuNameMap[menu.Name] = menu.ID
|
||
}
|
||
|
||
// 定义子菜单,并设置正确的ParentId
|
||
childMenus := []SysBaseMenu{
|
||
// superAdmin子菜单
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["superAdmin"], Path: "authority", Name: "authority", Component: "features/roles/RoleManagementPage", Sort: 1, Meta: Meta{Title: "角色管理", Icon: "TeamOutlined"}},
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["superAdmin"], Path: "menu", Name: "menu", Component: "features/menus/MenuManagementPage", Sort: 2, Meta: Meta{Title: "菜单管理", Icon: "AppstoreOutlined", KeepAlive: true}},
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["superAdmin"], Path: "api", Name: "api", Component: "features/apis/ApiManagementPage", Sort: 3, Meta: Meta{Title: "API管理", Icon: "ApiOutlined", KeepAlive: true}},
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["superAdmin"], Path: "user", Name: "user", Component: "features/users/UserManagementPage", Sort: 4, Meta: Meta{Title: "用户管理", Icon: "UserOutlined"}},
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["superAdmin"], Path: "dictionary", Name: "dictionary", Component: "features/dictionaries/DictionaryManagementPage", Sort: 5, Meta: Meta{Title: "字典管理", Icon: "BookOutlined"}},
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["superAdmin"], Path: "operation", Name: "operation", Component: "features/logs/OperationLogPage", Sort: 6, Meta: Meta{Title: "操作历史", Icon: "ProfileOutlined"}},
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["superAdmin"], Path: "sysParams", Name: "sysParams", Component: "features/params/ParamsManagementPage", Sort: 7, Meta: Meta{Title: "参数管理", Icon: "SettingOutlined"}},
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["superAdmin"], Path: "system", Name: "system", Component: "features/system/SystemConfigPage", Sort: 8, Meta: Meta{Title: "系统配置", Icon: "SettingOutlined"}},
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["superAdmin"], Path: "apiToken", Name: "apiToken", Component: "features/tokens/ApiTokenPage", Sort: 9, Meta: Meta{Title: "API Token", Icon: "LockOutlined"}},
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["superAdmin"], Path: "loginLog", Name: "loginLog", Component: "features/logs/LoginLogPage", Sort: 10, Meta: Meta{Title: "登录日志", Icon: "FileTextOutlined"}},
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["superAdmin"], Path: "sysError", Name: "sysError", Component: "features/errors/ErrorLogPage", Sort: 11, Meta: Meta{Title: "错误日志", Icon: "BugOutlined"}},
|
||
|
||
// common子菜单
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["common"], Path: "upload", Name: "upload", Component: "features/media/MediaLibraryPage", Sort: 1, Meta: Meta{Title: "媒体库(上传下载)", Icon: "UploadOutlined"}},
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["common"], Path: "breakpoint", Name: "breakpoint", Component: "features/breakpoint/BreakpointPage", Sort: 2, Meta: Meta{Title: "断点续传", Icon: "DeploymentUnitOutlined"}},
|
||
|
||
// systemTools子菜单
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["systemTools"], Path: "mcpTest", Name: "mcpTest", Component: "features/mcp/McpTestPage", Sort: 1, Meta: Meta{Title: "MCP Tools管理", Icon: "ToolOutlined"}},
|
||
{MenuLevel: 1, Hidden: false, ParentId: menuNameMap["systemTools"], Path: "mcpTool", Name: "mcpTool", Component: "features/mcp/McpToolPage", Sort: 2, Meta: Meta{Title: "MCP Tools模板", Icon: "ToolOutlined"}},
|
||
}
|
||
|
||
// 创建子菜单
|
||
if err = db.Create(&childMenus).Error; err != nil {
|
||
return ctx, errors.Wrap(err, SysBaseMenu{}.TableName()+"子菜单初始化失败!")
|
||
}
|
||
|
||
// 组合所有菜单作为返回结果
|
||
allEntities := append(allMenus, childMenus...)
|
||
next = context.WithValue(ctx, i.InitializerName(), allEntities)
|
||
return next, nil
|
||
}
|
||
|
||
func (i *initMenu) DataInserted(ctx context.Context) bool {
|
||
db, ok := ctx.Value("db").(*gorm.DB)
|
||
if !ok {
|
||
return false
|
||
}
|
||
if errors.Is(db.Where("path = ?", "upload").First(&SysBaseMenu{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
|
||
return false
|
||
}
|
||
return true
|
||
}
|