|
|
package model
|
|
|
|
|
|
import (
|
|
|
"mygo/utils"
|
|
|
"reflect"
|
|
|
"time"
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
)
|
|
|
|
|
|
type Route struct {
|
|
|
ID string `json:"id" gorm:"column:id; primaryKey;autoIncrement:false"`
|
|
|
ParentId string `json:"parent_id" gorm:"comment:'父级ID'"`
|
|
|
Name string `json:"name" gorm:"comment:'名称'"`
|
|
|
Icon string `json:"icon" gorm:"comment:'图标'"`
|
|
|
Sort int `json:"sort" gorm:"comment:'排序'"`
|
|
|
Component string `json:"component" gorm:"comment:'组件'"`
|
|
|
Redirect string `json:"redirect" gorm:"comment:'一级菜单跳转地址'"`
|
|
|
Perms string `json:"perms" gorm:"comment:'权限编码'"`
|
|
|
PermsType int `json:"perms_type" gorm:"column:perms_type;default:0;comment:'权限策略 1仅显示 2可编辑';force"`
|
|
|
BtnStatus int `json:"btn_status" gorm:"column:btn_status;default:0;comment:'按钮权限状态 0:无效 1:有效';force"`
|
|
|
Title string `json:"title" gorm:"comment:'菜单标题'"`
|
|
|
Path string `json:"path" gorm:"comment:'路由规则'"`
|
|
|
MenuType int `json:"menu_type" gorm:"column:menu_type;default:0;comment:'类型 0:一级菜单 1:子菜单 2:按钮/权限';force"`
|
|
|
HideInBread int `json:"hideInBread" gorm:"column:hide_inbread;default:0;comment:'是否面包屑显示';force"`
|
|
|
HideInMenu int `json:"hideInMenu" gorm:"column:hide_inmenu;default:0;comment:'隐藏菜单0否 1是';force"`
|
|
|
NotCache int `json:"notCache" gorm:"column:not_cache;default:0;comment:'是否缓存0否 1是';force"`
|
|
|
IsMmin int `json:"isMmin" gorm:"column:is_mmin;default:0;comment:'是否子应用0否 1是';force"`
|
|
|
CreatedAt time.Time `json:"created_at" gorm:"column:created_at;comment:'创建时间'"`
|
|
|
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at;comment:'修改时间'"`
|
|
|
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"column:deleted_at;comment:'删除时间'"`
|
|
|
}
|
|
|
|
|
|
type MyMenu struct {
|
|
|
Menu
|
|
|
Children []MyMenu `json:"children"`
|
|
|
}
|
|
|
|
|
|
type Menu struct {
|
|
|
ID string `json:"id"`
|
|
|
ParentId string `json:"parent_id"`
|
|
|
Name string `json:"name"`
|
|
|
Icon string `json:"icon"`
|
|
|
Sort int `json:"sort"`
|
|
|
Component string `json:"component"`
|
|
|
Redirect string `json:"redirect"`
|
|
|
Perms string `json:"perms"`
|
|
|
PermsType int `json:"perms_type"`
|
|
|
Title string `json:"title"`
|
|
|
Path string `json:"path"`
|
|
|
MenuType int `json:"menu_type"`
|
|
|
BtnStatus int `json:"btn_status"`
|
|
|
IsChoose int `json:"is_choose"`
|
|
|
Meta Meta `json:"meta"`
|
|
|
}
|
|
|
|
|
|
type Meta struct {
|
|
|
Title string `json:"title"`
|
|
|
Icon string `json:"icon"`
|
|
|
HideInBread bool `json:"hideInBread"`
|
|
|
HideInMenu bool `json:"hideInMenu"`
|
|
|
NotCache bool `json:"notCache"`
|
|
|
IsMmin bool `json:"isMmin"`
|
|
|
}
|
|
|
type Routes struct {
|
|
|
MyRoute
|
|
|
Children []Routes `json:"children"`
|
|
|
}
|
|
|
type MyRoute struct {
|
|
|
ID string `json:"id"`
|
|
|
ParentId string `json:"parent_id"`
|
|
|
Name string `json:"name"`
|
|
|
Icon string `json:"icon"`
|
|
|
Sort int `json:"sort"`
|
|
|
Component string `json:"component"`
|
|
|
Redirect string `json:"redirect"`
|
|
|
Perms string `json:"perms"`
|
|
|
PermsType int `json:"perms_type"`
|
|
|
Title string `json:"title"`
|
|
|
Path string `json:"path"`
|
|
|
MenuType int `json:"menu_type"`
|
|
|
BtnStatus int `json:"btn_status"`
|
|
|
IsChoose int `json:"is_choose"`
|
|
|
HideInBread int `json:"hideInBread"`
|
|
|
HideInMenu int `json:"hideInMenu"`
|
|
|
NotCache int `json:"notCache"`
|
|
|
IsMmin int `json:"isMmin"`
|
|
|
}
|
|
|
|
|
|
type RoleRoutes struct {
|
|
|
MyRoleRoute
|
|
|
Children []RoleRoutes `json:"children"`
|
|
|
}
|
|
|
|
|
|
type MyRoleRoute struct {
|
|
|
ID string `json:"id"`
|
|
|
ParentId string `json:"parent_id"`
|
|
|
Title string `json:"title"`
|
|
|
Checked int `json:"checked"`
|
|
|
}
|
|
|
|
|
|
func (Route) TableName() string {
|
|
|
return "sys_auth_route"
|
|
|
}
|
|
|
|
|
|
func GetRouteById(db *gorm.DB, id string) (Route, error) {
|
|
|
r := Route{}
|
|
|
err := db.Where("id = ?", id).First(&r).Error
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
|
return r, err
|
|
|
}
|
|
|
return r, nil
|
|
|
}
|
|
|
|
|
|
//获取用户路由权限
|
|
|
func GetMyRoutes(db *gorm.DB, ids []string) ([]MyMenu, error) {
|
|
|
routes := []Route{}
|
|
|
|
|
|
// 当ids > 0 即获取用户角色的菜单权限
|
|
|
// 当ids 为空 则 获取系统下权限不菜单
|
|
|
|
|
|
if len(ids) > 0 {
|
|
|
err := db.Where("id in (?) and menu_type != ?", ids, 2).Order("sort asc").Find(&routes).Error
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
|
return nil, err
|
|
|
}
|
|
|
} else {
|
|
|
err := db.Where("1 = 1").Order("sort asc").Find(&routes).Error
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
|
|
|
menus := []MyMenu{}
|
|
|
for _, v := range routes {
|
|
|
|
|
|
meta := Meta{
|
|
|
Title: v.Title,
|
|
|
Icon: v.Icon,
|
|
|
HideInBread: utils.Itob(v.HideInBread),
|
|
|
HideInMenu: utils.Itob(v.HideInMenu),
|
|
|
NotCache: utils.Itob(v.NotCache),
|
|
|
IsMmin: utils.Itob(v.IsMmin),
|
|
|
}
|
|
|
menu := Menu{
|
|
|
ID: v.ID,
|
|
|
ParentId: v.ParentId,
|
|
|
Name: v.Name,
|
|
|
Icon: v.Icon,
|
|
|
Sort: v.Sort,
|
|
|
Component: v.Component,
|
|
|
Redirect: v.Redirect,
|
|
|
Perms: v.Perms,
|
|
|
PermsType: v.PermsType,
|
|
|
Title: v.Title,
|
|
|
Path: v.Path,
|
|
|
MenuType: v.MenuType,
|
|
|
BtnStatus: v.BtnStatus,
|
|
|
IsChoose: 1,
|
|
|
Meta: meta,
|
|
|
}
|
|
|
|
|
|
mymenu := MyMenu{}
|
|
|
mymenu.Menu = menu
|
|
|
mymenu.Children = []MyMenu{}
|
|
|
|
|
|
menus = append(menus, mymenu)
|
|
|
}
|
|
|
|
|
|
return ToTree(menus, ""), nil
|
|
|
}
|
|
|
|
|
|
func GetRouteList(db *gorm.DB) ([]Routes, error) {
|
|
|
routes := []Route{}
|
|
|
err := db.Where("1 = 1").Order("sort asc").Find(&routes).Error
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
menus := []Routes{}
|
|
|
for _, v := range routes {
|
|
|
menu := MyRoute{
|
|
|
ID: v.ID,
|
|
|
ParentId: v.ParentId,
|
|
|
Name: v.Name,
|
|
|
Icon: v.Icon,
|
|
|
Sort: v.Sort,
|
|
|
Component: v.Component,
|
|
|
Redirect: v.Redirect,
|
|
|
Perms: v.Perms,
|
|
|
PermsType: v.PermsType,
|
|
|
Title: v.Title,
|
|
|
Path: v.Path,
|
|
|
MenuType: v.MenuType,
|
|
|
BtnStatus: v.BtnStatus,
|
|
|
HideInBread: v.HideInBread,
|
|
|
HideInMenu: v.HideInMenu,
|
|
|
NotCache: v.NotCache,
|
|
|
IsMmin: v.IsMmin,
|
|
|
}
|
|
|
|
|
|
mymenu := Routes{}
|
|
|
mymenu.MyRoute = menu
|
|
|
mymenu.Children = []Routes{}
|
|
|
|
|
|
menus = append(menus, mymenu)
|
|
|
}
|
|
|
|
|
|
return RoutesToTree(menus, ""), nil
|
|
|
}
|
|
|
|
|
|
func ToTree(menus []MyMenu, pid string) []MyMenu {
|
|
|
nodes := []MyMenu{}
|
|
|
|
|
|
if reflect.ValueOf(menus).IsValid() {
|
|
|
for _, v := range menus {
|
|
|
if v.ParentId == pid {
|
|
|
v.Children = append(v.Children, ToTree(menus, v.ID)...)
|
|
|
nodes = append(nodes, v)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return nodes
|
|
|
}
|
|
|
|
|
|
func RoutesToTree(menus []Routes, pid string) []Routes {
|
|
|
nodes := []Routes{}
|
|
|
|
|
|
if reflect.ValueOf(menus).IsValid() {
|
|
|
for _, v := range menus {
|
|
|
if v.ParentId == pid {
|
|
|
v.Children = append(v.Children, RoutesToTree(menus, v.ID)...)
|
|
|
nodes = append(nodes, v)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return nodes
|
|
|
}
|
|
|
|
|
|
func RoleRoutesToTree(menus []RoleRoutes, pid string) []RoleRoutes {
|
|
|
nodes := []RoleRoutes{}
|
|
|
|
|
|
if reflect.ValueOf(menus).IsValid() {
|
|
|
for _, v := range menus {
|
|
|
if v.ParentId == pid {
|
|
|
v.Children = append(v.Children, RoleRoutesToTree(menus, v.ID)...)
|
|
|
nodes = append(nodes, v)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return nodes
|
|
|
}
|
|
|
|
|
|
//获取角色菜单列表
|
|
|
func GetRoleRoutes(db *gorm.DB, ids []string) ([]RoleRoutes, error) {
|
|
|
routes := []Route{}
|
|
|
err := db.Where("1 = 1").Order("sort asc").Find(&routes).Error
|
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
choose := func(id string) int {
|
|
|
for _, v := range ids {
|
|
|
if v == id {
|
|
|
return 1
|
|
|
}
|
|
|
}
|
|
|
return 0
|
|
|
}
|
|
|
|
|
|
menus := []RoleRoutes{}
|
|
|
for _, v := range routes {
|
|
|
|
|
|
menu := MyRoleRoute{
|
|
|
ID: v.ID,
|
|
|
ParentId: v.ParentId,
|
|
|
Title: v.Title,
|
|
|
Checked: choose(v.ID),
|
|
|
}
|
|
|
|
|
|
mymenu := RoleRoutes{}
|
|
|
mymenu.MyRoleRoute = menu
|
|
|
mymenu.Children = []RoleRoutes{}
|
|
|
|
|
|
menus = append(menus, mymenu)
|
|
|
}
|
|
|
|
|
|
return RoleRoutesToTree(menus, ""), nil
|
|
|
}
|