import Vue from 'vue'
import VueRouter from 'vue-router'
import routerMap from '../routerMap/index'

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {//防止重新加载相同的路由时控制台报错
  return routerPush.call(this, location).catch(error => error)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    meta: { index: 1, title: '九方软件' },

    component: resolve => require(['@/views/Login'], resolve)
  },
  {
    path: "/cms/print",
    name: "print", // 打印芯片
    component: (resolve) => require(["@/views/page/print"], resolve)
  },
  {
    path: '/cms',
    name: 'Cms',
    meta: { index: 2 },
    component: resolve => require(['@/views/cms'], resolve),
    children: routerMap,
  }
]

const router = new VueRouter({
  routes,
  mode: 'hash',
  linkActiveClass: 'active'
})
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next()
  } else {
    const token = localStorage.getItem('token')
    if (!token&&to.path!=='/cms/print') {
      next('/')
    } else {
      next()
    }
  }
  next()
})
export default router
