sys_menu.go 5.7 KB

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