158 lines
3.4 KiB
TypeScript
158 lines
3.4 KiB
TypeScript
import { createRouter, createWebHistory, RouteRecordRaw, createWebHashHistory } from 'vue-router'
|
||
import { defineAsyncComponent } from 'vue'
|
||
import { getBrowserInfo, murmur } from '@/tool/util'
|
||
import { getCookie, setCookie } from "@/tool/cookie";
|
||
const _import = (path: string) => defineAsyncComponent(() => import(`../views/${path}.vue`));
|
||
const _import_custom = (path : string) => defineAsyncComponent(() => import(`../component/${path}`));
|
||
|
||
const routes: Array<RouteRecordRaw> = [
|
||
{
|
||
path: "/",
|
||
// redirect重定向
|
||
redirect: "/login"
|
||
// redirect: "/upgrade"
|
||
},
|
||
{
|
||
path: '/login',
|
||
name: 'login',
|
||
component: _import('LoginPage')
|
||
},
|
||
{
|
||
path: '/upgrade',
|
||
name: 'upgrade',
|
||
component: _import('Upgrade'),
|
||
},
|
||
{
|
||
path: '/home',
|
||
name: 'home',
|
||
component: _import('HomeView')
|
||
},
|
||
{
|
||
path: '/demo',
|
||
name: 'demo',
|
||
component: _import('Demo')
|
||
},
|
||
{
|
||
path: '/history',
|
||
name: 'history',
|
||
component: _import('HistoryPage')
|
||
},
|
||
// {
|
||
// path: '/testClickData',
|
||
// name: 'testClickData',
|
||
// component: _import('TestClickData')
|
||
// },
|
||
{
|
||
path: '/administrator',
|
||
name: 'administrator',
|
||
component: _import('Administrator'),
|
||
children:[
|
||
{
|
||
path:'allUser',
|
||
name:'allUser',
|
||
component: _import_custom('Administrator/allUser.vue'),
|
||
},
|
||
{
|
||
path:'testClickData',
|
||
name:'testClickData',
|
||
component: _import_custom('Administrator/TestClickData.vue'),
|
||
},
|
||
{
|
||
path:'trialApproval',
|
||
name:'trialApproval',
|
||
component: _import_custom('Administrator/trialApproval.vue'),
|
||
},
|
||
]
|
||
},
|
||
// {//老版本history
|
||
// path: '/oldHistory',
|
||
// name: 'oldHistory',
|
||
// component: _import('OldHistoryPage')
|
||
// },
|
||
{
|
||
path: '/paySucceed',
|
||
name: 'paySucceed',
|
||
component: _import('paySucceed')
|
||
},
|
||
{
|
||
path: '/library',
|
||
name: 'library',
|
||
component: _import('LibraryPage')
|
||
},
|
||
// {
|
||
// path: '/trialApproval',
|
||
// name: 'trialApproval',
|
||
// component: _import('trialApproval')
|
||
// },
|
||
{
|
||
path: '/setIdentification',
|
||
name: 'setIdentification',
|
||
component: _import('setIdentification')
|
||
},
|
||
{
|
||
path: '/404',
|
||
name: '404',
|
||
component: _import('404')
|
||
},
|
||
]
|
||
|
||
const router = createRouter({
|
||
history: createWebHistory(process.env.BASE_URL),
|
||
// history: createWebHashHistory(),
|
||
routes
|
||
})
|
||
router.beforeEach((to, from, next) => {
|
||
// 系统维护
|
||
// const toName = to.name === 'upgrade';
|
||
// if (toName) {
|
||
// next();
|
||
// } else {
|
||
// next({ name: 'upgrade' });
|
||
// }
|
||
// 检查路由是否存在
|
||
// 机房用户
|
||
let userInfo = JSON.parse(getCookie("userInfo") as any);
|
||
let murmurStr: any = localStorage.getItem('murmurStr')
|
||
let getIsMurmur: any = getCookie("isMurmur")
|
||
let token = getCookie("token");
|
||
let isMurmur = JSON.parse(getIsMurmur)
|
||
let routeList = ['/testClickData','/trialApproval']//指定页面需要指定id才能进入
|
||
let userIdList = [83,88,6,87]
|
||
const routeExists = router.getRoutes().some(({ name }) =>{
|
||
if(name){
|
||
return name === to.name
|
||
}else{
|
||
return false
|
||
}
|
||
});
|
||
|
||
if (routeExists) {
|
||
if (isMurmur && murmurStr && token) {
|
||
const toName = to.name === 'login';
|
||
if (toName) {
|
||
next({ name: 'home' });
|
||
} else {
|
||
next();
|
||
}
|
||
} else {
|
||
if (routeList.indexOf(to.path) > -1 ) {
|
||
|
||
if(userIdList.indexOf(userInfo.userId) > -1){
|
||
next();
|
||
}else{
|
||
next({ name: '/404' });
|
||
}
|
||
}else{
|
||
next();
|
||
}
|
||
// 如果页面存在,正常跳转
|
||
}
|
||
} else {
|
||
// 如果页面不存在,可以跳转到404页面或者其他页面
|
||
next('/404');
|
||
}
|
||
|
||
|
||
});
|
||
export default router
|