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

axios.defaults.headers.post['elabEnvironment'] = '4'; //axios默认的请求方式,可以自己设置
axios.defaults.headers.post['userType'] = '1000'; //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/img2Img')){
		requestCount++;//请求数+1
	}
	if (config.url.includes('http')) {
		return config;
	} 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'] = "";
			showToast = true;
		} else {//uploadOss 上传format文件过大时提示不成功，接口500，通过测试与header有关，注释掉上传的header就好了
			if (!config.url.includes('elab-marketing-sms/aliyun/openapi/uploadOss')){
				let userinfo = getStorage('userInfoThing');
				config.headers['userId'] = userinfo ? JSON.parse(userinfo).dataUserId : '';
				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')){
		requestCount = requestCount - 1;
	}
	if (requestCount < 1) {
		store.state.loading = false;//不显示loading状态
	}
	let result = response.data;
	return result;
}, function(error) {
	console.log("***error***", error, requestCount)
	if(	!error.config.url.includes('aiGenerateImg/getResult') &&
		!error.config.url.includes('aiGenerateImg/uploadImgControlNet') &&
		!error.config.url.includes('aiGenerateImg/img2Img')){
		requestCount = requestCount - 1;
	}
	if (requestCount < 1) {
		store.state.loading = false;//不显示loading状态
	}
	//如果返回的状态码是401则需要返回到登录
	if(error.response && error.response.status && error.response.status==401){
		window.$bus.$off();//取消所有的订阅事件
		removeItem('userInfoThing');
		removeItem('currentConponent');
		removeItem('setProgramComponetData');
		for (let i = 0; i < window.localStorage.length; i++) {
			let key = window.localStorage.key(i);
			if (key.includes("currentProject")) {
				removeItem(key);
			}
			if (key.includes("planItem")) {
				removeItem(key);
			}
		}
		window.$clearPermissionRole();//登出时重置权限里面记录的权限单元
		store.dispatch('setCurrentProject',null);
		router.push({
			name: "Login"
		});
		if (showToast){
			showToast = false;
			// ElementUI.Message({
			// 	message:"账号已失效",
			// 	type:"error"
			// });
		}
		error.response.message = "账号已失效";
		error.response.data.message = "账号已失效";
		// MessageBox.alert('账号已失效,无法登录', '提示', {
		//           confirmButtonText: '确定',
		//           callback: action => {
		//               router.replace({
		//                   name: 'Login',
		//                   query: {redirect: router.currentRoute.fullPath} //登录后再跳回此页面时要做的配置
		//               })
		//           }
		//       });
	}
	let result = error.response;
	return result;
});
export default axios;
