import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

import App from '../App.vue'
import Login from '../views/Login/Login.vue'
import LoginMiddle from '../views/LoginMiddle/LoginMiddle.vue'
import Home from '../views/Home/Home.vue'


Vue.use(VueRouter)

const routes = [
	{
		path: '/',
		component: App,
		children: [
			{
				path: '',
				redirect: '/login'
			},
		]
	},
	{
		path: '/login',
		name: 'login',
		component: Login
	},
	{
		path: '/loginMiddle',
		name: 'loginMiddle',
		component: LoginMiddle
	},
	{
		path: '/home',
		component: Home
	},
	{
		path: '*',
		name: '*',
		component: Login
	},
]

const router = new VueRouter({
	routes
})
// 判断下是否需要登录
router.beforeEach((to,from,next)=>{
	// console.log(store.getters.token,to)
    if(store.getters.token == null && !localStorage.getItem("token") && to.path !== '/login'){
        next({
            path:'/login',
            query: {redirect:to.fullPath}
        })
    }else next()
})
export default router
