sys_menu.go 6.8 KB

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