sys_menu.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. //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. sql := ` SELECT a.* FROM sys_menu AS a
  104. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id AND b.type = 0
  105. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  106. WHERE c.role_id in (` + roleIds + `)
  107. AND a.parent_id=0
  108. ORDER BY sort ASC `
  109. //_, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  110. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  111. return
  112. }
  113. // GetMenuByParentIdRoleIds 根据管理员多个角色查询子菜单ID
  114. //func GetMenuByParentIdRoleIds(roleIds string, parentId int) (items []*ChildMenu, err error) {
  115. // sql := ` SELECT DISTINCT a.* FROM sys_menu AS a
  116. // INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id AND b.type = 0
  117. // INNER JOIN sys_role AS c ON b.role_id=c.role_id
  118. // WHERE c.role_id in (` + roleIds + `)
  119. // AND a.parent_id=?
  120. // ORDER BY a.sort ASC, create_time DESC, menu_id DESC`
  121. // _, err = orm.NewOrm().Raw(sql, parentId).QueryRows(&items)
  122. // return
  123. //}
  124. // GetMenuButtonsByRoleId 获取角色按钮菜单
  125. func GetMenuButtonsByRoleId(roleId int) (items []*SysMenu, err error) {
  126. sql := `SELECT
  127. r.*
  128. FROM
  129. sys_menu AS r
  130. JOIN sys_role_menu AS rm ON r.menu_id = rm.menu_id AND rm.type = 0
  131. WHERE
  132. rm.role_id = ? AND r.menu_type <> 0 AND r.hidden = 0
  133. ORDER BY
  134. r.sort ASC,
  135. r.create_time DESC`
  136. //_, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
  137. err = global.DEFAULT_DmSQL.Raw(sql, roleId).Find(&items).Error
  138. return
  139. }
  140. // SysMenuButtonResp 按钮菜单响应体
  141. type SysMenuButtonResp struct {
  142. MenuId int `description:"菜单ID"`
  143. ParentId int `description:"父级菜单ID"`
  144. Name string `description:"菜单名称或者按钮名称"`
  145. MenuType int `description:"菜单类型: 0-菜单; 1-按钮"`
  146. ButtonCode string `description:"按钮唯一标识"`
  147. }
  148. // SysMenuListResp ETA商家菜单列表响应体
  149. type SysMenuListResp struct {
  150. ChoiceList []int `description:"已选菜单"`
  151. HalfChoiceList []int `description:"半选菜单-方便前端回显用的"`
  152. List []*SysMenuItem `description:"菜单列表"`
  153. }
  154. // SysMenuItem ETA商家菜单
  155. type SysMenuItem struct {
  156. MenuId int
  157. ParentId int `description:"父级菜单ID"`
  158. Name string `description:"菜单名称或者按钮名称"`
  159. Sort string `description:"排序"`
  160. Path string `description:"路由地址"`
  161. IconPath string `description:"菜单图标地址"`
  162. Component string `description:"组件路径"`
  163. Hidden int `description:"是否隐藏:1-隐藏 0-显示"`
  164. MenuType int `description:"菜单类型: 0-菜单; 1-按钮; 2-字段(需要特殊处理)"`
  165. ButtonCode string `description:"按钮/菜单唯一标识"`
  166. Children []*SysMenuItem `gorm:"-" description:"子菜单"`
  167. }
  168. // BusinessConf 商户配置表
  169. type BusinessConf struct {
  170. ConfKey string `description:"配置Key"`
  171. ConfVal string `description:"配置值"`
  172. }
  173. // GetMenuButtonApisByRoleId 获取角色按钮api菜单
  174. func GetMenuButtonApisByRoleId(roleId int) (items []*SysMenu, err error) {
  175. sql := `SELECT
  176. r.*
  177. FROM
  178. sys_menu AS r
  179. JOIN sys_role_menu AS rm ON r.menu_id = rm.menu_id
  180. WHERE
  181. rm.role_id = ?
  182. ORDER BY
  183. r.sort ASC,
  184. r.create_time DESC`
  185. err = global.DEFAULT_DmSQL.Raw(sql, roleId).Scan(&items).Error
  186. return
  187. }
  188. // GetMenuListAllByRoleIds 根据管理员多个角色查询菜单ID
  189. func GetMenuListAllByRoleIds(roleIds string) (items []*MenuList, err error) {
  190. //sql := ` SELECT DISTINCT a.* FROM sys_menu AS a
  191. // INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id AND b.type = 0
  192. // INNER JOIN sys_role AS c ON b.role_id=c.role_id
  193. // WHERE c.role_id in (` + roleIds + `)
  194. // AND a.menu_type=0
  195. // ORDER BY menu_id ASC `
  196. sql := ` SELECT a.* FROM sys_menu AS a
  197. INNER JOIN sys_role_menu AS b ON a.menu_id=b.menu_id AND b.type = 0
  198. INNER JOIN sys_role AS c ON b.role_id=c.role_id
  199. WHERE c.role_id in (` + roleIds + `)
  200. AND a.menu_type=0
  201. ORDER BY menu_id ASC `
  202. //_, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  203. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  204. return
  205. }