sys_menu.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package system
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strings"
  6. "time"
  7. )
  8. // 需要特殊处理的菜单字段项
  9. const (
  10. MenuSpecialHandleClassifyChildMenu = "classifyList:cnClassify:childMenu"
  11. MenuSpecialHandleClassifyShowType = "classifyList:cnClassify:showType"
  12. MenuSpecialHandleClassifyReportImgs = "classifyList:cnClassify:reportImgs"
  13. MenuSpecialHandleSandboxVariety = "sandbox:variety"
  14. )
  15. type SysMenu struct {
  16. MenuId int `orm:"column(menu_id);pk"`
  17. ParentId int `description:"父级菜单ID"`
  18. Name string `description:"菜单名称或者按钮名称"`
  19. Sort string `description:"排序"`
  20. Path string `description:"路由地址"`
  21. IconPath string `description:"菜单图标地址"`
  22. Component int `description:"组件路径"`
  23. Hidden int `description:"是否隐藏:1-隐藏 0-显示"`
  24. IsLevel int `description:"是否为多级菜单:1,只有一级;2,有多级"`
  25. LevelPath string `description:"兼容以前menu表的字段"`
  26. MenuType int `description:"菜单类型: 0-菜单; 1-按钮; 2-字段(需要特殊处理)"`
  27. ButtonCode string `description:"按钮/菜单唯一标识"`
  28. CreateTime time.Time `description:"创建时间"`
  29. ModifyTime time.Time `description:"更新时间"`
  30. }
  31. // GetSysMenuItemsByCondition 获取菜单列表
  32. func GetSysMenuItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SysMenu, err error) {
  33. o := orm.NewOrm()
  34. fields := strings.Join(fieldArr, ",")
  35. if len(fieldArr) == 0 {
  36. fields = `*`
  37. }
  38. order := `ORDER BY create_time DESC`
  39. if orderRule != "" {
  40. order = ` ORDER BY ` + orderRule
  41. }
  42. sql := fmt.Sprintf(`SELECT %s FROM sys_menu WHERE 1=1 %s %s`, fields, condition, order)
  43. _, err = o.Raw(sql, pars).QueryRows(&items)
  44. return
  45. }
  46. type MenuList struct {
  47. MenuId int `description:"导航唯一标识"`
  48. IsLevel int `description:"1,只有一级;2,有多级"`
  49. Name string `json:"name" description:"导航名称"`
  50. Path string `json:"path"`
  51. IconPath string `json:"icon_path"`
  52. LevelPath string `json:"level_path"`
  53. Component string `json:"component"`
  54. Hidden bool `json:"hidden"`
  55. Children []*ChildMenu `json:"children"`
  56. }
  57. type ChildMenu struct {
  58. MenuId int `description:"导航唯一标识"`
  59. Name string `json:"name" description:"导航名称"`
  60. Path string `json:"path"`
  61. Component string `json:"component"`
  62. IconPath string `json:"icon_path"`
  63. Hidden bool `json:"hidden"`
  64. }
  65. type MenuListResp struct {
  66. List []*MenuList
  67. }
  68. func GetMenuList(roleId int) (items []*MenuList, err error) {
  69. sql := ` SELECT a.* FROM sys_menu AS a
  70. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id
  71. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  72. WHERE c.role_id=?
  73. AND a.parent_id=0
  74. ORDER BY sort ASC `
  75. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  76. return
  77. }
  78. func GetMenuByParentId(roleId, parentId int) (items []*ChildMenu, err error) {
  79. sql := ` SELECT a.* FROM sys_menu AS a
  80. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id
  81. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  82. WHERE c.role_id=?
  83. AND a.parent_id=?
  84. ORDER BY sort ASC `
  85. _, err = orm.NewOrm().Raw(sql, roleId, parentId).QueryRows(&items)
  86. return
  87. }
  88. // GetMenuListByRoleIds 根据管理员多个角色查询菜单ID
  89. func GetMenuListByRoleIds(roleIds string) (items []*MenuList, err error) {
  90. sql := ` SELECT DISTINCT a.* FROM sys_menu AS a
  91. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id AND b.type = 0
  92. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  93. WHERE c.role_id in (` + roleIds + `)
  94. AND a.parent_id=0
  95. ORDER BY sort ASC `
  96. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  97. return
  98. }
  99. // GetMenuByParentIdRoleIds 根据管理员多个角色查询子菜单ID
  100. func GetMenuByParentIdRoleIds(roleIds string, parentId int) (items []*ChildMenu, err error) {
  101. sql := ` SELECT DISTINCT a.* FROM sys_menu AS a
  102. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id AND b.type = 0
  103. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  104. WHERE c.role_id in (` + roleIds + `)
  105. AND a.parent_id=?
  106. ORDER BY a.sort ASC`
  107. _, err = orm.NewOrm().Raw(sql, parentId).QueryRows(&items)
  108. return
  109. }
  110. // GetMenuButtonsByRoleId 获取角色按钮菜单
  111. func GetMenuButtonsByRoleId(roleId int) (items []*SysMenu, err error) {
  112. sql := `SELECT
  113. r.*
  114. FROM
  115. sys_menu AS r
  116. JOIN sys_role_menu AS rm ON r.menu_id = rm.menu_id AND rm.type = 0
  117. WHERE
  118. rm.role_id = ? AND r.menu_type <> 0 AND r.hidden = 0
  119. ORDER BY
  120. r.sort ASC,
  121. r.create_time DESC`
  122. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  123. return
  124. }
  125. // SysMenuButtonResp 按钮菜单响应体
  126. type SysMenuButtonResp struct {
  127. MenuId int `description:"菜单ID"`
  128. ParentId int `description:"父级菜单ID"`
  129. Name string `description:"菜单名称或者按钮名称"`
  130. MenuType int `description:"菜单类型: 0-菜单; 1-按钮"`
  131. ButtonCode string `description:"按钮唯一标识"`
  132. }
  133. // SysMenuListResp ETA商家菜单列表响应体
  134. type SysMenuListResp struct {
  135. ChoiceList []int `description:"已选菜单"`
  136. HalfChoiceList []int `description:"半选菜单-方便前端回显用的"`
  137. List []*SysMenuItem `description:"菜单列表"`
  138. }
  139. // SysMenuItem ETA商家菜单
  140. type SysMenuItem struct {
  141. MenuId int
  142. ParentId int `description:"父级菜单ID"`
  143. Name string `description:"菜单名称或者按钮名称"`
  144. Sort string `description:"排序"`
  145. Path string `description:"路由地址"`
  146. IconPath string `description:"菜单图标地址"`
  147. Component int `description:"组件路径"`
  148. Hidden int `description:"是否隐藏:1-隐藏 0-显示"`
  149. MenuType int `description:"菜单类型: 0-菜单; 1-按钮; 2-字段(需要特殊处理)"`
  150. ButtonCode string `description:"按钮/菜单唯一标识"`
  151. Children []*SysMenuItem `description:"子菜单"`
  152. }
  153. // BusinessConf 商户配置表
  154. type BusinessConf struct {
  155. ConfKey string `description:"配置Key"`
  156. ConfVal string `description:"配置值"`
  157. }