sys_menu.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package system
  2. import (
  3. "errors"
  4. "strconv"
  5. "gorm.io/gorm"
  6. "lcfns/global"
  7. "lcfns/model/common/request"
  8. "lcfns/model/system"
  9. )
  10. //@author: [piexlmax](https://github.com/piexlmax)
  11. //@function: getMenuTreeMap
  12. //@description: 获取路由总树map
  13. //@param: authorityId string
  14. //@return: treeMap map[string][]system.SysMenu, err error
  15. type MenuService struct{}
  16. var MenuServiceApp = new(MenuService)
  17. func (menuService *MenuService) getMenuTreeMap(authorityId uint) (treeMap map[string][]system.SysMenu, err error) {
  18. var allMenus []system.SysMenu
  19. var baseMenu []system.SysBaseMenu
  20. var btns []system.SysAuthorityBtn
  21. treeMap = make(map[string][]system.SysMenu)
  22. var SysAuthorityMenus []system.SysAuthorityMenu
  23. err = global.Db.Where("sys_authority_authority_id = ?", authorityId).Find(&SysAuthorityMenus).Error
  24. if err != nil {
  25. return
  26. }
  27. var MenuIds []string
  28. for i := range SysAuthorityMenus {
  29. MenuIds = append(MenuIds, SysAuthorityMenus[i].MenuId)
  30. }
  31. err = global.Db.Where("id in (?)", MenuIds).Order("sort").Preload("Parameters").Find(&baseMenu).Error
  32. if err != nil {
  33. return
  34. }
  35. for i := range baseMenu {
  36. allMenus = append(allMenus, system.SysMenu{
  37. SysBaseMenu: baseMenu[i],
  38. AuthorityId: authorityId,
  39. MenuId: strconv.Itoa(int(baseMenu[i].ID)),
  40. Parameters: baseMenu[i].Parameters,
  41. })
  42. }
  43. err = global.Db.Where("authority_id = ?", authorityId).Preload("SysBaseMenuBtn").Find(&btns).Error
  44. if err != nil {
  45. return
  46. }
  47. var btnMap = make(map[uint]map[string]uint)
  48. for _, v := range btns {
  49. if btnMap[v.SysMenuID] == nil {
  50. btnMap[v.SysMenuID] = make(map[string]uint)
  51. }
  52. btnMap[v.SysMenuID][v.SysBaseMenuBtn.Name] = authorityId
  53. }
  54. for _, v := range allMenus {
  55. v.Btns = btnMap[v.ID]
  56. treeMap[v.ParentId] = append(treeMap[v.ParentId], v)
  57. }
  58. return treeMap, err
  59. }
  60. //@author: [piexlmax](https://github.com/piexlmax)
  61. //@function: GetMenuTree
  62. //@description: 获取动态菜单树
  63. //@param: authorityId string
  64. //@return: menus []system.SysMenu, err error
  65. func (menuService *MenuService) GetMenuTree(authorityId uint) (menus []system.SysMenu, err error) {
  66. menuTree, err := menuService.getMenuTreeMap(authorityId)
  67. menus = menuTree["0"]
  68. for i := 0; i < len(menus); i++ {
  69. err = menuService.getChildrenList(&menus[i], menuTree)
  70. }
  71. return menus, err
  72. }
  73. //@author: [piexlmax](https://github.com/piexlmax)
  74. //@function: getChildrenList
  75. //@description: 获取子菜单
  76. //@param: menu *model.SysMenu, treeMap map[string][]model.SysMenu
  77. //@return: err error
  78. func (menuService *MenuService) getChildrenList(menu *system.SysMenu, treeMap map[string][]system.SysMenu) (err error) {
  79. menu.Children = treeMap[menu.MenuId]
  80. for i := 0; i < len(menu.Children); i++ {
  81. err = menuService.getChildrenList(&menu.Children[i], treeMap)
  82. }
  83. return err
  84. }
  85. //@author: [piexlmax](https://github.com/piexlmax)
  86. //@function: GetInfoList
  87. //@description: 获取路由分页
  88. //@return: list interface{}, total int64,err error
  89. func (menuService *MenuService) GetInfoList() (list interface{}, total int64, err error) {
  90. var menuList []system.SysBaseMenu
  91. treeMap, err := menuService.getBaseMenuTreeMap()
  92. menuList = treeMap["0"]
  93. for i := 0; i < len(menuList); i++ {
  94. err = menuService.getBaseChildrenList(&menuList[i], treeMap)
  95. }
  96. return menuList, total, err
  97. }
  98. //@author: [piexlmax](https://github.com/piexlmax)
  99. //@function: getBaseChildrenList
  100. //@description: 获取菜单的子菜单
  101. //@param: menu *model.SysBaseMenu, treeMap map[string][]model.SysBaseMenu
  102. //@return: err error
  103. func (menuService *MenuService) getBaseChildrenList(menu *system.SysBaseMenu, treeMap map[string][]system.SysBaseMenu) (err error) {
  104. menu.Children = treeMap[strconv.Itoa(int(menu.ID))]
  105. for i := 0; i < len(menu.Children); i++ {
  106. err = menuService.getBaseChildrenList(&menu.Children[i], treeMap)
  107. }
  108. return err
  109. }
  110. //@author: [piexlmax](https://github.com/piexlmax)
  111. //@function: AddBaseMenu
  112. //@description: 添加基础路由
  113. //@param: menu model.SysBaseMenu
  114. //@return: error
  115. func (menuService *MenuService) AddBaseMenu(menu system.SysBaseMenu) error {
  116. if !errors.Is(global.Db.Where("name = ?", menu.Name).First(&system.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) {
  117. return errors.New("存在重复name,请修改name")
  118. }
  119. return global.Db.Create(&menu).Error
  120. }
  121. //@author: [piexlmax](https://github.com/piexlmax)
  122. //@function: getBaseMenuTreeMap
  123. //@description: 获取路由总树map
  124. //@return: treeMap map[string][]system.SysBaseMenu, err error
  125. func (menuService *MenuService) getBaseMenuTreeMap() (treeMap map[string][]system.SysBaseMenu, err error) {
  126. var allMenus []system.SysBaseMenu
  127. treeMap = make(map[string][]system.SysBaseMenu)
  128. err = global.Db.Order("sort").Preload("MenuBtn").Preload("Parameters").Find(&allMenus).Error
  129. for _, v := range allMenus {
  130. treeMap[v.ParentId] = append(treeMap[v.ParentId], v)
  131. }
  132. return treeMap, err
  133. }
  134. //@author: [piexlmax](https://github.com/piexlmax)
  135. //@function: GetBaseMenuTree
  136. //@description: 获取基础路由树
  137. //@return: menus []system.SysBaseMenu, err error
  138. func (menuService *MenuService) GetBaseMenuTree() (menus []system.SysBaseMenu, err error) {
  139. treeMap, err := menuService.getBaseMenuTreeMap()
  140. menus = treeMap["0"]
  141. for i := 0; i < len(menus); i++ {
  142. err = menuService.getBaseChildrenList(&menus[i], treeMap)
  143. }
  144. return menus, err
  145. }
  146. //@author: [piexlmax](https://github.com/piexlmax)
  147. //@function: AddMenuAuthority
  148. //@description: 为角色增加menu树
  149. //@param: menus []model.SysBaseMenu, authorityId string
  150. //@return: err error
  151. func (menuService *MenuService) AddMenuAuthority(menus []system.SysBaseMenu, authorityId uint) (err error) {
  152. var auth system.SysAuthority
  153. auth.AuthorityId = authorityId
  154. auth.SysBaseMenus = menus
  155. err = AuthorityServiceApp.SetMenuAuthority(&auth)
  156. return err
  157. }
  158. //@author: [piexlmax](https://github.com/piexlmax)
  159. //@function: GetMenuAuthority
  160. //@description: 查看当前角色树
  161. //@param: info *request.GetAuthorityId
  162. //@return: menus []system.SysMenu, err error
  163. func (menuService *MenuService) GetMenuAuthority(info *request.GetAuthorityId) (menus []system.SysMenu, err error) {
  164. var baseMenu []system.SysBaseMenu
  165. var SysAuthorityMenus []system.SysAuthorityMenu
  166. err = global.Db.Where("sys_authority_authority_id = ?", info.AuthorityId).Find(&SysAuthorityMenus).Error
  167. if err != nil {
  168. return
  169. }
  170. var MenuIds []string
  171. for i := range SysAuthorityMenus {
  172. MenuIds = append(MenuIds, SysAuthorityMenus[i].MenuId)
  173. }
  174. err = global.Db.Where("id in (?) ", MenuIds).Order("sort").Find(&baseMenu).Error
  175. for i := range baseMenu {
  176. menus = append(menus, system.SysMenu{
  177. SysBaseMenu: baseMenu[i],
  178. AuthorityId: info.AuthorityId,
  179. MenuId: strconv.Itoa(int(baseMenu[i].ID)),
  180. Parameters: baseMenu[i].Parameters,
  181. })
  182. }
  183. // sql := "SELECT authority_menu.keep_alive,authority_menu.default_menu,authority_menu.created_at,authority_menu.updated_at,authority_menu.deleted_at,authority_menu.menu_level,authority_menu.parent_id,authority_menu.path,authority_menu.`name`,authority_menu.hidden,authority_menu.component,authority_menu.title,authority_menu.icon,authority_menu.sort,authority_menu.menu_id,authority_menu.authority_id FROM authority_menu WHERE authority_menu.authority_id = ? ORDER BY authority_menu.sort ASC"
  184. // err = global.Db.Raw(sql, authorityId).Scan(&menus).Error
  185. return menus, err
  186. }
  187. // UserAuthorityDefaultRouter 用户角色默认路由检查
  188. //
  189. // Author [SliverHorn](https://github.com/SliverHorn)
  190. func (menuService *MenuService) UserAuthorityDefaultRouter(user *system.SysUser) {
  191. var menuIds []string
  192. err := global.Db.Model(&system.SysAuthorityMenu{}).Where("sys_authority_authority_id = ?", user.AuthorityId).Pluck("sys_base_menu_id", &menuIds).Error
  193. if err != nil {
  194. return
  195. }
  196. var am system.SysBaseMenu
  197. err = global.Db.First(&am, "name = ? and id in (?)", user.Authority.DefaultRouter, menuIds).Error
  198. if errors.Is(err, gorm.ErrRecordNotFound) {
  199. user.Authority.DefaultRouter = "404"
  200. }
  201. }