package models import ( "fmt" "strings" "time" "github.com/beego/beego/v2/client/orm" ) type SysMenu struct { SysMenuId int `orm:"pk" description:"菜单id"` ParentId int `description:"父级id"` Sort int `description:"排序id"` ButtonCode string `description:"按钮唯一编码"` Name string `description:"按钮名称"` Path string `description:"路径"` Api string `description:"api接口"` IconPath string `description:"图标路径"` Level int `description:"层级"` MenuType int `description:"菜单类型,1:菜单,2:按钮"` CreateTime time.Time `description:"创建时间"` ModifyTime time.Time `description:"修改时间"` } type SysRoleMenuMapping struct { SysRoleMenuMappingId int `orm:"pk" description:"id"` SysRoleId int `description:"角色id"` SysMenuId int `description:"菜单id"` Type int `description:"类型"` } // SysMenuItem 角色菜单 type SysMenuItem struct { SysMenuId int ParentId int `description:"父级菜单ID"` Name string `description:"菜单名称或者按钮名称"` Level int `description:"层级"` Sort int `description:"排序"` Path string `description:"路由地址"` IconPath string `description:"菜单图标地址"` ButtonCode string `description:"按钮/菜单唯一标识"` Children []*SysMenuItem `description:"子菜单"` } // GetMenuButtonsByRoleId 获取角色按钮 func GetMenuButtonsByRoleId(roleId int) (items []*SysMenu, err error) { sql := `SELECT r.* FROM sys_menu AS r JOIN sys_role_menu_mapping AS rm ON r.sys_menu_id = rm.sys_menu_id WHERE rm.sys_role_id = ? ORDER BY r.sort ASC, r.create_time DESC` o := orm.NewOrm() _, err = o.Raw(sql, roleId).QueryRows(&items) return } // GetMenuListByRoleIds 根据管理员角色id查询菜单 func GetMenuListByRoleIds(roleId int) (items []*SysMenuItem, err error) { o := orm.NewOrm() sql := `SELECT a.* FROM sys_menu AS a LEFT JOIN sys_role_menu_mapping AS b ON a.sys_menu_id=b.sys_menu_id WHERE b.sys_role_id = ? AND a.menu_type = 1 ORDER BY sort ASC ` _, err = o.Raw(sql, roleId).QueryRows(&items) return } // GetSysMenuItemsByCondition 获取菜单列表 func GetSysMenuItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SysMenu, err error) { o := orm.NewOrm() fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := `ORDER BY create_time DESC` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM sys_menu WHERE 1=1 %s %s`, fields, condition, order) _, err = o.Raw(sql, pars).QueryRows(&items) return }