//var publish_version = false;
//var jqAjax = $.ajax;
//
//if(publish_version) {
//	$.ajax = function(url, opt) {
//		var userAgent = window.localStorage.getItem(agentName);
//		var ua = null;
//		var opts = opt || {}
//		if(userAgent) {
//			ua = JSON.parse(userAgent);
//		}
//		
//		var param ={};
//		if(ua) {
//			param = {
//					"userAgent_is_switch": "1",
//					"userAgent_id": ua.id,
//					"userAgent_uid": ua.uid,
//					"userAgent_openid": ua.openid,
//					"userAgent_token": ua.token,
//					"userAgent_lastUid": ua.lastUid,
//					"userAgent_platform": ua.platform,
//					"userAgent_represented": ua.represented
//				}
//		}
//		
//		if("object" == typeof url) {
//			opts = $.extend(true, opts , url);
//		}
//		
//		if(opts.type.toUpperCase() == "POST") {
//			opts.data = $.extend(true,{},opts.data, param)
//		} else if(opts.data){
//			if((typeof opts.data == 'string')&& opts.data.constructor == String) {
//				opts.data += "&"+$.param(param)
//			} else {
//				opts.data = $.extend(true,{},opts.data, param)
//			}
//		}
//		
//		opts.beforeSend = function(request,settings) {
//			
//		}
//		return jqAjax(url, opts);
//	}
//}

/**
 * 统一请求ajax发送方法
 * url 请求地址：例如：patient/health_index/add
 * params 请求参数
 * dataType 数据类型：json等
 * reqType 请求方式:get 或 post
 * error 请求失败处理方法
 * success 请求成功处理方法
 */
function sendPost(url, params, dataType, reqType, error, success,timeOut) {
	var userAgent = window.localStorage.getItem(agentName);
	var serverUrl = "";
	if(url.indexOf("http://")>-1 || url.indexOf("https://")>-1 ) {
		serverUrl = url;
	} else {
		serverUrl = server + url;
	}
	//发送ajax请求
	$.ajax(serverUrl, {
		data: params,
		dataType: dataType,
		beforeSend: function(request) {
			request.setRequestHeader("userAgent", userAgent);
		},
		timeout: timeOut || 60000, 
		type: reqType,
		error: function(res) {
			if(res.status == 999 || res.status == 998 || res.status == 997){
				loginUrl(res.status);
				return;
			}
			error(res);
		},
		success: function(res) {
			if(res.status == 999 || res.status == 998 || res.status == 997){
				loginUrl(res.status);
				return;
			}
			success(res);
		}
	});
}

function sendPostNoCache(url, params, dataType, reqType, error, success,timeOut) {
	var userAgent = window.localStorage.getItem(agentName);
	//发送ajax请求
	$.ajax(server + url, {
		data: params,
		dataType: dataType,
		cache: false,
		beforeSend: function(request) {
			request.setRequestHeader("userAgent", userAgent);
		},
		timeout: timeOut || 60000, 
		type: reqType,
		error: function(res) {
			if(res.status == 999 || res.status == 998 || res.status == 997){
				loginUrl(res.status);
				return;
			}
			error(res);
		},
		success: function(res) {
			if(res.status == 999 || res.status == 998 || res.status == 997){
				loginUrl(res.status);
				return;
			}
			success(res);
		}
	});
}

/**
 * 统一请求ajax发送方法
 * url 请求地址：例如：patient/health_index/add
 * params 请求参数
 * dataType 数据类型：json等
 * reqType 请求方式:get 或 post
 * error 请求失败处理方法
 * success 请求成功处理方法
 */
function sendPostAsync(url, params, dataType, reqType, error, success) {
	var userAgent = window.localStorage.getItem(agentName);
	//发送ajax请求
	$.ajax(server + url, {
		data: params,
		dataType: dataType,
		async:false,
		beforeSend: function(request) {
			request.setRequestHeader("userAgent", userAgent);
		},
		type: reqType,
		error: function(res) {
			if(res.status == 999 || res.status == 998 || res.status == 997){
				loginUrl(res.status);
				return;
			}
			error(res);
		},
		success: function(res) {
			if(res.status == 999 || res.status == 998 || res.status == 997){
				loginUrl(res.status);
				return;
			}
			success(res);
		}
	});
}


//重新登陆
var count = 0;
function loginUrl(status){
	var pageurl = "";
//	var hreftemp = server + "wx/html/home/html/zhmm-login.html?type=0&openid=";
	var hreftemp = "../../home/html/zhmm-login.html?type=0&openid=";
	var content = "";
	count ++;
	try{
		if(d){
		    d.close();
		}
	}catch(e){
		//TODO handle the exception
	}
	//如果有两个以上的请求会获得999等错误码的时候，只处理第一个
	if(count >= 2){
	    return false;
	}
	if(status == 999){
		content = "此账号已在别处登录，需要请重新登录";
	}else if(status == 998){
		content = "登录超时，需要请重新登录";
	}else if(status == 997){
		content = "此账号未登录，需要请重新登录";
//		hreftemp=server + "wx/html/home/html/zhmm-login.html?type=0&openid="
        hreftemp = "../../home/html/zhmm-login.html?type=0&openid=";
	}
	var openid = "";
	var userAgent = window.localStorage.getItem(agentName);
	if(userAgent){
		var jsonstr = $.parseJSON(userAgent);
		openid = jsonstr.openid;
	}
	if(openid==null||openid==""){
		Request = GetRequest();
		openid = Request["openid"];
	}
    clearAgent();
    //将保存访问路径的逻辑放到clearAgent后面
    pageurl = window.parent.location.href;
    saveAgentPage(pageurl);
	dialog({
	  	skin:"ui-dialog ax-popup pror", 
	  	content: content, 
	  	ok: function (){
	  	    count = 0;
	  		window.parent.location.href = hreftemp+openid;
	  	}
	}).showModal();
}

// 获取异步请求处理Promise对象
function getReqPromise(url, params, dataType, reqType) {
	return new Promise(function(resolve, reject) {
		sendPost(url, params,dataType, reqType,
		  	function queryFailed (xht, type, throwErr) {
				reject({
					xht:xht, type:type, throwErr:throwErr
				})
		  	}
	  		, function success(res) {
				resolve(res);
	  		});
	});
}

/*
 * reqs: 请求的参数数组，格式：[{url:'a/xxx', reqType: 'POST', data:{...}},{url:'b/xxx',data:{...}}]
 * */
function getReqPromises(reqs) {
	if(!reqs || !reqs.length) {
		return new Promise(function(resolve, reject) {
			resolve([]);
		});
	}
	return Promise.all(_.map(reqs,function(param){
		return getReqPromise(param.url,param.data,param.dataType, param.reqType);
	}));
};

/*
 * 获取图片路径方法修改
 */
function getImgUrl(str){
    if(typeof str != 'string'){
        return "";
    }
    if(str.length == 0){
        return "";
    }else{
        if(str.indexOf("../")>-1){
            //访问本地路径
            return str;
        }else if((str.indexOf("http://")>-1) || (str.indexOf("https://")>-1)){
            return str;
        }else{
            //服务器上的图片路径
            return imgUrlDomain + str;
        }
    }
}
