Skip to content

Commit

Permalink
fix(server-client): add dynamic router hook
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 2, 2021
1 parent 9d973f0 commit a4a3923
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/system-client/src/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
import store from './store'

import { Message } from 'element-ui'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

Expand All @@ -23,6 +26,35 @@ router.beforeEach(async(to, from, next) => {
// if is logged in, redirect to the home page
next({ path: '/' })
NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
} else if (to.path.startsWith('/app/')) {
console.log('router', to.path)
if (store.state.app.application) {
next()
} else {
let appid = to.params.appid
// parse appid manually while refreshing page
if (!appid) {
const arr = to.path.split('/')
appid = arr[2]
}

// load the application
try {
await store.dispatch('app/loadCurrentApplication', appid)
} catch (error) {
console.error(`failed to load application: ${appid}`, error)
Message('加载应用信息出错,请重试')
}

const roles = store.state.app.roles
const permissions = store.state.app.permissions
const accessRoutes = await store.dispatch('permission/generateRoutes', { appid, roles, permissions })
router.addRoutes(accessRoutes)

// hack method to ensure that addRoutes is complete
// set the replace: true, so the navigation will not leave a history record
next({ ...to, replace: true })
}
} else {
next()
}
Expand Down

0 comments on commit a4a3923

Please sign in to comment.