(function(exports) {
    var publish_version = false,
	server,
	imgUrlDomain,
//	agentName = "wlyyAgentForDoc",
    agentName = "useragent", //先设置与医生端一致，暂借用app端的userAgent值
	userInfo = "userInfoForPay",
	userAgent = '';
    if(publish_version) { // 生产环境配置
        server = ""
        
    } else { // 测试环境配置
        server = "http://172.19.103.88:9092/wlyy/";
        imserver = "http://172.19.103.88:3000/api/v2";
        imgUrlDomain = "http://172.19.103.54/";
    }
    
    //测试使用，写死userAgent。如果要切换账号，自己再其他端登录后将userAgent的值拷贝过来即可
    var userAgent = {"id":4835,"uid":"rll201703150222","imei":"860573010176837","token":"c0cdef2e1238d89eb4397e1349b56416","platform":2,"hospital":"3502050100","appVersion":"1.4.3"};
    window.localStorage.setItem(agentName, JSON.stringify(userAgent));

    function httpGet(url, options) {
        //发送ajax请求
        return new Promise(function(resolve, reject) {
            $.ajax(server + url,
                $.extend({}, {
                    type: 'GET',
                    dataType: 'JSON',
                    beforeSend: function(request) {
                    	var userAgent = window.localStorage.getItem(agentName);
                    	request.setRequestHeader(agentName, userAgent);
                    },
                    error: function(res) {
                        reject(res)
                    },
                    success: function(res) {
                        resolve(res)
                    }
                }, options));
        })
    }

    function httpPost(url, options) {
        //发送ajax请求
        return new Promise(function(resolve, reject) {
            $.ajax(server + url,
                $.extend({}, {
                    type: 'POST',
                    dataType: 'JSON',
                    beforeSend: function(request) {
                    	var userAgent = window.localStorage.getItem(agentName);
                    	request.setRequestHeader(agentName, userAgent);
                    },
                    error: function(res) {
                        reject(res)
                    },
                    success: function(res) {
                        resolve(res)
                    }
                }, options));
        })
    }
    
    function imHttpGet(url, options) {
        //发送ajax请求
        return new Promise(function(resolve, reject) {
            $.ajax(imserver + url,
                $.extend({}, {
                    type: 'GET',
                    dataType: 'JSON',
                    beforeSend: function(request) {
                        request.setRequestHeader("userAgent", JSON.stringify(sendHead));
                    },
                    error: function(res) {
                        reject(res)
                    },
                    success: function(res) {
                        resolve(res)
                    }
                }, options));
        })
    }
    
    /*
	 * 获取图片路径
	 */
	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;
	        }
	    }
	}

    httpRequest = {
    	server: server,
    	userInfo: userInfo,
    	agentName: agentName,
    	userAgent:userAgent,
        get: function(url, options) {
        	return httpGet(url, options)
        },
        post: function(url, options) {
        	return httpPost(url, options)
        },
        getImgUrl:function(url){
        	return getImgUrl(url)
        },
        imGet:function(url, options) {
        	return imHttpGet(url, options)
        }
    }

    exports.httpRequest = httpRequest;
})(window)