From 6b876c6e078a9b439870bb5ee415aceffbc967a3 Mon Sep 17 00:00:00 2001 From: pixel <303176530@qq.com> Date: Wed, 6 May 2020 11:19:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86menu=E7=9A=84?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=AC=A1=E6=95=B0=20=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E4=BA=86=E8=8F=9C=E5=8D=95=E6=9F=A5=E8=AF=A2=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/api/v1/sys_menu.go | 2 +- server/service/sys_menu.go | 68 ++++++++++++++----- web/src/store/module/router.js | 2 +- .../layout/aside/asideComponent/index.vue | 4 +- .../view/superAdmin/authority/authority.vue | 2 +- .../superAdmin/authority/components/datas.vue | 2 +- web/src/view/superAdmin/menu/menu.vue | 2 +- web/src/view/superAdmin/user/user.vue | 2 +- 8 files changed, 58 insertions(+), 26 deletions(-) diff --git a/server/api/v1/sys_menu.go b/server/api/v1/sys_menu.go index bd421e18..7ca0f2b4 100644 --- a/server/api/v1/sys_menu.go +++ b/server/api/v1/sys_menu.go @@ -39,7 +39,7 @@ func GetMenu(c *gin.Context) { func GetMenuList(c *gin.Context) { var pageInfo request.PageInfo _ = c.ShouldBindJSON(&pageInfo) - err, menuList, total := service.GetInfoList(pageInfo) + err, menuList, total := service.GetInfoList() if err != nil { response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c) } else { diff --git a/server/service/sys_menu.go b/server/service/sys_menu.go index 6c9bd140..50314a26 100644 --- a/server/service/sys_menu.go +++ b/server/service/sys_menu.go @@ -4,9 +4,26 @@ import ( "errors" "gin-vue-admin/global" "gin-vue-admin/model" - "gin-vue-admin/model/request" + "strconv" ) +// @title getMenuTreeMap +// @description 获取路由总树map +// @auth qm (2020/05/06 10:26) +// @return err error +// @return menusMsp map{string}[]SysBaseMenu + +func getMenuTreeMap(authorityId string)(err error,treeMap map[string][]model.SysMenu){ + var allMenus []model.SysMenu + treeMap = make(map[string][]model.SysMenu) + 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" + err = global.GVA_DB.Raw(sql,authorityId).Scan(&allMenus).Error + for _,v := range allMenus{ + treeMap[v.ParentId] = append(treeMap[v.ParentId], v) + } + return err,treeMap +} + // @title GetMenuTree // @description 获取动态菜单树 // @auth (2020/04/05 20:22) @@ -15,10 +32,10 @@ import ( // @return menus []model.SysMenu func GetMenuTree(authorityId string) (err error, menus []model.SysMenu) { - 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 = ? AND authority_menu.parent_id = ? ORDER BY authority_menu.sort ASC" - err = global.GVA_DB.Raw(sql, authorityId, 0).Scan(&menus).Error + err,menuTree := getMenuTreeMap(authorityId) + menus = menuTree["0"] for i := 0; i < len(menus); i++ { - err = getChildrenList(&menus[i], sql) + err = getChildrenList(&menus[i], menuTree) } return err, menus } @@ -30,10 +47,10 @@ func GetMenuTree(authorityId string) (err error, menus []model.SysMenu) { // @param sql string // @return err error -func getChildrenList(menu *model.SysMenu, sql string) (err error) { - err = global.GVA_DB.Raw(sql, menu.AuthorityId, menu.MenuId).Scan(&menu.Children).Error +func getChildrenList(menu *model.SysMenu,treeMap map[string][]model.SysMenu) (err error) { + menu.Children = treeMap[menu.MenuId] for i := 0; i < len(menu.Children); i++ { - err = getChildrenList(&menu.Children[i], sql) + err = getChildrenList(&menu.Children[i], treeMap) } return err } @@ -46,14 +63,12 @@ func getChildrenList(menu *model.SysMenu, sql string) (err error) { // @return list interface{} // @return total int -func GetInfoList(info request.PageInfo) (err error, list interface{}, total int) { - limit := info.PageSize - offset := info.PageSize * (info.Page - 1) - db := global.GVA_DB +func GetInfoList() (err error, list interface{}, total int) { var menuList []model.SysBaseMenu - err = db.Limit(limit).Offset(offset).Where("parent_id = 0").Order("sort", true).Find(&menuList).Error + err,treeMap := getBaseMenuTreeMap() + menuList = treeMap["0"] for i := 0; i < len(menuList); i++ { - err = getBaseChildrenList(&menuList[i]) + err = getBaseChildrenList(&menuList[i],treeMap) } return err, menuList, total } @@ -64,10 +79,10 @@ func GetInfoList(info request.PageInfo) (err error, list interface{}, total int) // @param menu *model.SysBaseMenu // @return err error -func getBaseChildrenList(menu *model.SysBaseMenu) (err error) { - err = global.GVA_DB.Where("parent_id = ?", menu.ID).Order("sort", true).Find(&menu.Children).Error +func getBaseChildrenList(menu *model.SysBaseMenu ,treeMap map[string][]model.SysBaseMenu) (err error) { + menu.Children = treeMap[strconv.Itoa(int(menu.ID))] for i := 0; i < len(menu.Children); i++ { - err = getBaseChildrenList(&menu.Children[i]) + err = getBaseChildrenList(&menu.Children[i],treeMap) } return err } @@ -89,6 +104,22 @@ func AddBaseMenu(menu model.SysBaseMenu) (err error) { return err } +// @title getBaseMenuTreeMap +// @description 获取路由总树map +// @auth qm (2020/05/06 10:26) +// @return err error +// @return menusMsp map{string}[]SysBaseMenu + +func getBaseMenuTreeMap()(err error,treeMap map[string][]model.SysBaseMenu){ + var allMenus []model.SysBaseMenu + treeMap = make(map[string][]model.SysBaseMenu) + err = global.GVA_DB.Order("sort", true).Find(&allMenus).Error + for _,v := range allMenus{ + treeMap[v.ParentId] = append(treeMap[v.ParentId], v) + } + return err,treeMap +} + // @title GetBaseMenuTree // @description 获取基础路由树 // @auth (2020/04/05 20:22) @@ -96,9 +127,10 @@ func AddBaseMenu(menu model.SysBaseMenu) (err error) { // @return menus []SysBaseMenu func GetBaseMenuTree() (err error, menus []model.SysBaseMenu) { - err = global.GVA_DB.Where(" parent_id = ?", 0).Order("sort", true).Find(&menus).Error + err,treeMap := getBaseMenuTreeMap() + menus = treeMap["0"] for i := 0; i < len(menus); i++ { - err = getBaseChildrenList(&menus[i]) + err = getBaseChildrenList(&menus[i],treeMap) } return err, menus } diff --git a/web/src/store/module/router.js b/web/src/store/module/router.js index 168e98ba..548a42e1 100644 --- a/web/src/store/module/router.js +++ b/web/src/store/module/router.js @@ -6,7 +6,7 @@ import { asyncMenu } from '@/api/menu' const formatRouter = (routes) => { routes && routes.map(item => { item.meta.hidden = item.hidden - if (item.children.length > 0) { + if (item.children && item.children.length > 0) { formatRouter(item.children) } }) diff --git a/web/src/view/layout/aside/asideComponent/index.vue b/web/src/view/layout/aside/asideComponent/index.vue index cb9fdddf..004f1e66 100644 --- a/web/src/view/layout/aside/asideComponent/index.vue +++ b/web/src/view/layout/aside/asideComponent/index.vue @@ -1,6 +1,6 @@