import axios from 'axios'

let url='';
 // uEnvDev
 if (process.env.NODE_ENV === 'development') {
     // TODO
	 url='api';
 }
 // uEnvProd
 if (process.env.NODE_ENV === 'production') {
     // TODO
	 url='api';
 }
// create an axios instance
const service = axios.create({
	baseURL: url, // url = base url + request url
	// withCredentials: true, // send cookies when cross-domain requests
	//crossDomain=true,
	headers: {'Content-Type': 'application/json;charset=utf-8'},
	timeout: 20000 // request timeout
})

// request interceptor
service.interceptors.request.use(
	config => {
		// do something before request is sent
		//console.info("axioshelper request");
		//console.info(mget.getters);
		//console.info(store);
	
		console.info('axioshelper request config',config);
		return config;
	},
	error => {
		// do something with request error
		console.log(error) // for debug
		return Promise.reject(error)
	}
)

// response interceptor
service.interceptors.response.use(
	/**
	 * If you want to get http information such as headers or status
	 * Please return  response => response
	 */

	/**
	 * Determine the request status by custom code
	 * Here is just an example
	 * You can also judge the status by HTTP Status Code
	 */
	response => {
		 
		console.info('axioshelper应答原始数据', response);

		var res = response.data;
		//去除返回的json数据中有{"d":null}的情况
		if(res.indexOf('{"d":null}')>=0){
            res=res.replace('{"d":null}','');
        }
        var obj=JSON.parse(res);
		return obj;
		
		 
	},
	error => {
		console.log('axioshelper 应答error' + error) // for debug
		

		return Promise.reject(error)
	}
)

export default service
