index.js 6.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'

const router = new Router({
  // mode: 'history',
  routes: [{
    path: '/home',
    name: 'home',
    meta: {
      permissionPath: 'home'
    },
    // component: home,
    component: () => import('@/views/home'),
    // redirect: '/account',
    children: [
      ...reportRouter,
      analysisRouter,
      customerRouter,
      faceAnalysisRouter,
      {
        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'),
          }]
        },]
      },
      managementRouter
    ]
  },
  {
    path: '/',
    name: 'login',
    component: () => import('@/views/login/login'),
    // component: Login,
    children: []
  },
  {
    path: '/asus',
    name: 'asuslogin',
    component: () => import('@/views/loginAsus/loginAsus'),
    // component: Login,
    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'),
    // component: Login,
    children: []
  },
  {
    path: '/free',
    name: 'freelogin',
    component: () => import('@/views/Freelogin/freelogin'),
    // component: Login,
    children: []
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error-page/404')
  },
  {
    path: '*',
    redirect: '/404'
  },
  // {
  //   path: '/layout',
  //   name: 'Layout',
  //   //
  // }
  ]
  // , {
  //     path: '/',
  //     name: 'login',
  //     component: Login,
  //     children: []
  // }

})

// 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.allMenu) {
        router.push('/account');
      } else {
        const cacheMenuList = window.localStorage.getItem('menu')
        if (cacheMenuList) {
          router.push('/' + (firstPath || 'home'));
        } 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.allMenu
      ) {
        next();
      } else {
        next('/404')
      }
    } else {
      router.push('/');
    }
  }
  // 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;