var HOST="http://127.0.0.1/project/xyx/";
var API_HOST=HOST+"/api/";
var CDN_HOST="https://media-mall-prod-1259811287.cos.ap-guangzhou.myqcloud.com/static/plant/";

var session_id;

function request_url(url,method,data,callbacks){
	if(!callbacks)callbacks={};
	if(!data)data={};
	uni.request({
		url:url,
		method:method,
		data:data,
		header: {
			'Cookie': session_id?'PHPSESSID=' + session_id:''
		},
		success:function(res){
			uni.hideLoading();
			if(res.data.err==0){
				if(res.data.result&&res.data.result.session_id)session_id=res.data.result.session_id;
				if(callbacks.success)callbacks.success(res.data);
			}else if(res.data.err==1001){
				uni.redirectTo({
					url: '/pages/login/login'
				});
			}else{
				if(callbacks.fail)callbacks.fail(res.data);
			}
		},
		fail:function(err){
			uni.hideLoading();
			if(callbacks.fail)callbacks.fail(err);
		},
		complete:function(){
			uni.hideLoading();
			if(callbacks.complete)callbacks.complete();
		}
	})
}
function request_api(api_name,method,data,callbacks){
	request_url(API_HOST+api_name+".php",method,data,callbacks);
}
function get_api(api_name,data,callbacks){
	request_api(api_name,"get",data,callbacks);
}
function post_api(api_name,data,callbacks){
	request_api(api_name,"post",data,callbacks);
}
function get_res(res_url)
{
	if(res_url.indexOf("http")>=0){
		return res_url;
	}else{
		return CDN_HOST+res_url;
	}
}
function cdn2host(cdnurl){
	var prefix_index=cdnurl.indexOf(CDN_HOST);
	if(prefix_index>=0){
		return HOST+cdnurl.substring(CDN_HOST.length,cdnurl.length);
	}
	return cdnurl;
}

module.exports = {
	HOST:HOST,
	API_HOST:API_HOST,
	CDN_HOST:CDN_HOST,
	request_url:request_url,
	get_api:get_api,
	post_api:post_api,
	get_res:get_res,
	cdn2host
}