From 26a38665b852433a428296917ef194e06532d011 Mon Sep 17 00:00:00 2001 From: vapao Date: Thu, 1 Apr 2021 11:51:51 +0800 Subject: [PATCH] fix issues --- spug_web/src/layout/index.js | 13 ++++++------- spug_web/src/libs/functools.js | 4 +++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/spug_web/src/layout/index.js b/spug_web/src/layout/index.js index 6d71c53..2742951 100644 --- a/spug_web/src/layout/index.js +++ b/spug_web/src/layout/index.js @@ -10,21 +10,19 @@ import Sider from './Sider'; import Header from './Header'; import Footer from './Footer' import routes from '../routes'; -import { updatePermissions, hasPermission } from 'libs'; +import { hasPermission } from 'libs'; import styles from './layout.module.less'; -function initRoutes(routes) { - const Routes = []; +function initRoutes(Routes, routes) { for (let route of routes) { if (route.component) { if (!route.auth || hasPermission(route.auth)) { Routes.push() } } else if (route.child) { - initRoutes(route.child) + initRoutes(Routes, route.child) } } - return Routes } // 404 @@ -47,8 +45,9 @@ export default function () { const [Routes, setRoutes] = useState([]); useEffect(() => { - updatePermissions(); - setRoutes(initRoutes(routes)); + const Routes = []; + initRoutes(Routes, routes); + setRoutes(Routes) }, []) return ( diff --git a/spug_web/src/libs/functools.js b/spug_web/src/libs/functools.js index ebf5bbf..dab88c7 100644 --- a/spug_web/src/libs/functools.js +++ b/spug_web/src/libs/functools.js @@ -4,6 +4,7 @@ * Released under the AGPL-3.0 License. */ let Permission = { + isReady: false, isSuper: false, hostPerms: [], permissions: [] @@ -13,6 +14,7 @@ export let X_TOKEN; export function updatePermissions() { X_TOKEN = localStorage.getItem('token'); + Permission.isReady = true; Permission.isSuper = localStorage.getItem('is_supper') === 'true'; Permission.hostPerms = JSON.parse(localStorage.getItem('host_perms') || '[]'); Permission.permissions = JSON.parse(localStorage.getItem('permissions') || '[]'); @@ -20,8 +22,8 @@ export function updatePermissions() { // 前端页面的权限判断(仅作为前端功能展示的控制,具体权限控制应在后端实现) export function hasPermission(strCode) { + if (!Permission.isReady) updatePermissions(); const {isSuper, permissions} = Permission; - // console.log(isSuper, strCode, permissions); if (!strCode || isSuper) return true; for (let or_item of strCode.split('|')) { if (isSubArray(permissions, or_item.split('&'))) {