import router from './router' import store from './store' import { MessageBox, Message } from 'element-ui' import NProgress from 'nprogress' import 'nprogress/nprogress.css' import methods from '@/utils/methodsTool'; import { getToken, setToken } from '@/utils/auth' import { checkSession, spotCheckFunc } from '@/utils/spotCheck' import $api from "@/api/api" NProgress.configure({ showSpinner: false }) let spotCheckData = {} const whiteList = ['/login', '/auth-redirect', '/bind', '/register'] router.beforeEach(async (to, from, next) => { if (to.path !== from.path) { NProgress.start(); } if (to.path === '/login' && to.query.account) { await store.dispatch('oldLogin', to.query) } else { if (to.path === '/login' && !methods.getQueryVariable('TenantId')) { sessionStorage.removeItem('TenantId') } if (methods.getQueryVariable('TenantId')) { sessionStorage.TenantId = methods.getQueryVariable('TenantId') } else if (!sessionStorage.TenantId && !(to.path.includes('/spotCheck') || to.path.includes('/spotCheck2'))) { await store.dispatch('findTenantId') } } //监管人员抽查处理 if (to.path.includes('/spotCheck') || to.path.includes('/spotCheck2')) { let array = to.path.split('/').filter(i => !!i) sessionStorage.TenantId = array[1] if (to.query.data) { to.query.data = to.query.data.replace(/\s/g, '+') store.dispatch('FedLogOut') await new Promise((resolve) => { let url = '' switch (array[0]) { case "spotCheck": url = 'commonfreedecryptOfficialInfo' break; case "spotCheck2": url = 'commonfreedecryptSevenOfficialInfo' break; default: break; } $api[url]({ data: to.query.data }).then(res => { let data = { userId: res.data.userId, realName: res.data.realName, id: res.data.gradeId, className: res.data.className, goodsId: res.data.goodsId, goodsName: res.data.goodsName, orderGoodsId: res.data.orderGoodsId, fullName: res.data.fullName, token: res.data.token, keyId: `${res.data.userId}-${res.data.goodsId}-${res.data.gradeId}-${res.data.orderGoodsId}`, }; if (res.data.tenantName) { store.state.user.companyName = res.data.tenantName document.title = res.data.tenantName } sessionStorage.TenantId = res.data.tenantId setToken(data.token) store.commit('SET_TOKEN', data.token) spotCheckData = data resolve() }).catch(() => { Message.error("数据错误,解析失败") store.dispatch('FedLogOut') next(`/login?TenantId=${sessionStorage.TenantId}`) }) }) } } if (sessionStorage.TenantId && !store.state.user.companyName) { store.dispatch('footerData') } if (getToken()) { to.meta.title && store.dispatch('settings/setTitle', to.meta.title) /* has token*/ if (to.path === '/login') { next({ path: '/' }) NProgress.done() } else { if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息 store.dispatch('GetInfo').then(() => { store.dispatch('GenerateRoutes').then(accessRoutes => { // 根据roles权限生成可访问的路由表 router.addRoutes(accessRoutes) // 动态添加可访问路由表 if (to.path.includes('/spotCheck')) { checkSession(spotCheckData) .then(() => { //学时审核 next({ path: '/classHoursReviews', replace: true }) }) .catch(() => { Message.error("存在异常,请联系开发人员") }); } else { next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 } }) }).catch(err => { store.dispatch('LogOut').then(() => { Message.error(err) next({ path: '/' }) }) }) } else { if (!to.path.includes('/user/profile')) { checkFunc() } if (!to.query.TenantId) { let query = to.query query.TenantId = sessionStorage.TenantId next({ path: to.path, query: query }) } else { next() } } } } else { // 没有token if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入 next() } else { if (sessionStorage.TenantId == undefined) { next(`/login`) // 否则全部重定向到登录页 } else { next(`/login?TenantId=${sessionStorage.TenantId}`) // 否则全部重定向到登录页 } NProgress.done() } } }) function checkFunc() { $api.inquireusercheckPwdTime().then(res => { if (res.data) { MessageBox.confirm('您已90天为修改密码,请前往修改密码', '系统提示', { confirmButtonText: '前往修改', showCancelButton: false, closeOnClickModal: false, closeOnPressEscape: false, showClose: false, type: 'warning' } ).then(() => { location.href = '/user/profile'; }).catch(() => { }); } }) } router.afterEach(() => { NProgress.done() })