147 lines
7.6 KiB
Go
147 lines
7.6 KiB
Go
package service
|
|
|
|
import (
|
|
"photowall/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type MenuService struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewMenuService(db *gorm.DB) *MenuService {
|
|
return &MenuService{db: db}
|
|
}
|
|
|
|
// defaultMenus 默认菜单数据
|
|
var defaultMenus = []model.Menu{
|
|
// 首页
|
|
{ID: 1, ParentID: 0, Name: "Home", Path: "/", Component: "home/index", Title: "首页", Icon: "fas fa-home", Sort: 1, Visible: true, RequireAuth: false, RequireAdmin: false},
|
|
// 班级管理
|
|
{ID: 2, ParentID: 0, Name: "Class", Path: "/class", Component: "", Title: "班级管理", Icon: "fas fa-users", Sort: 2, Visible: true, RequireAuth: false, RequireAdmin: false, Redirect: "/class/list"},
|
|
{ID: 3, ParentID: 2, Name: "ClassList", Path: "/class/list", Component: "class/list/index", Title: "班级列表", Icon: "fas fa-list", Sort: 1, Visible: true, RequireAuth: false, RequireAdmin: false},
|
|
{ID: 4, ParentID: 2, Name: "ClassEdit", Path: "/class/edit", Component: "class/edit/index", Title: "创建班级", Icon: "fas fa-plus", Sort: 2, Visible: true, RequireAuth: true, RequireAdmin: false},
|
|
{ID: 5, ParentID: 2, Name: "ClassJoin", Path: "/class/join", Component: "class/join/index", Title: "加入班级", Icon: "fas fa-sign-in-alt", Sort: 3, Visible: true, RequireAuth: true, RequireAdmin: false},
|
|
{ID: 6, ParentID: 2, Name: "ClassDetail", Path: "/class/detail/:id", Component: "class/detail/index", Title: "班级详情", Icon: "", Sort: 4, Visible: false, RequireAuth: false, RequireAdmin: false},
|
|
// 个人中心
|
|
{ID: 7, ParentID: 0, Name: "Profile", Path: "/profile", Component: "normalsettings/usersettings/detail/index", Title: "个人中心", Icon: "fas fa-user", Sort: 3, Visible: true, RequireAuth: true, RequireAdmin: false},
|
|
// 系统管理(管理员)
|
|
{ID: 8, ParentID: 0, Name: "Admin", Path: "/admin", Component: "", Title: "系统管理", Icon: "fas fa-cog", Sort: 4, Visible: true, RequireAuth: true, RequireAdmin: true, Redirect: "/admin/dashboard"},
|
|
{ID: 18, ParentID: 8, Name: "Dashboard", Path: "/admin/dashboard", Component: "admin/dashboard/index", Title: "数据统计", Icon: "fas fa-chart-line", Sort: 1, Visible: true, RequireAuth: true, RequireAdmin: true},
|
|
{ID: 9, ParentID: 8, Name: "AdminAudit", Path: "/admin/audit", Component: "admin/audit/list/index", Title: "审核管理", Icon: "fas fa-clipboard-check", Sort: 2, Visible: true, RequireAuth: true, RequireAdmin: true},
|
|
{ID: 10, ParentID: 8, Name: "AdminAuditDetail", Path: "/admin/audit/detail/:id", Component: "admin/audit/detail/index", Title: "审核详情", Icon: "", Sort: 3, Visible: false, RequireAuth: true, RequireAdmin: true},
|
|
{ID: 21, ParentID: 8, Name: "AdminBaseData", Path: "/admin/basedata", Component: "admin/basedata/index", Title: "基础数据", Icon: "fas fa-database", Sort: 4, Visible: true, RequireAuth: true, RequireAdmin: true},
|
|
{ID: 11, ParentID: 8, Name: "AdminConfig", Path: "/admin/config", Component: "admin/config/index", Title: "系统配置", Icon: "fas fa-sliders-h", Sort: 5, Visible: true, RequireAuth: true, RequireAdmin: true},
|
|
{ID: 12, ParentID: 8, Name: "AdminMenu", Path: "/admin/menu", Component: "admin/menu/index", Title: "菜单管理", Icon: "fas fa-bars", Sort: 6, Visible: true, RequireAuth: true, RequireAdmin: true},
|
|
{ID: 19, ParentID: 8, Name: "UserManage", Path: "/admin/users", Component: "admin/users/index", Title: "用户管理", Icon: "fas fa-user-shield", Sort: 7, Visible: true, RequireAuth: true, RequireAdmin: true},
|
|
{ID: 20, ParentID: 8, Name: "SensitiveWord", Path: "/admin/sensitive", Component: "admin/sensitive/index", Title: "敏感词管理", Icon: "fas fa-shield-alt", Sort: 8, Visible: true, RequireAuth: true, RequireAdmin: true},
|
|
// 基本设置(管理员)
|
|
{ID: 13, ParentID: 0, Name: "NormalSettings", Path: "/normalsettings", Component: "", Title: "基本设置", Icon: "fas fa-wrench", Sort: 5, Visible: true, RequireAuth: true, RequireAdmin: true, Redirect: "/normalsettings/usersettings"},
|
|
{ID: 14, ParentID: 13, Name: "UserSettings", Path: "/normalsettings/usersettings", Component: "normalsettings/usersettings/list/index", Title: "用户设置", Icon: "fas fa-user-cog", Sort: 1, Visible: true, RequireAuth: true, RequireAdmin: true},
|
|
{ID: 15, ParentID: 13, Name: "RoleSettings", Path: "/normalsettings/rolesettings", Component: "normalsettings/rolesettings/list/index", Title: "角色设置", Icon: "fas fa-user-tag", Sort: 2, Visible: true, RequireAuth: true, RequireAdmin: true},
|
|
{ID: 16, ParentID: 13, Name: "LogManagement", Path: "/normalsettings/logmanagement", Component: "normalsettings/logmanagement/list/index", Title: "日志管理", Icon: "fas fa-file-alt", Sort: 3, Visible: true, RequireAuth: true, RequireAdmin: true},
|
|
// 登录页(不显示在菜单)
|
|
{ID: 17, ParentID: 0, Name: "Login", Path: "/login", Component: "auth/login/index", Title: "登录", Icon: "", Sort: 99, Visible: false, RequireAuth: false, RequireAdmin: false},
|
|
}
|
|
|
|
// InitDefaults 初始化默认菜单(不存在则创建)
|
|
func (s *MenuService) InitDefaults() error {
|
|
for _, m := range defaultMenus {
|
|
var existing model.Menu
|
|
err := s.db.Where("id = ? OR name = ?", m.ID, m.Name).First(&existing).Error
|
|
if err != nil {
|
|
if err := s.db.Select("*").Create(&m).Error; err != nil {
|
|
return err
|
|
}
|
|
} else if !m.Visible && existing.Visible {
|
|
// 默认隐藏菜单(如详情页带 :id 参数、登录页)在库中若是显示状态,自动校正为隐藏
|
|
s.db.Model(&existing).Update("visible", false)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetMenuTree 获取菜单树(isAdmin 过滤管理员菜单)
|
|
func (s *MenuService) GetMenuTree(isAdmin bool) ([]*model.MenuTree, error) {
|
|
var all []model.Menu
|
|
query := s.db.Order("parent_id, sort")
|
|
if !isAdmin {
|
|
query = query.Where("require_admin = ?", false)
|
|
}
|
|
if err := query.Find(&all).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return buildTree(all, 0), nil
|
|
}
|
|
|
|
// GetAll 获取所有菜单(管理用,含隐藏)
|
|
func (s *MenuService) GetAll() ([]model.Menu, error) {
|
|
var list []model.Menu
|
|
err := s.db.Order("sort, id").Find(&list).Error
|
|
return list, err
|
|
}
|
|
|
|
// GetAllTree 获取所有菜单树结构(管理用,含隐藏)
|
|
func (s *MenuService) GetAllTree() ([]*model.MenuTree, error) {
|
|
var all []model.Menu
|
|
if err := s.db.Order("sort, id").Find(&all).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return buildTree(all, 0), nil
|
|
}
|
|
|
|
// GetByID 获取单个菜单
|
|
func (s *MenuService) GetByID(id uint) (*model.Menu, error) {
|
|
var m model.Menu
|
|
if err := s.db.First(&m, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &m, nil
|
|
}
|
|
|
|
// Create 创建菜单
|
|
func (s *MenuService) Create(m *model.Menu) error {
|
|
return s.db.Create(m).Error
|
|
}
|
|
|
|
// Update 更新菜单
|
|
func (s *MenuService) Update(m *model.Menu) error {
|
|
return s.db.Save(m).Error
|
|
}
|
|
|
|
// UpdateFields 更新菜单指定字段
|
|
func (s *MenuService) UpdateFields(id uint, updates map[string]interface{}) error {
|
|
return s.db.Model(&model.Menu{}).Where("id = ?", id).Updates(updates).Error
|
|
}
|
|
|
|
// Delete 删除菜单(同时删除子菜单)
|
|
func (s *MenuService) Delete(id uint) error {
|
|
return s.db.Transaction(func(tx *gorm.DB) error {
|
|
// 递归删除子菜单
|
|
var children []model.Menu
|
|
tx.Where("parent_id = ?", id).Find(&children)
|
|
for _, child := range children {
|
|
if err := tx.Delete(&child).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.Delete(&model.Menu{}, id).Error
|
|
})
|
|
}
|
|
|
|
// buildTree 构建菜单树
|
|
func buildTree(list []model.Menu, parentID uint) []*model.MenuTree {
|
|
var tree []*model.MenuTree
|
|
for i := range list {
|
|
if list[i].ParentID == parentID {
|
|
node := &model.MenuTree{
|
|
Menu: list[i],
|
|
Children: buildTree(list, list[i].ID),
|
|
}
|
|
tree = append(tree, node)
|
|
}
|
|
}
|
|
return tree
|
|
}
|