sys_menu.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package system
  2. import (
  3. "eta_gn/eta_api/global"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // 需要特殊处理的菜单字段项
  10. const (
  11. MenuSpecialHandleClassifyChildMenu = "classifyList:cnClassify:childMenu"
  12. MenuSpecialHandleClassifyShowType = "classifyList:cnClassify:showType"
  13. MenuSpecialHandleClassifyReportImgs = "classifyList:cnClassify:reportImgs"
  14. MenuSpecialHandleSandboxVariety = "sandbox:variety"
  15. )
  16. type SysMenu struct {
  17. MenuId int `orm:"column(menu_id);pk"`
  18. ParentId int `description:"父级菜单ID"`
  19. Name string `description:"菜单名称或者按钮名称"`
  20. Sort string `description:"排序"`
  21. Path string `description:"路由地址"`
  22. IconPath string `description:"菜单图标地址"`
  23. Component string `description:"组件路径"`
  24. Hidden int `description:"是否隐藏:1-隐藏 0-显示"`
  25. IsLevel int `description:"是否为多级菜单:1,只有一级;2,有多级"`
  26. LevelPath string `description:"兼容以前menu表的字段"`
  27. MenuType int `description:"菜单类型: 0-菜单; 1-按钮; 2-字段(需要特殊处理)"`
  28. ButtonCode string `description:"按钮/菜单唯一标识"`
  29. CreateTime time.Time `description:"创建时间"`
  30. ModifyTime time.Time `description:"更新时间"`
  31. Api string `description:"按钮相关api"`
  32. NameEn string `description:"菜单名称或者按钮名称(英文)"`
  33. }
  34. // GetSysMenuItemsByCondition 获取菜单列表
  35. func GetSysMenuItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SysMenu, err error) {
  36. o := orm.NewOrm()
  37. fields := strings.Join(fieldArr, ",")
  38. if len(fieldArr) == 0 {
  39. fields = `*`
  40. }
  41. order := `ORDER BY create_time DESC`
  42. if orderRule != "" {
  43. order = ` ORDER BY ` + orderRule
  44. }
  45. sql := fmt.Sprintf(`SELECT %s FROM sys_menu WHERE 1=1 %s %s`, fields, condition, order)
  46. _, err = o.Raw(sql, pars).QueryRows(&items)
  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 `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 `json:"children"`
  71. }
  72. type MenuListResp struct {
  73. List []*MenuList
  74. }
  75. func GetMenuList(roleId int) (items []*MenuList, err error) {
  76. sql := ` SELECT a.* FROM sys_menu AS a
  77. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id
  78. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  79. WHERE c.role_id=?
  80. AND a.parent_id=0
  81. ORDER BY sort ASC `
  82. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  83. return
  84. }
  85. func GetMenuByParentId(roleId, parentId int) (items []*ChildMenu, err error) {
  86. sql := ` SELECT a.* FROM sys_menu AS a
  87. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id
  88. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  89. WHERE c.role_id=?
  90. AND a.parent_id=?
  91. ORDER BY sort ASC `
  92. _, err = orm.NewOrm().Raw(sql, roleId, parentId).QueryRows(&items)
  93. return
  94. }
  95. // GetMenuListByRoleIds 根据管理员多个角色查询菜单ID
  96. func GetMenuListByRoleIds(roleIds string) (items []*MenuList, err error) {
  97. sql := ` SELECT DISTINCT a.* FROM sys_menu AS a
  98. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id AND b.type = 0
  99. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  100. WHERE c.role_id in (` + roleIds + `)
  101. AND a.parent_id=0
  102. ORDER BY sort ASC `
  103. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  104. return
  105. }
  106. // GetMenuByParentIdRoleIds 根据管理员多个角色查询子菜单ID
  107. func GetMenuByParentIdRoleIds(roleIds string, parentId int) (items []*ChildMenu, err error) {
  108. sql := ` SELECT DISTINCT a.* FROM sys_menu AS a
  109. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id AND b.type = 0
  110. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  111. WHERE c.role_id in (` + roleIds + `)
  112. AND a.parent_id=?
  113. ORDER BY a.sort ASC, create_time DESC, menu_id DESC`
  114. _, err = orm.NewOrm().Raw(sql, parentId).QueryRows(&items)
  115. return
  116. }
  117. // GetMenuButtonsByRoleId 获取角色按钮菜单
  118. func GetMenuButtonsByRoleId(roleId int) (items []*SysMenu, err error) {
  119. sql := `SELECT
  120. r.*
  121. FROM
  122. sys_menu AS r
  123. JOIN sys_role_menu AS rm ON r.menu_id = rm.menu_id AND rm.type = 0
  124. WHERE
  125. rm.role_id = ? AND r.menu_type <> 0 AND r.hidden = 0
  126. ORDER BY
  127. r.sort ASC,
  128. r.create_time DESC`
  129. _, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  130. return
  131. }
  132. // SysMenuButtonResp 按钮菜单响应体
  133. type SysMenuButtonResp struct {
  134. MenuId int `description:"菜单ID"`
  135. ParentId int `description:"父级菜单ID"`
  136. Name string `description:"菜单名称或者按钮名称"`
  137. MenuType int `description:"菜单类型: 0-菜单; 1-按钮"`
  138. ButtonCode string `description:"按钮唯一标识"`
  139. }
  140. // SysMenuListResp ETA商家菜单列表响应体
  141. type SysMenuListResp struct {
  142. ChoiceList []int `description:"已选菜单"`
  143. HalfChoiceList []int `description:"半选菜单-方便前端回显用的"`
  144. List []*SysMenuItem `description:"菜单列表"`
  145. }
  146. // SysMenuItem ETA商家菜单
  147. type SysMenuItem struct {
  148. MenuId int
  149. ParentId int `description:"父级菜单ID"`
  150. Name string `description:"菜单名称或者按钮名称"`
  151. Sort string `description:"排序"`
  152. Path string `description:"路由地址"`
  153. IconPath string `description:"菜单图标地址"`
  154. Component string `description:"组件路径"`
  155. Hidden int `description:"是否隐藏:1-隐藏 0-显示"`
  156. MenuType int `description:"菜单类型: 0-菜单; 1-按钮; 2-字段(需要特殊处理)"`
  157. ButtonCode string `description:"按钮/菜单唯一标识"`
  158. Children []*SysMenuItem `description:"子菜单"`
  159. }
  160. // BusinessConf 商户配置表
  161. type BusinessConf struct {
  162. ConfKey string `description:"配置Key"`
  163. ConfVal string `description:"配置值"`
  164. }
  165. // GetMenuButtonApisByRoleId 获取角色按钮api菜单
  166. func GetMenuButtonApisByRoleId(roleId int) (items []*SysMenu, err error) {
  167. sql := `SELECT
  168. r.*
  169. FROM
  170. sys_menu AS r
  171. JOIN sys_role_menu AS rm ON r.menu_id = rm.menu_id
  172. WHERE
  173. rm.role_id = ?
  174. ORDER BY
  175. r.sort ASC,
  176. r.create_time DESC`
  177. err = global.DEFAULT_DmSQL.Raw(sql, roleId).Scan(&items).Error
  178. return
  179. }
  180. // GetMenuListByRoleIds 根据管理员多个角色查询菜单ID
  181. func GetMenuListAllByRoleIds(roleIds string) (items []*MenuList, err error) {
  182. sql := ` SELECT DISTINCT a.* FROM sys_menu AS a
  183. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id AND b.type = 0
  184. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  185. WHERE c.role_id in (` + roleIds + `)
  186. AND a.menu_type=0
  187. ORDER BY menu_id ASC `
  188. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  189. return
  190. }