import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from '../store/index'
Vue.use(VueRouter)

// 路由懒加载
const Login = () => import('../views/login/login.vue')
const PoiceMessage = () => import('../views/Poice/PoiceMessage.vue')
const articles = () => import('../views/article/articles.vue')
const Search =() => import('../views/searchview/search.vue')
const Searchriskarea =() => import('../views/searchview/searchriskarea.vue')

const routes = [{
    path: '/articles/:fileID',
    name: 'articles',
    component: articles,
    meta: {
      title: '政策文件详情页面',
      keepAlive: false,
      // 需要验证登录才能访问
      auth: true,
    }
  },

  {  // 搜索
    path: '/Search/:kw',
    name: 'Search',
    component: Search,
    meta: {
      title: '搜索页面',
      keepAlive: false,
      auth: true,
    }
  },
  {  // 搜索疫情地区
    path: '/Searchriskarea/:area_name',
    name: 'Searchriskarea',
    component: Searchriskarea,
    meta: {
      title: '搜索疫情页面',
      keepAlive: false,
      auth: true,
    }
  },
  {
    path: '/PoiceMessage',
    name: 'PoiceMessage',
    component: PoiceMessage,
    meta: {
      title: '政策文件页面',
      keepAlive: true,
      // 需要验证登录才能访问
      auth: true,
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    name: 'index',
    component: HomeView,
    meta: {
      title: '首页',
      keepAlive: true,
      // 需要验证登录才能访问
      auth: true,
    }
  },
  // {
  //   path: '/about',
  //   name: 'about',
  // route level code-splitting
  // this generates a separate chunk (about.[hash].js) for this route
  // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  // mode: 'history',
  
  base: process.env.BASE_URL,
  routes,
  
})


// 登录验证
router.beforeEach((to, from, next) => {
  if (to.matched.some(m => m.meta.auth)) {
    // 对路由进行验证
    if (store.state.islogin == '100') { // 已经登陆
      next() // 正常跳转到页面
    } else {
      // 未登录则跳转到登陆界面，query:{ Rurl: to.fullPath}表示把当前路由信息传递过去方便登录后跳转回来；
      next({
        path: '/login',
        query: {
          Rurl: to.fullPath
        }
      })
    }
  } else {
    next()
  }
})

export default router


