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 './icons' // icon
import axios from 'axios'
import service from './api/axios'
import echarts from 'echarts'
import NProgress from 'nprogress';
import 'nprogress/nprogress.css'
import {
	getEmployeeList,
	setEmployeeList,
	getDeptTree,
	setDeptTree,
	getToken,
	setToken,
	getUserData,
	setUserData,
	getIsCreator,
	supremeAuthority,
	isAuthoritys,
	authoritys,
	generateUUID,
	getCache,
	setCache,
	removeCache,
	getCycleType,
	getRole,
	returnCode
} from './api/auth';
Vue.prototype.$isAuthoritys = isAuthoritys
Vue.prototype.$action = 'https://integralsys.oss-cn-shenzhen.aliyuncs.com'
Vue.prototype.$getCycleType = getCycleType
Vue.prototype.$getRole = getRole
Vue.prototype.$getEmployeeList = getEmployeeList
Vue.prototype.$setEmployeeList = setEmployeeList
Vue.prototype.$getDeptTree = getDeptTree
Vue.prototype.$setDeptTree = setDeptTree
Vue.prototype.$getToken = getToken
Vue.prototype.$setToken = setToken
Vue.prototype.$getUserData = getUserData
Vue.prototype.$setUserData = setUserData
Vue.prototype.$getIsCreator = getIsCreator
Vue.prototype.$supremeAuthority = supremeAuthority
Vue.prototype.$authoritys = authoritys
Vue.prototype.$getCache = getCache
Vue.prototype.$setCache = setCache
Vue.prototype.$returnCode = returnCode //UID 唯一标识
Vue.prototype.$removeCache = removeCache
Vue.prototype.$generateUUID = generateUUID //UID 唯一标识

import * as socketApi from './api/websocket'
// 新绩效系统相关
import Pagination from '@/components/public/Pagination'
import PreBox from '@/components/public/PreBox'
Vue.component('Pagination', Pagination)
Vue.component('PreBox', PreBox)
// 头像
import userImage from '@/components/public/UserImage'
import NoData from '@/components/public/NoData'
import BrawerBox from '@/components/public/BrawerBox';
Vue.component('userImage', userImage)
Vue.component('NoData', NoData)
Vue.component('BrawerBox', BrawerBox)
Vue.use(ElementUI);

if (process.env.NODE_ENV === 'development') {
  new VConsole()
}

Vue.prototype.$moment = moment
Vue.prototype.$echarts = echarts
Vue.prototype.$dd = dd;
Vue.prototype.$socketApi = socketApi //长连接
Vue.prototype.$appId = process.env.VUE_APP_APPID  //小程序APPID
Vue.prototype.$serverdomain = process.env.VUE_APP_BASE_API //导出地址

Vue.prototype.$1 = 1
Vue.prototype.$2 = 2
Vue.prototype.$3 = 3
Vue.prototype.$4 = 4
Vue.prototype.$5 = 5
Vue.prototype.$6 = 6
Vue.prototype.$7 = 7
Vue.prototype.$8 = 8
Vue.prototype.$9 = 9
Vue.prototype.$10 = 10
Vue.prototype.$11 = 11
Vue.prototype.$12 = 12
Vue.prototype.$13 = 13
Vue.prototype.$14 = 14
Vue.prototype.$15 = 15
Vue.prototype.$16 = 16
Vue.prototype.$17 = 17
Vue.prototype.$18 = 18

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') {
		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'); //给个标志，中断请求
		})
	}
	// console.log(to.path)
	next();
});
router.afterEach(() => {
	NProgress.done();
});


window.__BIRD_CONFIG = window.__BIRD_CONFIG || {};
__BIRD_CONFIG.disableReport = true;

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