sys_menu.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package system
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type MenuList struct {
  7. MenuId int `description:"导航唯一标识"`
  8. IsLevel int `description:"1,只有一级;2,有多级"`
  9. Name string `json:"name" description:"导航名称"`
  10. Path string `json:"path"`
  11. IconPath string `json:"icon_path"`
  12. LevelPath string `json:"level_path"`
  13. Component string `json:"component"`
  14. Hidden bool `json:"hidden"`
  15. Children []*ChildMenu `json:"children"`
  16. }
  17. type ChildMenu struct {
  18. MenuId int `description:"导航唯一标识"`
  19. Name string `json:"name" description:"导航名称"`
  20. Path string `json:"path"`
  21. Component string `json:"component"`
  22. IconPath string `json:"icon_path"`
  23. Hidden bool `json:"hidden"`
  24. }
  25. type MenuListResp struct {
  26. List []*MenuList
  27. }
  28. func GetMenuList(roleId int) (items []*MenuList, err error) {
  29. sql := ` SELECT a.* FROM sys_menu AS a
  30. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id
  31. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  32. WHERE c.role_id=?
  33. AND a.parent_id=0
  34. ORDER BY sort ASC `
  35. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  36. return
  37. }
  38. func GetMenuByParentId(roleId, parentId int) (items []*ChildMenu, err error) {
  39. sql := ` SELECT a.* FROM sys_menu AS a
  40. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id
  41. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  42. WHERE c.role_id=?
  43. AND a.parent_id=?
  44. ORDER BY sort ASC `
  45. _, err = orm.NewOrm().Raw(sql, roleId, parentId).QueryRows(&items)
  46. return
  47. }
  48. // GetMenuListByRoleIds 根据管理员多个角色查询菜单ID
  49. func GetMenuListByRoleIds(roleIds string) (items []*MenuList, err error) {
  50. sql := ` SELECT DISTINCT a.* FROM sys_menu AS a
  51. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id
  52. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  53. WHERE c.role_id in (` + roleIds + `)
  54. AND a.parent_id=0
  55. ORDER BY sort ASC `
  56. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  57. return
  58. }
  59. // GetMenuByParentIdRoleIds 根据管理员多个角色查询子菜单ID
  60. func GetMenuByParentIdRoleIds(roleIds string, parentId int) (items []*ChildMenu, err error) {
  61. sql := ` SELECT DISTINCT a.* FROM sys_menu AS a
  62. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id
  63. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  64. WHERE c.role_id in (` + roleIds + `)
  65. AND a.parent_id=?
  66. ORDER BY a.sort ASC`
  67. _, err = orm.NewOrm().Raw(sql, parentId).QueryRows(&items)
  68. return
  69. }
  70. type SysMenu struct {
  71. MenuId int `orm:"column(menu_id);pk"`
  72. ParentId int `description:"父级菜单ID"`
  73. Name string `description:"菜单名称或者按钮名称"`
  74. Sort string `description:"排序"`
  75. Path string `description:"路由地址"`
  76. IconPath string `description:"菜单图标地址"`
  77. Component int `description:"组件路径"`
  78. Hidden int `description:"是否隐藏:1-隐藏 0-显示"`
  79. IsLevel int `description:"是否为多级菜单:1,只有一级;2,有多级"`
  80. LevelPath string `description:"兼容以前menu表的字段"`
  81. MenuType int `description:"菜单类型: 0-菜单; 1-按钮; 2-字段(需要特殊处理)"`
  82. ButtonCode string `description:"按钮/菜单唯一标识"`
  83. CreateTime time.Time `description:"创建时间"`
  84. ModifyTime time.Time `description:"更新时间"`
  85. }
  86. // GetMenuButtonsByRoleId 获取角色按钮菜单
  87. func GetMenuButtonsByRoleId(roleId int) (items []*SysMenu, err error) {
  88. sql := `SELECT
  89. r.*
  90. FROM
  91. sys_menu AS r
  92. JOIN sys_role_menu AS rm ON r.menu_id = rm.menu_id AND rm.type = 0
  93. WHERE
  94. rm.role_id = ? AND r.menu_type <> 0 AND r.hidden = 0
  95. ORDER BY
  96. r.sort ASC,
  97. r.create_time DESC`
  98. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  99. return
  100. }
  101. // SysMenuButtonResp 按钮菜单响应体
  102. type SysMenuButtonResp struct {
  103. MenuId int `description:"菜单ID"`
  104. ParentId int `description:"父级菜单ID"`
  105. Name string `description:"菜单名称或者按钮名称"`
  106. MenuType int `description:"菜单类型: 0-菜单; 1-按钮"`
  107. ButtonCode string `description:"按钮唯一标识"`
  108. }
  109. // BusinessConf 商户配置表
  110. type BusinessConf struct {
  111. ConfKey string `description:"配置Key"`
  112. ConfVal string `description:"配置值"`
  113. }