sys_menu.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package models
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/beego/beego/v2/client/orm"
  7. )
  8. type SysMenu struct {
  9. SysMenuId int `orm:"pk" description:"菜单id"`
  10. ParentId int `description:"父级id"`
  11. Sort int `description:"排序id"`
  12. ButtonCode string `description:"按钮唯一编码"`
  13. Name string `description:"按钮名称"`
  14. Path string `description:"路径"`
  15. Api string `description:"api接口"`
  16. IconPath string `description:"图标路径"`
  17. Level int `description:"层级"`
  18. MenuType int `description:"菜单类型,1:菜单,2:按钮"`
  19. CreateTime time.Time `description:"创建时间"`
  20. ModifyTime time.Time `description:"修改时间"`
  21. }
  22. type SysRoleMenuMapping struct {
  23. SysRoleMenuMappingId int `orm:"pk" description:"id"`
  24. SysRoleId int `description:"角色id"`
  25. SysMenuId int `description:"菜单id"`
  26. Type int `description:"类型"`
  27. }
  28. // SysMenuItem 角色菜单
  29. type SysMenuItem struct {
  30. SysMenuId int
  31. ParentId int `description:"父级菜单ID"`
  32. Name string `description:"菜单名称或者按钮名称"`
  33. Level int `description:"层级"`
  34. Sort int `description:"排序"`
  35. Path string `description:"路由地址"`
  36. IconPath string `description:"菜单图标地址"`
  37. ButtonCode string `description:"按钮/菜单唯一标识"`
  38. Children []*SysMenuItem `description:"子菜单"`
  39. }
  40. // GetMenuButtonsByRoleId 获取角色按钮
  41. func GetMenuButtonsByRoleId(roleId int) (items []*SysMenu, err error) {
  42. sql := `SELECT
  43. r.*
  44. FROM
  45. sys_menu AS r
  46. JOIN sys_role_menu_mapping AS rm ON r.sys_menu_id = rm.sys_menu_id
  47. WHERE
  48. rm.sys_role_id = ?
  49. ORDER BY
  50. r.sort ASC,
  51. r.create_time DESC`
  52. o := orm.NewOrm()
  53. _, err = o.Raw(sql, roleId).QueryRows(&items)
  54. return
  55. }
  56. // GetMenuListByRoleIds 根据管理员角色id查询菜单
  57. func GetMenuListByRoleIds(roleId int) (items []*SysMenuItem, err error) {
  58. o := orm.NewOrm()
  59. sql := `SELECT a.*
  60. FROM sys_menu AS a
  61. LEFT JOIN sys_role_menu_mapping AS b ON a.sys_menu_id=b.sys_menu_id
  62. WHERE b.sys_role_id = ? AND a.menu_type = 1
  63. ORDER BY sort ASC `
  64. _, err = o.Raw(sql, roleId).QueryRows(&items)
  65. return
  66. }
  67. // GetSysMenuItemsByCondition 获取菜单列表
  68. func GetSysMenuItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SysMenu, err error) {
  69. o := orm.NewOrm()
  70. fields := strings.Join(fieldArr, ",")
  71. if len(fieldArr) == 0 {
  72. fields = `*`
  73. }
  74. order := `ORDER BY create_time DESC`
  75. if orderRule != "" {
  76. order = ` ORDER BY ` + orderRule
  77. }
  78. sql := fmt.Sprintf(`SELECT %s FROM sys_menu WHERE 1=1 %s %s`, fields, condition, order)
  79. _, err = o.Raw(sql, pars).QueryRows(&items)
  80. return
  81. }