sys_menu.go 6.3 KB

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