sys_menu.go 7.4 KB

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