sys_menu.go 6.9 KB

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