import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import { VueJsonp } from 'vue-jsonp'  //解决百度地图跨域
Vue.use(VueJsonp)
import './util/http.js'
Vue.prototype.$axios = axios //全局注册，使用方法为:this.$axios
import qs from 'qs'
Vue.prototype.qs = qs //全局注册，使用方法为:this.qs
Vue.prototype.$EventBus = new Vue() //中央时间总线

Vue.config.productionTip = false //是阻止显示生产模式的消息

Vue.prototype.jsonUrl = 'http://182.92.193.64'; //地图三级下钻页面使用-韩有波
// Vue.prototype.$host = 'http://182.92.193.64:8002/api' // 线上图片服务器路径常量
Vue.prototype.$host = 'http://192.168.1.120:8001/api' // 线下图片服务器路径常量


import 'babel-polyfill' //兼容IE11
require("babel-polyfill")

// 引入百度地图
import BaiduMap from 'vue-baidu-map'
import {
  BmPolygon,
  BmControl,
  BmMarker
} from 'vue-baidu-map'
Vue.use(BaiduMap, BmPolygon, BmControl, BmMarker, {
  ak: 'nroAiX0Lf6ppNEGt2dBLtDkOldGCPFwF'
})

import store from './vuex/store' //引入store.js

import promise from 'es6-promise'
promise.polyfill();


import VueHighcharts from 'vue-highcharts';
Vue.use(VueHighcharts);
import Viewer from 'v-viewer'
Vue.use(Viewer, {
  defaultOptions: {
    "zIndex": 9999,
  }
})
import 'viewerjs/dist/viewer.css'

// 样式
import './assets/icon/iconfont.css'
import './plugin/elementUi';
// import './assets/css/theme/ffffff/index.css' //皮肤一
import '../theme/index.css'  //皮肤一

//导入全局样式
import './assets/css/global.css'

// 图片放大
import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
Vue.use(preview)
// 图片裁剪
import VueCropper from 'vue-cropper'
Vue.use(VueCropper)



//格式化时间戳
import './util/formatTime'

//设备类型
Vue.filter('equipType', function (i) {
  switch (i) {
    case '2':
      return '杀虫灯';
      break;
    case '3':
      return '虫情测报灯';
      break;
    case '4':
      return '性诱测报';
      break;
    case '5':
      return '环境监测';
      break;
    case '6':
      return '监控';
      break;
    case '7':
      return '孢子仪';
      break;
    case '9':
      return '糖醋测报';
      break;
  }
})

function checkLoginRequire(type, isLogin, next) {
  if (type == false) {
    next();
  } else {
    if (isLogin) {
      next();
    } else {
      next('/login')
    }
  }
}
router.beforeEach((to, from, next) => {
  //针对菜单进入列表不缓存
  if (to.name == 'CbdDataDetails' || to.name == 'cbdDataPhotos' || to.name == 'MessageWarn' || to.name == 'PestsStats' || to.name == "BzyPhotos" || to.name == 'DataDetail' || to.name == 'xycbDataPhotos' || to.name == 'xycbDataDetail' || to.name == 'ScdDetail') {
    from.meta.keepAlive = true
  } else {
    from.meta.keepAlive = false
  }

  let isLogin = JSON.parse(localStorage.getItem('isLogin'));
  if (to.path === '/login') {
    if (isLogin) {
      next('/index/home') //登录后默认跳转到首页
    } else {
      next()
    }
  } else {
    let type = to.meta.login_require; //是否需要登录,为了解决扫码在手机上看
    axios({
      method: 'POST',
      url: '/api/api_gateway?method=user.login.users_check'
    }).then(res => {
      if (res.data.message=='') { //登录状态
        // console.log('check_success');
        localStorage.setItem("isLogin", true)
        checkLoginRequire(type, isLogin, next)
      } else { //session过期 
        // console.log('check_false');
        localStorage.setItem("isLogin", false)
        next('/login');
      }
    }, err => {
      // console.log('check_error');
      localStorage.setItem("isLogin", false);
      next('/login');
    })
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: {
    App
  },
  template: '<App/>'
})