import Vue from 'vue' import router, { resetRouter } 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, USER_NAME, USER_INFO, UI_CACHE_DB_DICT_DATA, INDEX_MAIN_PAGE_PATH } from '@/store/mutation-types' import { generateIndexRouter } from '@/utils/util' import { JSEncrypt } from 'jsencrypt' import { getAction } from '@/api/manage' import { welcome } from '@/utils/util' import { Dialog } from 'vant' let isPhone = localStorage.getItem('isPhone') function isBrowser() { const userAgent = window.navigator.userAgent.toLowerCase() return userAgent.includes('lark') || userAgent.includes('feishu') } NProgress.configure({ showSpinner: false }) // NProgress Configuration // TODO 线上部署在conmponetns/tools/UserMenu.vue的退出登录时需解开代码以及注释代码 // TODO 在部署线上时注掉以下代码 const whiteList = ['/user/login', '/user/register', '/user/register-result', '/user/alteration'] // TODO 在部署线上时解开一下代码 // const whiteList = [] // no redirect whitelist router.beforeEach((to, from, next) => { NProgress.start() // start progress bar if (isPhone == 'yes'){ if (!isBrowser()){ Dialog.alert({ message: '未授权用户
Unauthorized User', theme: 'round-button', }).then(() => { // on close }); return } } if (to.query.code) { getAction(`opensso/callback`, { code: to.query.code }).then(res => { if (res.success) { Vue.ls.set(ACCESS_TOKEN, res.result.token, 7 * 24 * 60 * 60 * 1000) Vue.ls.set(USER_NAME, res.result.userInfo.username, 7 * 24 * 60 * 60 * 1000) Vue.ls.set(USER_INFO, res.result.userInfo, 7 * 24 * 60 * 60 * 1000) Vue.ls.set(UI_CACHE_DB_DICT_DATA, res.result.sysAllDictItems, 7 * 24 * 60 * 60 * 1000) Vue.ls.set('domianWebSocketURL', res.result.domianWebSocketURL) Vue.ls.set('domianWebImgURL', res.result.domianWebImgURL) Vue.ls.set('onlinePreviewDomainURL', res.result.onlinePreviewDomainURL) Vue.ls.set('httpsOnlinePreviewDomainURL', res.result.httpsOnlinePreviewDomainURL) Vue.ls.set('domianPreviewURL', res.result.domianPreviewURL) window._CONFIG['domianPreviewURL'] = res.result.domianPreviewURL + '/jero-boot' window._CONFIG['domianWebSocketURL'] = res.result.domianWebSocketURL + '/jero-boot' window._CONFIG['onlinePreviewDomainURL'] = res.result.onlinePreviewDomainURL + '/onlinePreview' window._CONFIG['domianWebImgURL'] = res.result.domianWebImgURL + '/jero-boot' window._CONFIG['httpsOnlinePreviewDomainURL'] = res.result.httpsOnlinePreviewDomainURL + '/onlinePreview' store.commit('SET_TOKEN', res.result.token) store.commit('SET_INFO', res.result.userInfo) store.commit('SET_NAME', { username: res.result.userInfo.username, realname: res.result.userInfo.realname, welcome: welcome() }) store.commit('SET_AVATAR', res.result.userInfo.avatar) let fullPath = '' if (to.fullPath == '/') { if (isPhone == 'yes') { fullPath = '/phoneSearch' } else { fullPath = INDEX_MAIN_PAGE_PATH } } else { fullPath = to.path } let query = to.query let status = {} if (query.state) { status = JSON.parse(query.state) } delete query.code next({ path: fullPath, query: status }) } }) return } if (Vue.ls.get(ACCESS_TOKEN)) { /* has token */ if (to.path === '/user/login') { let fullPath = '' if (isPhone == 'yes') { fullPath = '/phoneSearch' } else { fullPath = INDEX_MAIN_PAGE_PATH } next({ path: fullPath }) NProgress.done() } else { if (store.getters.permissionList.length === 0) { store.dispatch('GetPermissionList').then(res => { const menuData = res.result.menu if (menuData === null || menuData === '' || menuData === undefined) { return } resetRouter() let constRoutes = [] constRoutes = generateIndexRouter(menuData) // 添加主界面路由 store.dispatch('UpdateAppRouter', { constRoutes }).then(() => { // 根据roles权限生成可访问的路由表 // 动态添加可访问路由表 let redirect = decodeURIComponent(from.query.redirect || to.path) if (isPhone == 'yes') { router.addRoutes([{ 'path': '*', 'redirect': '/404', 'hidden': true }]) if (redirect == '/') { redirect = '/phoneSearch' } } else { router.addRoutes(store.getters.addRouters) } 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: '请求用户信息失败,请重试!' })*/ // query: { redirect: to.fullPath } store.dispatch('Logout').then(() => { // TODO 在部署线上时注掉此代码 next({ path: '/user/login', query: { redirect: to.fullPath } }) let fullPath = '' if (to.fullPath == '/') { if (isPhone == 'yes') { fullPath = '/phoneSearch' } else { fullPath = INDEX_MAIN_PAGE_PATH } } else { fullPath = to.path } // TODO 在部署线上时解开此代码 // window.location.href = 'https://signin.nio.com/oauth2/authorize?client_id=100679&redirect_uri=' // + encodeURIComponent(window.loginUrl + fullPath) + '&response_type=code&state=' + JSON.stringify(to.query) //线上 }) }) } else { next() } } } else { if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入 next() } else { // TODO 在部署线上时注掉此代码 next({ path: '/user/login', query: { redirect: to.fullPath } }) let fullPath = '' if (to.fullPath == '/') { if (isPhone == 'yes') { fullPath = '/phoneSearch' } else { fullPath = INDEX_MAIN_PAGE_PATH } } else { fullPath = to.path } // TODO 在部署线上时解开此代码 // window.location.href = 'https://signin.nio.com/oauth2/authorize?client_id=100679&redirect_uri=' // + encodeURIComponent(window.loginUrl + fullPath) + '&response_type=code&state=' + JSON.stringify(to.query) //线上 NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it } } }) router.afterEach(() => { NProgress.done() // finish progress bar })