You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.3 KiB

2 years ago
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
2 years ago
import {getToken, setToken} from '@/utils/auth' // get token from cookie
2 years ago
import getPageTitle from '@/utils/get-page-title'
2 years ago
import { ossLogin } from "@/api/h5"
2 years ago
NProgress.configure({ showSpinner: false }) // NProgress Configuration
2 years ago
const whiteList = ['/login','/h5/login','/print'] // no redirect whitelist
2 years ago
router.beforeEach(async(to, from, next) => {
// start progress bar
NProgress.start()
// set page title
document.title = getPageTitle(to.meta.title)
2 years ago
//osslogin
if (/^\/oss_login.*/.test(to.path)) {
if (to.query.token) {
const ossRes= await ossLogin({
oss_token: to.query.token
})
setToken(ossRes.user.bianma,"difficult_employee_token_h5")
next({
path: "/h5",
replace: true
})
}
return
}
2 years ago
// determine whether the user has logged in
2 years ago
let hasToken = getToken()
2 years ago
if (/^\/h5.*/.test(to.path)) {
hasToken = getToken("difficult_employee_token_h5")
} else {
hasToken = getToken()
}
console.log(hasToken)
2 years ago
2 years ago
if (whiteList.indexOf(to.path) !== -1) {
// in the free login whitelist, go directly
next()
return
}
2 years ago
if (hasToken && hasToken !== 'undefined') {
2 years ago
if (to.path === '/login' || to.path === '/h5/login') {
2 years ago
// if is logged in, redirect to the home page
2 years ago
let isH5 = to.path !== '/login'
next({ path: isH5 ? '/h5' : '/' })
2 years ago
NProgress.done()
} else {
// determine whether the user has obtained his permission roles through getInfo
const hasRoles = store.getters.roles && store.getters.roles.length > 0
if (hasRoles) {
next()
} else {
try {
// get user info
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
2 years ago
if (!/^\/h5.*/.test(to.path)) {
const { roles } = await store.dispatch('user/getInfo')
2 years ago
2 years ago
// generate accessible routes map based on roles
const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
2 years ago
2 years ago
//console.log(accessRoutes)
// dynamically add accessible routes
router.addRoutes(accessRoutes)
next({ ...to, replace: true })
} else {
next();
}
2 years ago
// hack method to ensure that addRoutes is complete
// set the replace: true, so the navigation will not leave a history record
2 years ago
2 years ago
} catch (error) {
console.log(error)
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')
Message.error(error || 'Has Error')
2 years ago
next(`${/^\/h5.*/.test(to.path) ? '/h5/login' : '/login'}?redirect=${to.path}`)
2 years ago
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.
2 years ago
2 years ago
next(`${/^\/h5.*/.test(to.path) ? '/h5/login' : '/login'}?redirect=${to.path}`)
2 years ago
NProgress.done()
}
}
})
router.afterEach(() => {
// finish progress bar
NProgress.done()
})