//jquery.getJSON数据库查询类
//var  webserviceUrl="http://localhost:41564/Webservice/WebService.asmx";
var  webserviceUrl="http://120.55.160.15:8080/CNGreenWebService/Webservice/WebService.asmx";
//webserviceUrl = "http://180.166.90.44:8088/HealthInspectNew/Webservice/WebService.asmx";
webserviceUrl = "http://df.soyi.sh.cn:8081/DMWebService/DMWebservice";
// webserviceUrl = "http://jq.shanghai12301.com/sqlhelper_mid/Middle_LYJ/middle_lyj.asmx";
function WsCommonClass(method, variable, value, callbackFun,keyValue){
    if (variable.length != value.length) {
        return "参数不正确！";
    }
    var RequestVariable = {};
    for (var i = 0; i < variable.length; i++) {
        RequestVariable[variable[i]] = value[i];
    }
	$.base64.utf8encode = true;
	RequestVariable = $.base64.encode(RequestVariable); //base64 加密
    $.getJSON(webserviceUrl + "/"+method, RequestVariable,
        function (data) {
            console.info(data);
            callbackFun(data,keyValue);
        },function (data){
			console.info(data);
		});
}

function WsCommonClassGet(method, variable, value, callbackFun,keyValue){
	var RequestVariable = $.base64.encode(value[0]); 
	$.ajax({
	    url: webserviceUrl+"/"+method,
	    type: "get",
	    data: {
			username : 'admin',
			password : 'admin@citygis',
			sql : RequestVariable,
			pageSize : '0',
			pageIndex : '0'
		},
	    success: function (data) {
			data =  data.childNodes[0].textContent;
	        callbackFun(data,keyValue);
	    },
	    error:function(data) {
	        var ss =  "";
	    }
	});
}

function WsCommonClassSP(method, variable, value, callbackFun,keyValue){
	if (variable.length != value.length) {
	    return "参数不正确！";
	}
	// webserviceUrl = "http://180.166.90.44:8088/HealthInspectNew/Webservice/WebService.asmx";
	var RequestVariable = {};
	for (var i = 0; i < variable.length; i++) {
	    RequestVariable[variable[i]] = value[i];
	}
	var urls = webserviceUrl + "/"+method+"?sql="+value[0];
	// $.ajax({  
	//         type : "get",  
	//         async:false,  
	//         url : urls,  
	//         dataType : "jsonp",//数据类型为jsonp  
	//         jsonp: "jsonpCallback",//服务端用于接收callback调用的function名的参数  
	//         // jsonpCallback:"自定义回调函数名"
	//         success : function(data){  
	//            alert(data.info) 
	//         },  
	//         error:function(e){  
	//             alert('fail');  
	//         }  
	//     });
		
	// $.ajax({
	//     url: urls,
	//     type: 'post',
	//     dataType: 'json'
	// })
	// .done(function(data) {
	//     alert('请重试！');
	// })
	// .fail(function(e) {
	//     alert('服务器超时，请重试！');
	// });
	
	// get(urls,keyValue)
	// .then(function(response){
	//     console.log("success",response);
	//     document.getElementById('map').src = response.url;
	// })
	// .catch(function(err){
	//     console.log('error',err)
	// })
	
	try{
		var httpRequest = new XMLHttpRequest();//第一步：建立所需的对象
		        httpRequest.open('GET', urls, true);//第二步：打开连接  将请求参数写在url中  ps:"./Ptest.php?name=test&nameone=testone"
		        httpRequest.send();//第三步：发送请求  将请求参数写在URL中
		        /**
		         * 获取数据后的处理程序
		         */
		        httpRequest.onreadystatechange = function () {
		            if (httpRequest.readyState == 4 && httpRequest.status == 200) {
		                var json = httpRequest.responseText;//获取到json字符串，还需解析
		                console.log(json);
						callbackFun(json,keyValue);
		            }else{
						callbackFun({"ss":"失败"},keyValue);
					}
		        };
	}catch(e){
		console.log(e.message);
	}
}


var get = function(url,keyValue){
    return new Promise(function(resolve,reject){
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function(){
            if(xhr.readyState === 4 && xhr.status == 200){
                var result = xhr.responseText;
                // result = JSON.parse(result);
                callbackFun(result,keyValue);
            }else {
                reject(xhr);
            }
        }
        xhr.open("POST",url,true);
        xhr.send(null);
    })
}


//Ajax通用数据查询
function WsCommonClassAjax(method, variable, value, callbackFun,keyValue){
    if (variable.length != value.length) {
        return "参数不正确！";
    }
    var RequestVariable = "{";
    for (var i = 0; i < variable.length; i++) {
        RequestVariable += "'"+variable[i]+"':'"+value[i]+"',";
        //RequestVariable["'"+variable[i]+"'"] = 'aaa';
    }
    RequestVariable.substr()
    if(RequestVariable.length >1)
        RequestVariable = RequestVariable.substr(0,RequestVariable.length-1);
    RequestVariable += "}";
    var data = "{'name':'" + $("#name1").val() + "'}";
    $.ajax({
        url: webserviceUrl+"/"+method,
        type: "POST",
        dataType: "text",
        contentType: "text",
        data: RequestVariable,
        success: function (json) {
            callbackFun(data,keyValue);
            console.info(json.d);
        },
        error:function(data) {
            var ss =  "";
        }
    });
}

//HttpRequest通用数据查询
function WsCommonClassHTTP( method, variable, value, callbackFun,keyValue) {
    var _url ;    //请求地址
    var _method = method; //方法名
    var _variable = variable; //变量名数组http://139.196.105.31/LGJSWebservice/Webservice/WebService.asmx
    var _value = value;
	// webserviceUrl = global_config_json.WebServiceURL;
    _url = webserviceUrl;
	
    if (_variable.length != _value.length) {
        return "参数不正确！";
    }
    //设置访问的字符串
    var data = "";
    data += '<?xml version="1.0" encoding="utf-8"?>';
    data += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
    data += '<soap:Body>';
    data += '<' + _method + ' xmlns="http://tempuri.org/">';
    for (var i = 0; i < _variable.length; i++) {
        data += '<' + _variable[i] + '>' + _value[i] + '</' + _variable[i] + '>';
    }
    data = data + '</' + _method + '>';
    data = data + '</soap:Body>';
    data = data + '</soap:Envelope>';
    var xmlhttp;
    try {
        xmlhttp = new XMLHttpRequest();
    }
    catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
    }
    //设置通讯请求的方式
    if (xmlhttp != null && xmlhttp != "undefind") {
        try{
            xmlhttp.open("POST", _url,false);
        }
        catch (e){
            try{
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                xmlhttp.open("POST", _url,false);
            }
            catch (e) {
                xmlhttp = new XMLHttpRequest();
                xmlhttp.open("POST", _url,false);
            }
        }
    }
    else {
        return "发生错误";
    }

    xmlhttp.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
    xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/" + _method);
    try {
        //获取放回结果并传递给调用者
        xmlhttp.onreadystatechange = function () {
            var data;
            if(xmlhttp.responseXML)
            {
                var xmlDoc = xmlhttp.responseXML;
                data = xmlDoc.getElementsByTagName(_method + "Result");
            }
            else
            {
                var xmlDoc = eval(xmlhttp.responseText);
                data = xmlDoc;
            }

            var result = data[0].textContent;//[0].childNodes[0].textContent;
            if (callbackFun)
                callbackFun(result,keyValue);
        };

        xmlhttp.send(data);
    }
    catch (e) {
        alert("请求失败！");
    }


}