index.js 8.05 KB
import Vue from 'vue'
import Router from 'vue-router'
import * as Cookies from 'js-cookie'

// NavigationDuplicated error https://github.com/vuejs/vue-router/issues/2881
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

/* module router */
import reportRouter from './modules/report'
import analysisRouter from './modules/analysis'
import faceAnalysisRouter from './modules/face-analysis'
import customerRouter from './modules/customer'
import managementRouter from './modules/back-stage-management'
import ipageRouter from './modules/ipage'
import videoShopTourRouter from './modules/videoShopTour'
import businessAnalysis from './modules/businessAnalysis.js'
import queueManagementRouter from './modules/queueManagement.js'
const router = new Router({
    routes: [{
            path: '/home',
            name: 'home',
            meta: {
                permissionPath: 'home'
            },
            component: () =>
                import ('@/views/home'),
            children: [
                ...reportRouter,
                ipageRouter,
                analysisRouter,
                customerRouter,
                faceAnalysisRouter,
                videoShopTourRouter,
                businessAnalysis,
                queueManagementRouter,
                {
                    path: '/dataasis',
                    meta: {
                        permissionPath: 'dataasis'
                    },
                    component: () =>
                        import ('@/views/DataAsis/dataasis'),
                    children: [{
                        path: '/dataasis/trafficasis',
                        name: 'trafficasis',
                        meta: {
                            permissionPath: 'dataasis'
                        },
                        component: () =>
                            import ('@/views/DataAsis/trafficAsis/trafficasis'),
                        children: [{
                            path: 'monthly',
                            name: 'ta_monthly',
                            meta: {
                                permissionPath: 'dataasis'
                            },
                            component: () =>
                                import ('@/views/DataAsis/trafficAsis/monthly'),
                        }, {
                            path: 'quarterly',
                            name: 'ta_monthly',
                            meta: {
                                permissionPath: 'dataasis'
                            },
                            component: () =>
                                import ('@/views/DataAsis/trafficAsis/quarterly'),
                        }]
                    }]
                },
                managementRouter
            ]
        },
        {
            path: '/',
            name: 'login',
            component: () => {
                if (['xincheng', 'sso'].includes(window._vionConfig.groupLabel)) {
                    return import ('@/views/loginSSO/login');
                }
                return import ('@/views/login/login');
            },
            children: []
        },
        {
            path: '/asus',
            name: 'asuslogin',
            component: () =>
                import ('@/views/loginAsus/loginAsus'),
            children: []
        },
        {
            path: '/historyCustomer',
            name: 'historyCustomer',
            meta: {
                permissionPath: 'historyCustomer'
            },
            component: () =>
                import ('@/views/Behavior/historyCustomer')
        },
        {
            path: '/repeatCustomerStore',
            name: 'repeatCustomerStore',
            meta: {
                permissionPath: 'repeatCustomerStore'
            },
            component: () =>
                import ('@/views/others-page/repeatCustomerStore'),
            children: []
        },
        {
            path: '/mark-staff',
            name: 'MarkStaff',
            meta: {
                permissionPath: 'mark-staff'
            },
            component: () =>
                import ('@/views/others-page/MarkStaff/index')
        },
        {
            path: '/xincheng',
            name: 'xinchenglogin',
            component: () =>
                import ('@/views/loginXincheng/loginXincheng'),
            children: []
        },
        {
            path: '/free',
            name: 'freelogin',
            component: () =>
                import ('@/views/Freelogin/freelogin'),
            children: []
        },
        {
            path: '/404',
            name: '404',
            component: () =>
                import ('@/views/error-page/404')
        },
        {
            path: '*',
            redirect: '/404'
        },
    ]
})

// window.getVersion = version => {
//   setInterval(() => {
//     if (
//       (localStorage.feVersion
//         && version !== localStorage.feVersion)
//       || (window.feVersion
//           && version !== window.feVersion)
//     ) {
//       // console.log('不相同-----', localStorage.feVersion, window.feVersion)
//       location.reload()
//     }
//     localStorage.feVersion = window.feVersion = version
//   }, 1000)
// }
const whiteList = [
    '/',
    '/asus',
    '/xincheng',
    '/free'
]

const errPageList = [
    '/404'
]

// 全局路由守卫
router.beforeEach((to, from, next) => {
    // if (!window.feVersion || !localStorage.feVersion) {
    //   let versionScript = document.createElement("script")
    //   const { origin, pathname } = location
    //   versionScript.src = origin + (pathname === '/' ? '' : pathname) + '/version.js?t=' + new Date().getTime()
    //   let s = document.getElementsByTagName("script")[0]
    //   s.parentNode.insertBefore(versionScript, s)
    // }
    // const toPath = to.path.slice(to.path.lastIndexOf('/') + 1)
    const firstPath = window.localStorage.getItem('firstPath')
    if (whiteList.indexOf(to.path) !== -1) {
        if (Cookies.get('atoken')) {
            if (window._vionConfig.allMenu) {
                router.push('/ipage');
            } else {
                const cacheMenuList = window.localStorage.getItem('menu')
                if (cacheMenuList) {
                    router.push('/' + (firstPath || 'ipage'));
                } else {
                    Cookies.remove('atoken')
                    next('/login');
                }
            }
        } else {
            next();
        }
    } else {
        if (Cookies.get('atoken')) {
            const isSuper = Cookies.get('user_type') === 'super'
            const cacheHasPermissionMenu = JSON.parse(window.localStorage.getItem('menuName')) || [];
            if (cacheHasPermissionMenu.indexOf(to.meta.permissionPath) !== -1 ||
                errPageList.indexOf(to.path) !== -1 ||
                isSuper ||
                window._vionConfig.allMenu
            ) {
                next();
            } else {
                next(from.meta.permissionPath ? '/404' : cacheHasPermissionMenu[0])
            }
        } else {
            router.push('/?path=' + from.path);
        }
    }
    // console.log('navigation-guards');
    // to: Route: 即将要进入的目标 路由对象
    // from: Route: 当前导航正要离开的路由
    // next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。
    // const nextRoute = ['home', 'account', 'analysis', 'behavior', 'manage'];
    // let isLogin = global.isLogin; // 是否登录
    // 未登录状态;当路由到nextRoute指定页时,跳转至login
    // console.log('beforeRoute:', to, to.path, nextRoute.indexOf(to.path), isLogin);
    // if (nextRoute.indexOf(to.path) >= 0) {
    //     if (!isLogin) {
    //         console.log('what fuck');
    //         router.push({ name: 'login' })
    //     }
    // }
    // 已登录状态;当路由到login时,跳转至home
    // if (to.name === 'login') {
    //     if (isLogin) {
    //         router.push({ name: 'home' });
    //     }
    // }
});

export default router;