xiuos_IoT/xiuosiot-frontend/src/permission.js

82 lines
2.9 KiB
JavaScript

/*
* @Author: 龚祖望 573413756@qq.com
* @Date: 2022-05-16 09:16:41
* @LastEditors: 龚祖望 573413756@qq.com
* @LastEditTime: 2023-03-08 15:57:02
* @FilePath: \dashengda\src\permission.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import router from './router'
import store from './store'
import { Message } from 'element-ui'
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'
NProgress.configure({ showSpinner: false }) // NProgress Configuration
const whiteList = ['/login', '/home', '/product/M168', '/product/RV400', '/product/RV400-SR100', '/product/M168-SM100',
'/product/RV400-AR100', '/product/RV400-AR100/instruction', '/product/RV400-CR100', '/product/M528-CM100', '/product/edu-arm', '/product/edu-riscv64', '/product/M528',
'/solution/shengda', '/solution/qianjiang', '/configuration/development', '/configuration/transferStation',
'/configuration/AiCity', '/configuration/bridgeMachine'] // no redirect whitelist
router.beforeEach(async(to, from, next) => {
// start progress bar
NProgress.start()
// set page title
document.title = getPageTitle(to.meta.title)
// determine whether the user has logged in
const hasToken = getToken()
if (hasToken) {
if (to.path === '/login') {
// if is logged in, redirect to the home page
next({ path: '/overview' })
NProgress.done()
} else {
const corpName = store.getters.corpName
if (corpName) {
next()
NProgress.done()
} else {
try {
const res = await store.dispatch('user/getInfo')
store.dispatch('user/generateRoutes', res).then(() => {
router.addRoutes(store.getters.asyncRouters)
next({ ...to, replace: true })
}).catch(async err => {
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')
Message.error(err || 'Has Error')
next(`/login?redirect=${to.path}`)
NProgress.done()
})
} catch (error) {
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')
Message.error(error || 'Has Error')
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
}
} else {
/* has no token*/
if (whiteList.indexOf(to.path) !== -1) {
// in the free login whitelist, go directly
next()
} else {
// other pages that do not have permission to access are redirected to the login page.
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
})
router.afterEach(() => {
// finish progress bar
NProgress.done()
})