import Vue from 'vue'
import Router from 'vue-router'
import {routes} from './routes'
import store from '@/store'
import Cookie from '@/util/local-cookie'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.NODE_ENV === 'development' ? '/' : '/',
  routes,
})

router.beforeEach((to, from, next) => {
  // TODO
  store.commit('setWebviewSrc', to.meta && to.meta.src) // 判断有src的话为需要嵌入iframe的子项目

  const token = Cookie.get('token')
  // 当前无token且不在login页面则推到登录页面
  if (to.path != '/login' && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router
