import VueRouter from "vue-router";
import Vue from 'vue'
import routers from "@/routers/routers"
Vue.use(VueRouter);
const origionalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location){
  return origionalPush.call(this,location).catch(err=>err)
}
const router = new VueRouter({
  mode:'hash',
  routes:routers
})
const allPaths = ['/origion/mattersGuidance/mainBody','/origion/handleSituation','/origion/handleAnalysis','/origion/evaluate','/origion/electricCard','/origion/controlCabin']

router.beforeResolve((to,form,next) => {
  let adminInfo = localStorage.getItem('ywtb_role_models')
  if(allPaths.indexOf(to.fullPath)>-1){
    if (adminInfo && Object.keys(JSON.parse(adminInfo)).length > 0){
      let arr = JSON.parse(adminInfo);
      let paths = []
      for(let i=0;i<arr.length;i++){
        paths.push(arr[i].O_PATH)
      }
      if(paths.indexOf(to.fullPath) > -1){
        next()
      }
    }else {
      if( to.name ==='login') { // 如果有此资源且是登录页
        // console.log('sas',to,next)
        next();
      } else { // 有此资源，没有登陆过，也不是登录页
        next({ path: '/login'})
      }
    }
  }else{
    next();
  }


})

export default router
