import { createRouter, createWebHistory, RouteRecordRaw, createWebHashHistory } from 'vue-router' import { defineAsyncComponent } from 'vue' import store from "@/store" import { Https } from "@/tool/https"; import { getCookie, setCookie } from "@/tool/cookie"; const _import = (path: string) => () => import(`../views/${path}.vue`); const _import_component = (path : string) => () => import(`../component/${path}`); const _import_custom = (path : string) => () => import(`../views/${path}`); // defineAsyncComponent(import(`../views/${path}.vue`)) const routes: Array = [ { path: "/", // redirect重定向 // redirect: "/Square" redirect: "/upgrade" }, { path: '/login', name: 'login', component: _import('LoginPage') }, // { // path: '/register', // name: 'register', // component: _import('RegisterPage') // }, { path: '/upgrade', name: 'upgrade', component: _import('Upgrade'), }, // { // path: '/home', // name: 'home', // component: _import('HomeView') // }, { path: '/home', name: 'home', component: _import('HomeMain'), children:[ { path: "", name:'HomeChil', redirect: "/home/homePage" }, { path:'homePage', name:'homePage', component: _import_custom('HomeView/HomeView.vue'), },{ path:'library', name:'library', component: _import_custom('HomeView/library.vue'), },{ path:'history', name:'history', component: _import_custom('HomeView/history.vue'), },{ path:'works', name:'works', component: _import_custom('HomeView/Works.vue'), },{ path:'events', name:'events', component: _import_custom('HomeView/Events.vue'), },{ path:'eventsDetail', name:'eventsDetail', component: _import_component('Events/eventsDetail.vue'), },{ path:'account', name:'account', component: _import_component('Account/account.vue'), children:[ { path: "", name:'accountChil', redirect: "/home/account/accountHome" }, { path:'accountHome', name:'accountHome', component: _import_component('Account/accountHome.vue'), }, { path:'accountEdit', name:'accountEdit', component: _import_component('Account/accountEdit.vue'), }, { path:'accountMessage', name:'accountMessage', component: _import_component('Account/accountMessage.vue'), }, { path:'accountFollowFans', name:'accountFollowFans', component: _import_component('Account/accountFollowFans.vue'), } ] },{ path:'otherUsers', name:'otherUsers', component: _import_component('Account/otherUsers.vue'), } ] }, { path: '/Square', name: "HomeRecommend", component: _import('HomeRecommend'), children:[ { path: "", name:'SquareChil', redirect: "/Square/works" }, { path:'works', name:'SquareWorks', component: _import_custom('HomeView/Works.vue'), },{ path:'events', name:'SquareEvents', component: _import_custom('HomeView/Events.vue'), },{ path:'eventsDetail', name:'SquareEventsDetail', component: _import_component('Events/eventsDetail.vue'), } ] }, { path: '/demo', name: 'demo', component: _import('Demo') }, // { // path: '/history', // name: 'history', // component: _import('history') // }, // { // path: '/testClickData', // name: 'testClickData', // component: _import('TestClickData') // }, { path: '/administrator', name: 'administrator', component: _import('Administrator'), children:[ { path: "", name:'adminChil', redirect: "/administrator/allUser" }, { path:'allUser', name:'allUser', component: _import_component('Administrator/allUser.vue'), }, { path:'testClickData', name:'testClickData', component: _import_component('Administrator/TestClickData.vue'), }, { path:'trialApproval', name:'trialApproval', component: _import_component('Administrator/trialApproval.vue'), },{ path:'questionnaire', name:'questionnaire', component: _import_component('Administrator/questionnaire.vue'), },{ path:'recentActiveChart', name:'recentActiveChart', component: _import_component('Administrator/recentActiveChart.vue'), },{ path:'recentActiveUser', name:'recentActiveUser', component: _import_component('Administrator/recentActiveUser.vue'), },{ path:'recentActiveUserChart', name:'recentActiveUserChart', component: _import_component('Administrator/recentActiveUserChart.vue'), },{ path:'recentNewUser', name:'recentNewUser', component: _import_component('Administrator/recentNewUser.vue'), },{ path:'recentNewUserChart', name:'recentNewUserChart', component: _import_component('Administrator/recentNewUserChart.vue'), },{ path:'trialUserCountry', name:'trialUserCountry', component: _import_component('Administrator/trialUserCountry.vue'), },{ path:'trialUserConversionRateChart', name:'trialUserConversionRateChart', component: _import_component('Administrator/trialUserConversionRateChart.vue'), },{ path:'trialAllUser', name:'trialAllUser', component: _import_component('Administrator/trialAllUser.vue'), }, ] }, { path: '/paySucceed', name: 'paySucceed', component: _import('paySucceed') }, // { // path: '/library', // name: 'library', // component: _import('library') // }, // { // path: '/trialApproval', // name: 'trialApproval', // component: _import('trialApproval') // }, { path: '/setIdentification', name: 'setIdentification', component: _import('setIdentification') }, { path: '/feedbackSurvey', name: 'feedbackSurvey', component: _import('feedbackSurvey'), }, { path: '/feedbackSurveyCN', name: 'feedbackSurveyCN', component: _import('feedbackSurveyCN'), }, { path: '/emailVerify', name: 'emailVerify', component: _import('emailVerify'), }, { path: '/404', name: '404', component: _import('404') }, { path: "/:catchAll(.*)", redirect: "/404", }, ] const router = createRouter({ history: createWebHistory(process.env.BASE_URL), // history: createWebHashHistory(), routes }) // 刷新保存数据- let state:any = store.state window.addEventListener("beforeunload", (e) => { localStorage.setItem( "vuex_setSystemUser", JSON.stringify(state.UserHabit.systemUser.value) ); localStorage.setItem( "vuex_setUserInfo", JSON.stringify(state.UserHabit.userInfo) ); }); var vuex_setSystemUser:any = localStorage.getItem("vuex_setSystemUser"); var vuex_setUserInfo:any = localStorage.getItem("vuex_setUserInfo"); if (vuex_setSystemUser == 0 || vuex_setSystemUser == 1) { store.commit("setSystemUser", JSON.parse(vuex_setSystemUser)); localStorage.removeItem("vuex_setSystemUser"); } if (vuex_setUserInfo) { store.commit("setUserInfo", JSON.parse(vuex_setUserInfo)); localStorage.removeItem("vuex_setSystemUser"); } let setMurmur = (id:any)=> { let murmurStr: any = localStorage.getItem('murmurStr') // let isSxis = false let data = { browserIdentifiers: murmurStr, id:id } if(data.id){ Https.axiosPost(Https.httpUrls.noLoginRequired, data) .then((rv) => { let isTest = rv.systemUser == 3 ? true : false let isBeginner = rv.isBeginner == 1 ? true : false setCookie("isMurmur", true); setCookie("token", rv.token); setCookie("isTest", isTest); setCookie("isBeginner", isBeginner); setCookie("isBeginnerNum", 0);//从第一步开始,机器人开始的话就是从第二部开始 setCookie("userInfo", JSON.stringify(rv)); sessionStorage.setItem('isTimeOne', JSON.stringify(false));//是否需要公告 提示 弹窗 let randomNum: any = Math.floor(Math.random() * 9000000000000000) + 1000000000000000; sessionStorage.setItem('sessionId', randomNum); router.push("/home"); }) .catch((res) => { // router.push('/Square') }); } } router.beforeEach((to:any, from, next) => { let upgradeList = ['/feedbackSurvey','/feedbackSurveyCN','emailVerify']//指定页面系统维护也可以访问 // // 系统维护 // const toName = to.name === 'upgrade'; // if(upgradeList.indexOf(to.path) > -1){ // next(); // }else{ // if (toName) { // next(); // } else { // next({ name: 'upgrade' }); // } // } // return // 检查路由是否存在 // 机房用户 let herfData = window.location.search.substring(1) if(herfData.split('=')[0] == 'noLogin' && to.name != 'homePage'){ setMurmur(herfData.split('=')[1]) // console.log(`-------------------成功${herfData.split('=')[1]}-------------------`) return } 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 = [88,6,46,31,73,83,87,4] let isSystemUserRouteList = ['/login','/Square']//游客用户只能进入这两个页面 let systemUser = state.UserHabit.systemUser const routeExists = router.getRoutes().some(({ name }) =>{ if(name){ return (name === to.name || to.path == '/') }else{ return false } }); if(upgradeList.indexOf(to.path) > -1){//指定页面任何用户都可以进入 next(); return } if(systemUser.value == 0){//游客用户只能进入这两个页面 let sSystemUser = false for (let index = 0; index < isSystemUserRouteList.length; index++) { if(to.path.indexOf(isSystemUserRouteList[index]) > -1){ sSystemUser = true break } } if(sSystemUser){ next(); }else{ next('/Square'); } return } if (isMurmur && murmurStr && token) { const toName = to.name === 'login'; if (toName) { return next({ name: '/home' });//机房用户 } } else { if (routeList.indexOf(to.path) > -1 ) {//指定也买你必须指定用户可以进入 if(userIdList.indexOf(userInfo.userId) > -1){ }else{ return next({ name: '/404' }); } } // 如果页面存在,正常跳转 } next(); // if (routeExists) {//检测档期那页面是否存在 // } else { // // 如果页面不存在,可以跳转到404页面或者其他页面 // next('/404'); // } }); export default router