// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui';
// import 'element-ui/lib/theme-chalk/index.css';
import App from './App'
import router from './router'
import axios from 'axios'
Vue.prototype.$axios = axios //全局注册，使用方法为:this.$axios
Vue.prototype.$EventBus = new Vue()
import './util/http.js'
import {
  VueJsonp
} from 'vue-jsonp' //解决百度地图跨域
Vue.use(VueJsonp)


import qs from 'qs'
Vue.prototype.qs = qs //全局注册，使用方法为:this.qs
Vue.prototype.$EventBus = new Vue() //中央时间总线
Vue.use(ElementUI);


import { Message } from 'element-ui';
//定义一个新的Message方法，多传入一个offset参数
const $message = options => {
  return Message({
    ...options,
    offset: 160,
    type: options.type
  });
};
//重写一遍success的方法,将offset写入options
['success', 'warning', 'info', 'error'].forEach(type => {
  $message[type] = options => {
    if (typeof options === 'string') {
      options = {
        message: options,
        offset: 160
      };
    }
    options.type = type;
    return Message(options);
  };
});

//将$message挂载到this上
Vue.prototype.$message = $message;


//节流
function throttle(){
  let prev=0
  Vue.prototype.$throttle = function(func,wait){
    let now=Date.now()
    if(now-prev>wait){
      func.apply(this)
      prev=now
    }
  }
}
throttle()


import VueLazyLoad from 'vue-lazyload'
Vue.use(VueLazyLoad, {
  preLoad: 1,
  // error: require('./assets/images/noimage.png'),
  error: require('./assets/images/newImg/noimage.png'),
  // loading: require('./assets/images/shot-1.gif'),
  loading: require('./assets/images/newImg/shot-1.gif'),
  attempt: 2,
})

Vue.prototype.jsonUrl = 'http://182.92.193.64'; //地图三级下钻页面使用-韩有波
Vue.prototype.$imghost = 'http://static.yfpyx.com/projectimg' // 线上图片服务器路径常量
Vue.prototype.$insectHost = 'https://images.weserv.nl/?url=' // 线上图片服务器路径常量
Vue.prototype.$deriveData = 'http://182.92.193.64:8002' // 导出
Vue.prototype.$faultvideo = 'http://192.168.1.3:8000' //售后视频地址前缀

Vue.config.productionTip = false

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

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

//导入全局样式
import './assets/css/global.css'
// 样式
import './assets/icon/iconfont.css'
// import './assets/css/theme/ffffff/index.css' //皮肤一
import '../theme/index.css'  //皮肤一

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 VueAMap from 'vue-amap';
// Vue.use(VueAMap);
// VueAMap.initAMapApiLoader({
//   key: '3939967b919a8003377dd113fc445a3d',
//   plugin: ['AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType','AMap.Geocoder','AMap.Geolocation','AMap.MouseTool'],
//   v: '1.4.4'
// });
// // 引入百度地图
// import BaiduMap from 'vue-baidu-map'
// import {
//   BmPolygon,
//   BmControl,
//   BmMarker
// } from 'vue-baidu-map'
// Vue.use(BaiduMap, BmPolygon, BmControl, BmMarker, {
//   ak: 'nroAiX0Lf6ppNEGt2dBLtDkOldGCPFwF'
// })

//导入全局样式
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 animated from 'animate.css' // npm install animate.css --save-dev安装，再引入
Vue.use(animated)


//格式化时间戳
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;
    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;
  }
})

// 弹框拖拽
import './util/directives.js'
// 弹框样式
import '@/assets/css/form.css'

router.beforeEach((to, from, next) => {
  let isLogin = JSON.parse(localStorage.getItem('isLogin'));
  if (to.path === '/login') {
    if (isLogin) {
      next('/plantGuard') //登录后默认跳转到首页
    } else {
      next()
    }
  } else if (to.name == 'PlantGuard') {
    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.data == 1) { //登录状态
        // console.log('check_success');
        localStorage.setItem("isLogin", true)
        next()
        // 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');
    })
  }
})

// 引入vuex-store
import store from './store/store';

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