134 lines
5.2 KiB
JavaScript
134 lines
5.2 KiB
JavaScript
import Vue from 'vue'
|
|
import router from './router'
|
|
import store from './store'
|
|
import NProgress from 'nprogress' // progress bar
|
|
import 'nprogress/nprogress.css' // progress bar style
|
|
// import notification from 'ant-design-vue/es/notification'
|
|
import { ACCESS_TOKEN, INDEX_MAIN_PAGE_PATH,USER_INFO } from '@/store/mutation-types'
|
|
import { generateIndexRouter } from '@/utils/util'
|
|
import { menuPermission } from './utils/hasPermission'
|
|
import {loginIdmPlatform, getUserInfo} from '@api/api'
|
|
import {notification} from 'ant-design-vue'
|
|
import { filterTreeData, isExternalTerminal } from './utils/util'
|
|
|
|
NProgress.configure({ showSpinner: false }) // NProgress Configuration
|
|
// ,'/signupentrance','/signUpPage'
|
|
|
|
const whiteList = ['/user/login', '/user/register', '/user/register-result', '/user/alteration'] // no redirect whitelist, '/screen'
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
// 会员系统的单点登录
|
|
if (to.query.token) {
|
|
Vue.ls.set(ACCESS_TOKEN, to.query.token, 7 * 24 * 60 * 60 * 1000)
|
|
store.commit('SET_TOKEN', to.query.token)
|
|
// 通过token获取登录的用户信息
|
|
let userInfoFromUrlResult = await getUserInfo()
|
|
if(userInfoFromUrlResult.success) {
|
|
Vue.ls.set(USER_INFO, userInfoFromUrlResult.result , 7 * 24 * 60 * 60 * 1000)
|
|
store.commit('SET_INFO', userInfoFromUrlResult.result)
|
|
}
|
|
// 判断是否是会员系统iframe嵌入的,如果是 不先做左侧菜单和 头部标签
|
|
if(to.query.system === 'member') {
|
|
store.commit('SET_SYSTEM', to.query.system)
|
|
}
|
|
}
|
|
// idm系统单点登录
|
|
if(to.path === '/user/login' && to.query.code){
|
|
let result = await loginIdmPlatform({code:to.query.code})
|
|
if(result.success){
|
|
// TODO 判断当前用户是否是领导角色,如果是的话跳转到内网的网址的首页 并用token 登录
|
|
let userInfo = result.result.userInfo
|
|
const token = result.result.token
|
|
// 领导角色需要跳转到内网,
|
|
if(userInfo.roleCode.includes('bmfzr') || userInfo.roleCode.includes('sld') || userInfo.roleCode.includes('admin')) {
|
|
window.open(`${window._CONFIG.intranetUrl}dashboard/analysis?token=${token}`, '_self')
|
|
} else {
|
|
Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
|
|
Vue.ls.set(ACCESS_TOKEN, token, 7 * 24 * 60 * 60 * 1000)
|
|
}
|
|
}else {
|
|
notification['error']({
|
|
message: '登录失败',
|
|
description: result.message,
|
|
duration: 4,
|
|
})
|
|
Vue.ls.remove(ACCESS_TOKEN)
|
|
Vue.ls.remove(USER_INFO)
|
|
next({ path: '/user/login' })
|
|
return
|
|
}
|
|
}
|
|
|
|
NProgress.start() // start progress bar
|
|
|
|
if (Vue.ls.get(ACCESS_TOKEN)) {
|
|
/* has token */
|
|
if (to.path === '/user/login') {
|
|
next({ path: INDEX_MAIN_PAGE_PATH })
|
|
NProgress.done()
|
|
} else {
|
|
if (store.getters.permissionList.length === 0) {
|
|
store.dispatch('GetPermissionList').then(res => {
|
|
let menuData = res.result.menu
|
|
if (menuData === null || menuData === '' || menuData === undefined) {
|
|
return
|
|
}
|
|
let constRoutes = []
|
|
if(isExternalTerminal) {
|
|
// 为外部系统的时候,过滤掉所有的 isExternalTerminal = false 的数据
|
|
menuData = filterTreeData(menuData)
|
|
}
|
|
constRoutes = generateIndexRouter(menuData)
|
|
// 添加主界面路由
|
|
store.dispatch('UpdateAppRouter', { constRoutes }).then(() => {
|
|
// 根据roles权限生成可访问的路由表
|
|
// 动态添加可访问路由表
|
|
router.addRoutes(store.getters.addRouters)
|
|
|
|
let redirect = decodeURIComponent(from.query.redirect || to.path)
|
|
let opeMenuData = JSON.parse(JSON.stringify(menuData))
|
|
if(!menuPermission(opeMenuData, redirect)) {
|
|
const firstRouter = constRoutes[0].redirect
|
|
redirect = firstRouter
|
|
}
|
|
console.log('redirect--------', redirect)
|
|
console.log('to.path--------', to.path)
|
|
// const redirect = decodeURIComponent(from.query.redirect || to.path)
|
|
// 大屏的路由跳转 to.path === redirect || to.path === '/screen'
|
|
if (to.path === redirect) {
|
|
// hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
|
|
next({ ...to, replace: true })
|
|
} else {
|
|
// 跳转到目的路由
|
|
next({ path: redirect })
|
|
}
|
|
})
|
|
})
|
|
.catch(() => {
|
|
/* notification.error({
|
|
message: '系统提示',
|
|
description: '请求用户信息失败,请重试!'
|
|
})*/
|
|
store.dispatch('Logout').then(() => {
|
|
next({ path: '/user/login', query: { redirect: to.fullPath } })
|
|
})
|
|
})
|
|
} else {
|
|
next()
|
|
}
|
|
}
|
|
} else {
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
// 在免登录白名单,直接进入
|
|
next()
|
|
} else {
|
|
next({ path: '/user/login', query: { redirect: to.fullPath } })
|
|
NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
|
|
}
|
|
}
|
|
})
|
|
|
|
router.afterEach(() => {
|
|
NProgress.done() // finish progress bar
|
|
})
|