sys_menu.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. Api string `description:"按钮相关api"`
  31. }
  32. // GetSysMenuItemsByCondition 获取菜单列表
  33. func GetSysMenuItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SysMenu, err error) {
  34. o := orm.NewOrm()
  35. fields := strings.Join(fieldArr, ",")
  36. if len(fieldArr) == 0 {
  37. fields = `*`
  38. }
  39. order := `ORDER BY create_time DESC`
  40. if orderRule != "" {
  41. order = ` ORDER BY ` + orderRule
  42. }
  43. sql := fmt.Sprintf(`SELECT %s FROM sys_menu WHERE 1=1 %s %s`, fields, condition, order)
  44. _, err = o.Raw(sql, pars).QueryRows(&items)
  45. return
  46. }
  47. type MenuList struct {
  48. MenuId int `description:"导航唯一标识"`
  49. IsLevel int `description:"1,只有一级;2,有多级"`
  50. Name string `json:"name" description:"导航名称"`
  51. Path string `json:"path"`
  52. IconPath string `json:"icon_path"`
  53. LevelPath string `json:"level_path"`
  54. Component string `json:"component"`
  55. Hidden bool `json:"hidden"`
  56. Children []*ChildMenu `json:"children"`
  57. }
  58. type ChildMenu struct {
  59. MenuId int `description:"导航唯一标识"`
  60. Name string `json:"name" description:"导航名称"`
  61. Path string `json:"path"`
  62. Component string `json:"component"`
  63. IconPath string `json:"icon_path"`
  64. Hidden bool `json:"hidden"`
  65. }
  66. type MenuListResp struct {
  67. List []*MenuList
  68. }
  69. func GetMenuList(roleId int) (items []*MenuList, err error) {
  70. sql := ` SELECT a.* FROM sys_menu AS a
  71. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id
  72. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  73. WHERE c.role_id=?
  74. AND a.parent_id=0
  75. ORDER BY sort ASC `
  76. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  77. return
  78. }
  79. func GetMenuByParentId(roleId, parentId int) (items []*ChildMenu, err error) {
  80. sql := ` SELECT a.* FROM sys_menu AS a
  81. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id
  82. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  83. WHERE c.role_id=?
  84. AND a.parent_id=?
  85. ORDER BY sort ASC `
  86. _, err = orm.NewOrm().Raw(sql, roleId, parentId).QueryRows(&items)
  87. return
  88. }
  89. // GetMenuListByRoleIds 根据管理员多个角色查询菜单ID
  90. func GetMenuListByRoleIds(roleIds string) (items []*MenuList, err error) {
  91. sql := ` SELECT DISTINCT a.* FROM sys_menu AS a
  92. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id AND b.type = 0
  93. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  94. WHERE c.role_id in (` + roleIds + `)
  95. AND a.parent_id=0
  96. ORDER BY sort ASC `
  97. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  98. return
  99. }
  100. // GetMenuByParentIdRoleIds 根据管理员多个角色查询子菜单ID
  101. func GetMenuByParentIdRoleIds(roleIds string, parentId int) (items []*ChildMenu, err error) {
  102. sql := ` SELECT DISTINCT a.* FROM sys_menu AS a
  103. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id AND b.type = 0
  104. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  105. WHERE c.role_id in (` + roleIds + `)
  106. AND a.parent_id=?
  107. ORDER BY a.sort ASC, create_time DESC, menu_id DESC`
  108. _, err = orm.NewOrm().Raw(sql, parentId).QueryRows(&items)
  109. return
  110. }
  111. // GetMenuButtonsByRoleId 获取角色按钮菜单
  112. func GetMenuButtonsByRoleId(roleId int) (items []*SysMenu, err error) {
  113. sql := `SELECT
  114. r.*
  115. FROM
  116. sys_menu AS r
  117. JOIN sys_role_menu AS rm ON r.menu_id = rm.menu_id AND rm.type = 0
  118. WHERE
  119. rm.role_id = ? AND r.menu_type <> 0 AND r.hidden = 0
  120. ORDER BY
  121. r.sort ASC,
  122. r.create_time DESC`
  123. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  124. return
  125. }
  126. // SysMenuButtonResp 按钮菜单响应体
  127. type SysMenuButtonResp struct {
  128. MenuId int `description:"菜单ID"`
  129. ParentId int `description:"父级菜单ID"`
  130. Name string `description:"菜单名称或者按钮名称"`
  131. MenuType int `description:"菜单类型: 0-菜单; 1-按钮"`
  132. ButtonCode string `description:"按钮唯一标识"`
  133. }
  134. // SysMenuListResp ETA商家菜单列表响应体
  135. type SysMenuListResp struct {
  136. ChoiceList []int `description:"已选菜单"`
  137. HalfChoiceList []int `description:"半选菜单-方便前端回显用的"`
  138. List []*SysMenuItem `description:"菜单列表"`
  139. }
  140. // SysMenuItem ETA商家菜单
  141. type SysMenuItem struct {
  142. MenuId int
  143. ParentId int `description:"父级菜单ID"`
  144. Name string `description:"菜单名称或者按钮名称"`
  145. Sort string `description:"排序"`
  146. Path string `description:"路由地址"`
  147. IconPath string `description:"菜单图标地址"`
  148. Component int `description:"组件路径"`
  149. Hidden int `description:"是否隐藏:1-隐藏 0-显示"`
  150. MenuType int `description:"菜单类型: 0-菜单; 1-按钮; 2-字段(需要特殊处理)"`
  151. ButtonCode string `description:"按钮/菜单唯一标识"`
  152. Children []*SysMenuItem `description:"子菜单"`
  153. }
  154. // BusinessConf 商户配置表
  155. type BusinessConf struct {
  156. ConfKey string `description:"配置Key"`
  157. ConfVal string `description:"配置值"`
  158. }
  159. // GetMenuButtonApisByRoleId 获取角色按钮api菜单
  160. func GetMenuButtonApisByRoleId(roleId int) (items []*SysMenu, err error) {
  161. sql := `SELECT
  162. r.*
  163. FROM
  164. sys_menu AS r
  165. JOIN sys_role_menu AS rm ON r.menu_id = rm.menu_id
  166. WHERE
  167. rm.role_id = ?
  168. ORDER BY
  169. r.sort ASC,
  170. r.create_time DESC`
  171. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  172. return
  173. }