import store from "@/store";
import axios from 'axios';
import router from "@/router";
import {getStorage, removeItem} from '@/utils/localStorage'
var requestCount = 0; //记录下同时的请求数
// import ElementUI from 'element-ui';

axios.defaults.headers.post['elabEnvironment'] = '4'; //axios默认的请求方式,可以自己设置
axios.defaults.headers.post['userType'] = '1000'; //axios默认的请求方式,可以自己设置
axios.defaults.timeout = 10*60*1000; //axios默认的请求方式,可以自己设置
// axios.defaults.headers.post['Authorization'] = 'Token 9837b7480a99ab4df7e3a06ad0d990fbc7b941e4'; //axios默认的请求方式,可以自己设置
axios.interceptors.request.use(function(config) {
	if(config.noLoading){
		store.dispatch('setLoading',false);//不显示loading态
	}
	if(!config.url.includes('aiGenerateImg/getResult') &&
	!config.url.includes('aiGenerateImg/uploadImgControlNet') &&
	!config.url.includes('aiGenerateImg/getPredictions') &&
	!config.url.includes('aiGenerateImg/img2img_local') &&
	!config.url.includes('aiGenerateImg/img2Img')){
		requestCount++;//请求数+1
	}
	if (config.url.endsWith('.jpg') || config.url.endsWith('.png') || config.url.endsWith('.gif')) {
	    // 给图片get请求附加header
		console.warn("***图片get请求***",config.url)
		return config;
	} else {
		if(config.url.startsWith('http')){
			config.url = config.url;
		}else{
			config.url = window.$config.api_url + config.url;
		}
		if (config.url.includes('elab-marketing-operate/mofangTest/user/login')
		||config.url.includes('elab-marketing-user/vcode/send/verifyCode')) {
			// config.headers['userId'] = "";
			// config.headers['ip'] = "";
		} else {//uploadOss 上传format文件过大时提示不成功，接口500，通过测试与header有关，注释掉上传的header就好了
			if (!config.url.includes('elab-marketing-sms/aliyun/openapi/uploadOss')
			&& !config.url.includes('/robust_video')){
				// config.headers['userId'] = mapVue.$route.query.leavePhoneCustomerId || '';
				// config.headers['openId'] = mapVue.$route.query.openid || '';
				// config.headers['ip'] = getStorage('ip');
				// config.headers['maiya_token'] =  getStorage('userInfoThing') ? JSON.parse(getStorage('userInfoThing')).maiya_token || "" : ""; //token
			}
		}
	}
	return config;
}, function(error) {
	console.log(error)
});

axios.interceptors.response.use(function(response) {
	if(!response.config.url.includes('aiGenerateImg/getResult') &&
	!response.config.url.includes('aiGenerateImg/uploadImgControlNet') &&
	!response.config.url.includes('aiGenerateImg/img2img_local') &&
	!response.config.url.includes('aiGenerateImg/getPredictions') &&
	!response.config.url.includes('aiGenerateImg/img2Img')){
		requestCount = requestCount - 1;
	}
	if (requestCount < 1) {
		store.state.loading = false;//不显示loading状态
	}
	let result = response.data;
	return result;
}, function(error) {
	console.log("***error***", error, requestCount)
	// alert("***error***"+JSON.stringify(error))
	if (axios.isCancel(error)) {
		console.log('上传已取消');
		requestCount = requestCount - 1;
		if (requestCount < 1) {
			store.state.loading = false;//不显示loading状态
		}
		return error;
	}else{
		if(	!error.config.url.includes('aiGenerateImg/getResult') &&
			!error.config.url.includes('aiGenerateImg/uploadImgControlNet') &&
			!error.config.url.includes('aiGenerateImg/img2img_local') &&
			!error.config.url.includes('aiGenerateImg/getPredictions') &&
			!error.config.url.includes('aiGenerateImg/img2Img')){
			requestCount = requestCount - 1;
		}
		if (requestCount < 1) {
			store.state.loading = false;//不显示loading状态
		}
		let result = error.response;
		return result;
	}
});
export default axios;
