import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import moment from 'moment' // 时间库
import * as dd from 'dingtalk-jsapi';
import VConsole from 'vconsole'
import './icons'
import axios from 'axios'
import service from './api/axios'
import echarts from 'echarts'
import NProgress from 'nprogress';
import 'nprogress/nprogress.css'
import {returnDeptName, getToken, setToken,getUserData,setUserData,getTyps,setTyps,getIsCreator,supremeAuthority,authoritys,getTypsName,generateUUID,getCache,setCache,removeCache,getEmployeeMapItem} from './api/auth';
import * as socketApi from './api/websocket'
import * as socketApiTow from './api/websocketTow'
import print from "./api/print.js";
Vue.use(print)

// 头像
import userImage from '@/components/UserImage'
import NoData from '@/components/noData'
import BrawerBox from '@/components/BrawerBox';
import FormBox from '@/components/FormBox';
Vue.component('BrawerBox', BrawerBox)
Vue.component('userImage', userImage)
Vue.component('noData', NoData)
Vue.component('FormBox', FormBox)
Vue.use(ElementUI);
if (process.env.NODE_ENV === 'development') {
  new VConsole()
}
Vue.prototype.$echarts = echarts
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.$getTypsName = getTypsName
Vue.prototype.$getIsCreator = getIsCreator
Vue.prototype.$supremeAuthority = supremeAuthority
Vue.prototype.$authoritys = authoritys
Vue.prototype.$moment = moment
Vue.prototype.$appId = process.env.VUE_APP_APPID
Vue.prototype.$socketApi = socketApi   //长连接
Vue.prototype.$socketApiTow = socketApiTow   //长连接
Vue.prototype.$generateUUID = generateUUID   //UID 唯一标识
Vue.prototype.$getCache = getCache
Vue.prototype.$setCache = setCache
Vue.prototype.$removeCache = removeCache
Vue.prototype.$returnDeptName = returnDeptName
Vue.prototype.$getEmployeeMapItem = getEmployeeMapItem

// 上传相关
Vue.prototype.$action = 'https://integralsys.oss-cn-shenzhen.aliyuncs.com'
Vue.prototype.$acceptImg = 'image/jpeg,image/png'
Vue.prototype.$acceptFile = 'application/pdf,application/vnd.ms-excel,application/msword,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
Vue.prototype.$acceptImgFile='image/jpeg,image/png,application/pdf,application/vnd.ms-excel,application/msword,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
Vue.prototype.$xtoken={ 'X-Token': getToken() };


Vue.prototype.$http = service;
var CancelToken = axios.CancelToken;
Vue.$httpRequestList = [];
Vue.prototype.$axios = (type, url, data,heaStr) => {
	if(url!='/api/ding/login'&&url!='/api/integral/types'&&url!='/api/screen/ranking'&&url!='/api/auth/qr'){
	    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 == "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) => {   //路由切换检测是否强行中断，
    NProgress.start();
	if(Vue.$httpRequestList.length>0){        //强行中断时才向下执行
        Vue.$httpRequestList.forEach(item=>{
            item('interrupt');//给个标志，中断请求
        })  
    }
    next();    
});
router.afterEach(() => {
  NProgress.done();
});

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

