import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
// 导入elementUI
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 导入图形验证码
import identify from '../src/assets/js/canvas_identifying.js'
import '../src/assets/js/flexible.js'
// 引入swiper
import VueAwesomeSwiper from 'vue-awesome-swiper'

// require styles
import 'swiper/dist/css/swiper.css'

// 全局指定配置
// 自定义指令实现back-top;
Vue.directive('scroll-show', {
  inserted (el, binding) {
    let scope = binding.arg || '200'
    window.addEventListener('scroll', function (e) {
      if (this.scrollY > Number(scope)) {
        binding.value.value = true
      } else {
        binding.value.value = false
      }
    })
  }
})
Vue.directive('back-top', {
  inserted (el, binding) {
    let e = binding.arg || 'click'
    el.addEventListener(e, function () {
      let scrollToptimer = setInterval(function () {
        let top = document.documentElement.scrollTop + document.body.scrollTop
        let speed = top / 4
        top -= speed
        document.documentElement.scrollTop = document.body.scrollTop = top
        if (top === 0) {
          clearInterval(scrollToptimer)
        }
      }, 30)
    })
  }
})
// axios请求配置
var root = process.env.VUE_APP_BASE_API
// console.log(root)

// 请求拦截器(前)
axios.interceptors.request.use(config => {
  // console.log(config, 'config')
  // --请求之前重新拼装url--
  config.url = root + config.url
  // --默认请求参数--
  // config.data = {
  //   ...config.data,
  //   openid
  // };
  // config.headers = {
  //   'Authorization': 'hjiujkolololjhgg12569jhjhF'
  // }
  config.headers = {
    Authorization: sessionStorage.getItem('token')
  }
  return config
})
// 返回拦截器(后)
axios.interceptors.response.use(data => {
  switch (data.data.code) {
    case 401:
      if (sessionStorage.getItem('token')) {
        Vue.prototype.$alert('登陆已过期，请重新登陆！', '温馨提示', {
          confirmButtonText: '确定'
        })
        sessionStorage.clear()
        window.location.reload()
      }
      break
    case 403:
      Vue.prototype.$alert('您没有查看此分类下数据的权限！', '温馨提示', {
        confirmButtonText: '确定'
      })
      break
    case 500:
      Vue.prototype.$alert('内部服务器错误！', '温馨提示', {
        confirmButtonText: '确定'
      })
      break
  }
  return data
})
// 将图形验证码写入
Vue.prototype.$identify = identify.default
Vue.prototype.$axios = axios
// 引入设置dom元素高度的插件
Vue.prototype.$detector = require('element-resize-detector')
// vue人员权限
Vue.prototype.$auth = {
  auth: false
}

Vue.use(VueAwesomeSwiper)
Vue.use(ElementUI)

// 关闭浏览器
// window.addEventListener('beforeunload', e => {
//   window.sessionStorage.clear()
// })
Vue.config.productionTip = false

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