import axios from 'axios';
let loadingInstance = null;
//请求拦截
axios.interceptors.request.use(
	config => {
		return config;
	},
	error => {
		return Promise.reject();
	}
);
//返回拦截
axios.interceptors.response.use(response => {
	return response
}, error => {
	return Promise.resolve(error.response)
})

//发送请求
async function service(method, url, params, type = 'application/x-www-form-urlencoded; charset=UTF-8;') {
	return axios({
		method: method,
		//拼接参数
		url: url,
		data: params,
		timeout: 10000,
		headers: {
			'X-Requested-With': 'XMLHttpRequest',
			"Access-Control-Allow-Origin":'*'
		},
		withCredentials: false
	}).then(
		(response) => {
			return checkStatus(response)
		}
	)
}
//发送请求
async function serviceGet(method, url, params, type = 'application/x-www-form-urlencoded; charset=UTF-8;') {
	return axios({
		method: method,
		//拼接参数
		url: url,
		data: params,
		timeout: 10000,
		headers: {
			'X-Requested-With': 'XMLHttpRequest',
			"Access-Control-Allow-Origin":'*'
		},
		withCredentials: false
	}).then(
		(response) => {
			return response
		}
	)
}
function checkStatus(response) {
	//状态码正常  去判断返回数据
	if (response.status === 200) {
		return checkCode(response.data)
	} else {
		return {
			suc: false,
			message: '系统请求错误'
		}
	}
}

function checkCode(res) {
	//成功的返回
	if (res.code == 200) {
		return {
			suc: true,
			data: res.data,
			msg: res.msg
		}
	} else {
		return res
	}

}

export const get = function (url, params) {
	return serviceGet('GET', url, params)
}
export const post = function (url, params) {
	return service('POST', url, params, 'application/json; charset=UTF-8;')
}

