import Vue from 'vue' import Router from 'vue-router' import routes from './routers' import store from '@/store' import iView from 'iview' import { setToken, getToken, canTurnTo, setTitle } from '@/libs/util' import config from '@/config' const { homeName } = config const originalPush = Router.prototype.push Router.prototype.push = function push(location, onResolve, onReject) { if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject) return originalPush.call(this, location).catch(err => err) } Vue.use(Router) const router = new Router({ routes }) const LOGIN_PAGE_NAME = 'login' const turnTo = (to, access, next) => { // console.log(to); if (canTurnTo(to.name, access, routes)) { next() } // 有权限,可访问 else { next({ replace: true, name: 'error_401' }) } // 无权限,重定向到401页面 } let nameList = ['login', 'forgot', 'register', 'newList', 'newsDetail', 'UserAgreement', 'privacyPolicy', 'newPushDetail','customPhone'] //无权限控制的页面 router.beforeEach((to, from, next) => { // console.log(to.name); iView.LoadingBar.start() const token = getToken() if (!token && nameList.indexOf(to.name) < 0) { // console.log(1,to.name); // 未登录且要跳转的页面不是登录页 next({ name: LOGIN_PAGE_NAME // 跳转到登录页 }) } else if (!token && nameList.indexOf(to.name) > -1) { // console.log(2,to.name); // 未登陆且要跳转的页面是登录页 next() // 跳转 } else if (token && nameList.indexOf(to.name) > -1) { // 已登录且要跳转的页面是登录页 next() } else { // console.log(3,to.name); if ((to.path == '/financingLease' || to.path == '/financingReceivable' || to.path == '/financingTrust' || to.path == '/financingPrivate' || to.path == '/financingMortgage' || to.path == '/MovablePropertyRegistration' || to.path == '/SRRHaig' || to.path == '/enterpriseSemiconductor' || to.name == 'RRHaigDetailPage' || to.name == 'semiconductorPage') && localStorage.getItem('usergrade') == "免费版") { console.log('VIP页面'); store.state.showGoVipModal = true // that.$store.commit("getshowGoVipModal", true); return false } else if (to.path == '/testPage/myOrders' || to.path == '/semiconductorPage/myOrders' || to.path == '/maPage/myOrders' || to.path == '/investPage/myOrders' || to.path == '/RRHaigDetailPage/myOrders') { next({ name: 'myOrders' }) } else if (to.path == '/testPage/authority' || to.path == '/semiconductorPage/authority' || to.path == '/maPage/authority' || to.path == '/investPage/authority' || to.path == '/RRHaigDetailPage/authority') { next({ name: 'authority' }) } else if (to.path == '/testPage/changePassword' || to.path == '/semiconductorPage/changePassword' || to.path == '/maPage/changePassword' || to.path == '/investPage/changePassword' || to.path == '/RRHaigDetailPage/changePassword') { next({ name: 'changePassword' }) } else if (to.path == '/testPage/rechargeManagement' || to.path == '/semiconductorPage/rechargeManagement' || to.path == '/maPage/rechargeManagement' || to.path == '/investPage/rechargeManagement' || to.path == '/RRHaigDetailPage/rechargeManagement') { next({ name: 'rechargeManagement' }) } else if (to.path == '/testPage/consumptionRecords' || to.path == '/semiconductorPage/consumptionRecords' || to.path == '/maPage/consumptionRecords' || to.path == '/investPage/consumptionRecords' || to.path == '/RRHaigDetailPage/consumptionRecords') { next({ name: 'consumptionRecords' }) } else if (to.path == '/rzd') { window.open('https://rzd.rensofter.com/') return false } else if (to.path == '/fingpt') { window.open('https://fingpt.vip.cpolar.cn/') return false } if (store.state.user.hasGetInfo) { turnTo(to, store.state.user.access, next) } else { store.dispatch('getUserInfo').then(user => { // 拉取用户信息,通过用户权限和跳转的页面的name来判断是否有权限访问;access必须是一个数组,如:['super_admin'] ['super_admin', 'admin'] turnTo(to, user.Data.access, next) }).catch(() => { setToken('') next({ name: 'login' }) }) } } }) router.afterEach(to => { setTitle(to, router.app) iView.LoadingBar.finish() window.scrollTo(0, 0) }) export default router