import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import filters from './filters'
import directive from './directive'
import {http} from './ajax/index.js';
import axios from 'axios'
import {Message} from 'element-ui';
import Util from "./tool";

//本地环境开启提示信息
Vue.config.productionTip = false;

//加载全局样式
import '@/assets/css/public/common.min.css'
import '@/assets/css/font/iconfont.css'


//加载过滤器
Object.keys(filters).forEach(key => Vue.filter(key, filters[key]));

//加载自定义指令
Vue.use(directive);

//处理顶部导航TAB
router.afterEach((to, from, next) => {
  if (to.path == "/"||to.path == "/login") {
    return
  }
  let tabOldArr = sessionStorage.getItem('tabs');
  let tabOldJson;
  let tabNewJson = [];
  if (tabOldArr) {
    tabOldJson = JSON.parse(tabOldArr);
    for (var i = 0; i < tabOldJson.length; i++) {
      tabNewJson.push(JSON.stringify(tabOldJson[i]))
    }
    if (tabNewJson.indexOf(JSON.stringify({
      path: to.path,
      title: to.meta.title,
      query:to.query
    })) == -1) {
      tabOldJson.push({
        path: to.path,
        title: to.meta.title,
        query:to.query
      })
    }
  } else {
    tabOldJson = [
      {
        path: to.path,
        title: to.meta.title
      }
    ]
    
    
  }
  if (tabOldJson) {
    sessionStorage.setItem('tabs', JSON.stringify(tabOldJson));
  }
});

axios.interceptors.response.use(res => {
  return res.data;
}, error => {
  // Message('网络出现问题,请检查网络');
  return Promise.reject(new Error(error))
})

//挂载到VUE原型上封装后的http请求
Vue.prototype.$http = http;

//路由页面回跳处理  mate里面参数 back:true 开启登录态回跳
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    next()
  } else {
    let token = Util.getCookie('token') || '';
    if (!token) {
      window.location.href = '/#/login?referer=' + encodeURI(to.path);
      history.go(0);
    } else {
      next();
    }
  }
});


new Vue({
  router,
  render: (h) => h(App),
}).$mount("#app");