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: localStorage.getItem('token')
  }
  return config
})
// 返回拦截器(后)
axios.interceptors.response.use(data => {
  // console.log(localStorage.getItem('token'))
  if (localStorage.getItem('token') && data.data.code === 403) {
    alert('您没有查看此分类下数据的权限！')
  }
  // let daraRes = []
  // if(data.data.code===200){
  //   daraRes===
  // }
  return data
})
// IP端
axios({
  method: 'GET',
  url: 'site/check',
  params: {}
}).then(res => {
  // console.log(res, 'IP段')
  if (res.data.code === 200) {
    if (!localStorage.getItem('userLogin')) {
      history.go(0)
    }
    // 用户信息存本地
    localStorage.setItem('username', res.data.data.username)
    localStorage.setItem('pic', res.data.data.pic)
    localStorage.setItem('userPwd', '123456')
    localStorage.setItem('token', 'Bearer ' + res.data.data.token)
    localStorage.setItem('tokenB', res.data.data.token)
    // 登录状态
    localStorage.setItem('userLogin', true)
    Vue.prototype.$auth = true
  }
  // 请求接口完成   请求成功
}).catch((fail) => {
  console.log(fail)
})
// 将图形验证码写入
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)

Vue.config.productionTip = false

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