// 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 App from './App'
import router from './router'
import "@/assets/fonts/iconfont.css"
Vue.config.productionTip = false

const axios = require('axios')
const Qs = require('qs')

Vue.prototype.$axios = axios //全局注册，使用方法为:this.$axios
import './util/http.js'
//格式化时间戳
import './util/formatTime'
// import qs from 'qs'
Vue.prototype.qs = Qs //全局注册，使用方法为:this.qs
Vue.prototype.$http = "http://106.119.196.18:10004"
/* eslint-disable no-new */
const ElementUI = require('element-ui')
Vue.use(ElementUI);

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,
})

import {
  VueJsonp
} from 'vue-jsonp' //解决百度地图跨域
Vue.use(VueJsonp)

import VueHighcharts from 'vue-highcharts';
Vue.use(VueHighcharts);
import Viewer from 'v-viewer'
Vue.use(Viewer, {
  defaultOptions: {
    "zIndex": 99999999,
  }
})
import 'viewerjs/dist/viewer.css'
// Vue.prototype.$imghost = 'http://8.136.98.49:8002' // 线上图片服务器路径常量

router.beforeEach(async (to, from, next) => {
  console.log(to)
  console.log(this)
  var tokens = localStorage.getItem("keys")
  if (to.path == "/login") {
    if (tokens) {
      next({
        path: '/index/home'
      })
    } else {
      next()
    }
  } else {
    if (to.query.token) {
      axios({
        method: "POST",
        url: "/api/api_gateway?method=user.login.zg_login_user",
        data: Qs.stringify({
          pp_token: to.query.token
        }),
      }).then((res) => {
        if (res.data.message == "") {
          console.log(res.data.data.data);
          var data = res.data.data.data.children;
          var menuList1 = [];
          for (let i = 0; i < data.length; i++) {
            menuList1.push(data[i]);
          }
          localStorage.setItem("zg_keys",to.query.token)
          localStorage.setItem("menu", JSON.stringify(menuList1));
          var key = res.data.data.session_key;
          localStorage.setItem("keys", key);
          localStorage.setItem("username", res.data.data.data.username);
          localStorage.setItem("myuid", res.data.data.data.myuid);
          next()
        } else {
          next({
            path: '/login'
          })
        }
      });
    } else {
      if (tokens) {
        next()
      } else {
        next({
          path: '/login'
        })
        // next()
      }
    }
  }


})

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()

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
