sys_menu.go 7.8 KB

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