Commit 0b68995bd907a05fa4f4e95290a194babe954674
1 parent
b7ce8589
fix:DEFECT-432 修复超级管理员分配菜单权限和按钮权限问题
Showing
1 changed file
with
112 additions
and
56 deletions
| @@ -11,12 +11,11 @@ import projectSetting from '/@/settings/projectSetting'; | @@ -11,12 +11,11 @@ import projectSetting from '/@/settings/projectSetting'; | ||
| 11 | import { PermissionModeEnum } from '/@/enums/appEnum'; | 11 | import { PermissionModeEnum } from '/@/enums/appEnum'; |
| 12 | import { asyncRoutes } from '/@/router/routes'; | 12 | import { asyncRoutes } from '/@/router/routes'; |
| 13 | import { ERROR_LOG_ROUTE, PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic'; | 13 | import { ERROR_LOG_ROUTE, PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic'; |
| 14 | -import { filter } from '/@/utils/helper/treeHelper'; | ||
| 15 | -import { getMenuList } from '/@/api/sys/menu'; | 14 | +import { filter, forEach } from '/@/utils/helper/treeHelper'; |
| 15 | +import { getMenuList, getMenusIdsByRoleId } from '/@/api/sys/menu'; | ||
| 16 | import { getPermCode } from '/@/api/sys/user'; | 16 | import { getPermCode } from '/@/api/sys/user'; |
| 17 | import { useMessage } from '/@/hooks/web/useMessage'; | 17 | import { useMessage } from '/@/hooks/web/useMessage'; |
| 18 | import { PageEnum } from '/@/enums/pageEnum'; | 18 | import { PageEnum } from '/@/enums/pageEnum'; |
| 19 | -import { router as navRouter } from '/@/router'; | ||
| 20 | import { MENU_LIST, USER_INFO_KEY } from '/@/enums/cacheEnum'; | 19 | import { MENU_LIST, USER_INFO_KEY } from '/@/enums/cacheEnum'; |
| 21 | import { getAuthCache, setAuthCache } from '/@/utils/auth'; | 20 | import { getAuthCache, setAuthCache } from '/@/utils/auth'; |
| 22 | import { createStorage } from '/@/utils/cache/index'; | 21 | import { createStorage } from '/@/utils/cache/index'; |
| @@ -90,8 +89,48 @@ export const usePermissionStore = defineStore({ | @@ -90,8 +89,48 @@ export const usePermissionStore = defineStore({ | ||
| 90 | this.lastBuildMenuTime = 0; | 89 | this.lastBuildMenuTime = 0; |
| 91 | }, | 90 | }, |
| 92 | async changePermissionCode() { | 91 | async changePermissionCode() { |
| 92 | + const filterMenu = (allMenuList, menuIdsList) => { | ||
| 93 | + return allMenuList | ||
| 94 | + .filter((item) => { | ||
| 95 | + return menuIdsList.indexOf(item.id) > -1; | ||
| 96 | + }) | ||
| 97 | + .map((subItem) => { | ||
| 98 | + subItem = Object.assign({}, subItem); | ||
| 99 | + if (subItem.children) { | ||
| 100 | + subItem.children = filterMenu(subItem.children, menuIdsList); | ||
| 101 | + } | ||
| 102 | + return subItem; | ||
| 103 | + }); | ||
| 104 | + }; | ||
| 105 | + const userInfo: any = getAuthCache(USER_INFO_KEY); | ||
| 106 | + const isSysAdmin = 'SYS_ADMIN'; | ||
| 107 | + const routeList = (await getMenuList(2)) as AppRouteRecordRaw[]; | ||
| 108 | + let getSysPermission = []; | ||
| 109 | + /** | ||
| 110 | + * 否则不是超级管理员-获取对应角色的权限列表 | ||
| 111 | + */ | ||
| 93 | const codeList = await getPermCode(); | 112 | const codeList = await getPermCode(); |
| 94 | this.setPermCodeList(codeList); | 113 | this.setPermCodeList(codeList); |
| 114 | + /** | ||
| 115 | + * 如果是超级管理员则获取对应权限列表 | ||
| 116 | + */ | ||
| 117 | + if (userInfo.roles.includes(isSysAdmin) || userInfo.realName == '超级管理员') { | ||
| 118 | + const getMenuIds = await getMenusIdsByRoleId(userInfo.plainRoles[0].roleId); | ||
| 119 | + //根据对应的使用者的菜单数组和所有菜单数组对象进行过滤,返回最终需要的菜单 | ||
| 120 | + const newMenu = filterMenu(routeList, getMenuIds); | ||
| 121 | + /** | ||
| 122 | + * 递归获取对应所有菜单的权限列表 | ||
| 123 | + */ | ||
| 124 | + function lookForAllId(data = [], arr = []) { | ||
| 125 | + for (const item of data) { | ||
| 126 | + arr.push(item.permission); | ||
| 127 | + if (item.children && item.children.length) lookForAllId(item.children, arr); | ||
| 128 | + } | ||
| 129 | + return arr; | ||
| 130 | + } | ||
| 131 | + getSysPermission = lookForAllId(newMenu); | ||
| 132 | + this.setPermCodeList(getSysPermission); | ||
| 133 | + } | ||
| 95 | }, | 134 | }, |
| 96 | async buildRoutesAction(): Promise<AppRouteRecordRaw[]> { | 135 | async buildRoutesAction(): Promise<AppRouteRecordRaw[]> { |
| 97 | const { t } = useI18n(); | 136 | const { t } = useI18n(); |
| @@ -183,70 +222,87 @@ export const usePermissionStore = defineStore({ | @@ -183,70 +222,87 @@ export const usePermissionStore = defineStore({ | ||
| 183 | // !Simulate to obtain permission codes from the background, | 222 | // !Simulate to obtain permission codes from the background, |
| 184 | // this function may only need to be executed once, and the actual project can be put at the right time by itself | 223 | // this function may only need to be executed once, and the actual project can be put at the right time by itself |
| 185 | let routeList: AppRouteRecordRaw[] = []; | 224 | let routeList: AppRouteRecordRaw[] = []; |
| 186 | - try { | ||
| 187 | - const userInfo = getAuthCache(USER_INFO_KEY); | ||
| 188 | - if (userInfo?.needSetPwd == true) { | ||
| 189 | - routeList = [ | ||
| 190 | - { | ||
| 191 | - name: 'routes.common.system.system', | ||
| 192 | - parentId: '', | ||
| 193 | - children: [ | ||
| 194 | - { | ||
| 195 | - id: 'a8ffa8c5-637e-476b-a9e6-b60cebe95718', | ||
| 196 | - createTime: '2021-09-10 20:50:55', | ||
| 197 | - updateTime: '2021-11-16 18:58:24', | ||
| 198 | - name: 'routes.common.system.modifyPassword', | ||
| 199 | - parentId: 'a8ffa8c5-637e-471b-a9e6-b60cebe95713', | ||
| 200 | - children: [], | ||
| 201 | - path: '/system/changePassword', | ||
| 202 | - type: 'SYSADMIN', | ||
| 203 | - permission: 'system:password:view', | ||
| 204 | - sort: 6, | ||
| 205 | - component: '/system/changePassword/index', | ||
| 206 | - meta: { | ||
| 207 | - icon: 'bx:bx-home', | ||
| 208 | - title: 'routes.common.system.modifyPassword', | ||
| 209 | - menuType: '1', | ||
| 210 | - ignoreKeepAlive: true, | ||
| 211 | - hideMenu: false, | ||
| 212 | - status: '0', | ||
| 213 | - }, | ||
| 214 | - redirect: '', | 225 | + const userInfo: any = getAuthCache(USER_INFO_KEY); |
| 226 | + const filterMenu = (allMenuList, menuIdsList) => { | ||
| 227 | + return allMenuList | ||
| 228 | + .filter((item) => { | ||
| 229 | + return menuIdsList.indexOf(item.id) > -1; | ||
| 230 | + }) | ||
| 231 | + .map((subItem) => { | ||
| 232 | + subItem = Object.assign({}, subItem); | ||
| 233 | + if (subItem.children) { | ||
| 234 | + subItem.children = filterMenu(subItem.children, menuIdsList); | ||
| 235 | + } | ||
| 236 | + return subItem; | ||
| 237 | + }); | ||
| 238 | + }; | ||
| 239 | + if (userInfo?.needSetPwd == true) { | ||
| 240 | + routeList = [ | ||
| 241 | + { | ||
| 242 | + name: 'routes.common.system.system', | ||
| 243 | + parentId: '', | ||
| 244 | + children: [ | ||
| 245 | + { | ||
| 246 | + id: 'a8ffa8c5-637e-476b-a9e6-b60cebe95718', | ||
| 247 | + createTime: '2021-09-10 20:50:55', | ||
| 248 | + updateTime: '2021-11-16 18:58:24', | ||
| 249 | + name: 'routes.common.system.modifyPassword', | ||
| 250 | + parentId: 'a8ffa8c5-637e-471b-a9e6-b60cebe95713', | ||
| 251 | + children: [], | ||
| 252 | + path: '/system/changePassword', | ||
| 253 | + type: 'SYSADMIN', | ||
| 254 | + permission: 'system:password:view', | ||
| 255 | + sort: 6, | ||
| 256 | + component: '/system/changePassword/index', | ||
| 257 | + meta: { | ||
| 258 | + icon: 'bx:bx-home', | ||
| 259 | + title: 'routes.common.system.modifyPassword', | ||
| 260 | + menuType: '1', | ||
| 261 | + ignoreKeepAlive: true, | ||
| 262 | + hideMenu: false, | ||
| 263 | + status: '0', | ||
| 215 | }, | 264 | }, |
| 216 | - ], | ||
| 217 | - path: '/system', | ||
| 218 | - type: 'SYSADMIN', | ||
| 219 | - permission: '', | ||
| 220 | - sort: 6, | ||
| 221 | - component: 'LAYOUT', | ||
| 222 | - meta: { | ||
| 223 | - icon: 'bx:bx-home', | ||
| 224 | - title: 'routes.common.system.system', | ||
| 225 | - status: '0', | ||
| 226 | - menuType: '0', | 265 | + redirect: '', |
| 227 | }, | 266 | }, |
| 228 | - redirect: '/system/systemManagement', | 267 | + ], |
| 268 | + path: '/system', | ||
| 269 | + type: 'SYSADMIN', | ||
| 270 | + permission: '', | ||
| 271 | + sort: 6, | ||
| 272 | + component: 'LAYOUT', | ||
| 273 | + meta: { | ||
| 274 | + icon: 'bx:bx-home', | ||
| 275 | + title: 'routes.common.system.system', | ||
| 276 | + status: '0', | ||
| 277 | + menuType: '0', | ||
| 229 | }, | 278 | }, |
| 230 | - ] as AppRouteRecordRaw[]; | ||
| 231 | - } else { | ||
| 232 | - this.changePermissionCode(); | ||
| 233 | - routeList = (await getMenuList(1)) as AppRouteRecordRaw[]; | ||
| 234 | - createStorage('MENU_LIST', JSON.stringify(routeList)); | ||
| 235 | - setAuthCache('MENU_LIST', routeList); | 279 | + redirect: '/system/systemManagement', |
| 280 | + }, | ||
| 281 | + ] as AppRouteRecordRaw[]; | ||
| 282 | + } else { | ||
| 283 | + this.changePermissionCode(); | ||
| 284 | + routeList = (await getMenuList(1)) as AppRouteRecordRaw[]; | ||
| 285 | + const isSysAdmin = 'SYS_ADMIN'; | ||
| 286 | + /** | ||
| 287 | + * 解决超级管理员分配菜单权限问题 | ||
| 288 | + */ | ||
| 289 | + if (userInfo.roles.includes(isSysAdmin) || userInfo.realName == '超级管理员') { | ||
| 290 | + const getMenuIds = await getMenusIdsByRoleId(userInfo.plainRoles[0].roleId); | ||
| 291 | + //根据对应的使用者的菜单数组和所有菜单数组对象进行过滤,返回最终需要的菜单 | ||
| 292 | + const newMenu = filterMenu(routeList, getMenuIds); | ||
| 293 | + routeList = newMenu; | ||
| 236 | } | 294 | } |
| 237 | - } catch (error) { | ||
| 238 | - console.error(error); | ||
| 239 | - } // Dynamically introduce components | 295 | + createStorage('MENU_LIST', JSON.stringify(routeList)); |
| 296 | + setAuthCache('MENU_LIST', routeList); | ||
| 297 | + } | ||
| 298 | + // Dynamically introduce components | ||
| 240 | routeList = transformObjToRoute(routeList); | 299 | routeList = transformObjToRoute(routeList); |
| 241 | - | ||
| 242 | // Background routing to menu structure | 300 | // Background routing to menu structure |
| 243 | const backMenuList = transformRouteToMenu(routeList); | 301 | const backMenuList = transformRouteToMenu(routeList); |
| 244 | this.setBackMenuList(backMenuList); | 302 | this.setBackMenuList(backMenuList); |
| 245 | - | ||
| 246 | // remove meta.ignoreRoute item | 303 | // remove meta.ignoreRoute item |
| 247 | routeList = filter(routeList, routeRemoveIgnoreFilter); | 304 | routeList = filter(routeList, routeRemoveIgnoreFilter); |
| 248 | routeList = routeList.filter(routeRemoveIgnoreFilter); | 305 | routeList = routeList.filter(routeRemoveIgnoreFilter); |
| 249 | - | ||
| 250 | routeList = flatMultiLevelRoutes(routeList); | 306 | routeList = flatMultiLevelRoutes(routeList); |
| 251 | routes = [PAGE_NOT_FOUND_ROUTE, ...routeList]; | 307 | routes = [PAGE_NOT_FOUND_ROUTE, ...routeList]; |
| 252 | break; | 308 | break; |