import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import moment from 'moment' // 时间库
import 'element-ui/lib/theme-chalk/index.css';
import * as dd from 'dingtalk-jsapi';
import VConsole from 'vconsole'
import axios from 'axios'
import service from './api/axios'
import { getToken, setToken,getUserData,setUserData,getTyps,setTyps } from './api/auth';

// 头像
import userImage from '@/components/UserImage'
Vue.component('userImage', userImage)

Vue.use(ElementUI);
// if (process.env.NODE_ENV === 'development') {
//   new VConsole()
// }

Vue.prototype.$dd=dd;
Vue.prototype.$getToken = getToken
Vue.prototype.$setToken = setToken
Vue.prototype.$getUserData = getUserData
Vue.prototype.$setUserData = setUserData
Vue.prototype.$getTyps = getTyps
Vue.prototype.$setTyps = setTyps
Vue.prototype.$moment = moment

Vue.prototype.$http= service;
//Vue函数添加一个原型属性$axios 指向axios,这样vue实例或组件中不用再去重复引用Axios 直接用this.$axios就能执行axios 方法
var CancelToken = axios.CancelToken;
Vue.$httpRequestList=[];

Vue.prototype.$axios = (type, url, data,heaStr) => {
	if(url!='/api/ding/login'&& url!='/api/integral/types'){
	    var Accept='';
		switch(heaStr){
		   case 'v2':
		     Accept='application/vnd.test.v2+json'
		     break;
		   case 'v3':
		     Accept='application/vnd.test.v3+json'
		     break;
		   case 'v4':
		     Accept='application/vnd.test.v4+json'
		     break;
		}
		if(getToken()){
			return new Promise((resolve, reject) => {   //封装ajax
			    var aa = {
			        method: type,
			        url: url,
					headers:{
					  'Accept':Accept
					},
			        cancelToken: new CancelToken(c => {  //强行中断请求要用到的
			            Vue.$httpRequestList.push(c);
			        })
			    }
			    var json = (type == 'get') ? Object.assign(aa, { params: data }) : Object.assign(aa, { data: data });
			    var ajax = Vue.prototype.$http(json).then(res => {
			        resolve(res);
			    }).catch(error => {   //中断请求和请求出错的处理
			            if (error.message == "interrupt") {
			                console.log('已中断请求');
			                return;
			            } else {
			                reject(error);
			            }
			        })
			    return ajax;
			})
		}else{return Promise.reject('无token')}
	}else{
		return new Promise((resolve, reject) => {   //封装ajax
		    var aa = {
		        method: type,
		        url: url,
		        cancelToken: new CancelToken(c => {  //强行中断请求要用到的
		            Vue.$httpRequestList.push(c);
		        })
		    }
		    var json = (type == 'get') ? Object.assign(aa, { params: data }) : Object.assign(aa, { data: data });
		    var ajax = Vue.prototype.$http(json).then(res => {
		        resolve(res);
		    }).catch(error => {   //中断请求和请求出错的处理
		            if (error.message == "interrupt") {
		                console.log('已中断请求');
		                return;
		            } else {
		                reject(error);
		            }
		        })
		    return ajax;
		})
	}
};

router.beforeEach((to, from, next) => {   //路由切换检测是否强行中断，
    if(Vue.$httpRequestList.length>0){        //强行中断时才向下执行
        Vue.$httpRequestList.forEach(item=>{
            item('interrupt');//给个标志，中断请求
        })  
    }
    next();    
});

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