import router from './router' import store from './store' // import { Message } from 'element-ui' import NProgress from 'nprogress' import 'nprogress/nprogress.css' import { getToken } from '@/utils/auth' import Vue from 'vue' import { OSSclient } from './utils/oss' import WHITELIST from "./utils/whiteList" // import getPageTitle from '@/utils/get-page-title' NProgress.configure({ showSpinner: false }) const whiteList = WHITELIST store.state.trials.whiteList = whiteList; router.beforeEach(async (to, from, next) => { NProgress.start() // 设置页面标题 // document.title = getPageTitle(to.meta.title) // 确定用户是否已登录 const hasToken = getToken() Vue.prototype.toPath = to.path Vue.prototype.$path = [] if (hasToken) { if (to.path === '/login' || to.path === '/recompose' || to.path === '/email-recompose' || to.path === '/error' || to.path === '/ReviewersResearchForm' || to.path === '/ReviewersResearch') { if (to.path === '/ReviewersResearch') { await this.$store.dispatch('user/logout') await OSSclient() } if (to.path === '/ReviewersResearchForm') { await OSSclient() } next() NProgress.done() } else { await OSSclient() const hasGetUserInfo = store.getters.userId if (hasGetUserInfo) { next() } else { try { // 获取用户信息 await store.dispatch('user/getInfo') const accessRoutes = await store.dispatch('permission/generateRoutes') router.addRoutes(accessRoutes) next({ ...to, replace: true }) } catch (error) { // 删除token并进入登录页面以重新登录 await store.dispatch('user/resetToken') next(`/login?redirect=${to.path}`) NProgress.done() } } } } else { /* has no token*/ if (whiteList.indexOf(to.path) !== -1) { if (to.path === '/researchLogin') { console.log(to) const flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i) if (flag) { next(`/researchLogin_m?trialId=${to.query.trialId}&lang=${to.query.lang}`) } else { next() } } else { // 在免登录whiteList中,直接进入 if (to.path === '/readingDicoms' || to.path === '/noneDicomReading') { await OSSclient() } next() } } else { if (to.path === '/researchForm') { next(`/researchLogin?`) NProgress.done() } else if (to.path === '/researchDetail_m') { next(`/researchLogin_m?`) NProgress.done() } else if (to.path === '/ReviewersResearchForm') { next(`/ReviewersResearch?`) NProgress.done() } else { // 其他无权访问的页面将重定向到登录页面。 next(`/login?`) NProgress.done() } } } }) router.afterEach(() => { NProgress.done() })