(function(exports) {
	var publish_version = true;
	var server;
	var agentName = "useragent"
	var userInfo = "userInfoForPay"
	var isNeedLogin = false
	var openServer;
	if(publish_version) { // 生产环境配置
		server = "http://www.xmtyw.cn/iot"; // 仕杰
		//      openServer="http://192.168.131.102:8080/user"; // 伟达
		openServer = "http://192.168.131.150:8080"; // 晓阳
	} else { // 测试环境配置
		//      server = "http://192.168.131.202:9099/iot"; // 桃红
//		      server = "http://192.168.131.24:9099/iot"; // 仕杰
		server = "http://172.19.103.88:9099/iot"; // 测试环境
		//		openServer="http://192.168.131.102:8080/user"; // 伟达
		openServer = "http://192.168.131.150:8080"; // 晓阳
	}

	//保存userAgent
	function saveUserAgent(obj) {
	    if(window.localStorage.getItem(agentName)){
	        window.localStorage.removeItem(agentName);
	    }
	    var userAgent = {
	        id: obj.id,
	        token: obj.token || null
	    }
	    userAgent = JSON.stringify(userAgent);
	    window.localStorage.setItem(agentName, userAgent);
	    return userAgent;
	}

	function httpGet(url, options, judge) {
		var setService = judge ? openServer : server
		//发送ajax请求
		return new Promise(function(resolve, reject) {
			$.ajax(setService + url,
				$.extend({}, {
					type: 'GET',
					dataType: 'JSON',
					beforeSend: function(request) {
						var userAgent = window.localStorage.getItem(agentName);
						request.setRequestHeader(agentName, userAgent);
					},
					error: function(res) {
						top.toastr.error("网络错误无法请求")
						reject(res)
					},
					success: function(res) {
						if(isNeedLogin) {
							return
						}
						if(res.status == 998) {
							isNeedLogin = true
							top.toastr.error(res.errorMsg || "登录失效请重新登录！")
							setTimeout(function() {
								location.href = "../login/login.html"
							}, 500)
							return
						}
						resolve(res)
					}
				}, options));
		})
	}

	function httpPost(url, options, judge) {
		var setService = judge ? openServer : server
		//发送ajax请求
		return new Promise(function(resolve, reject) {
			$.ajax(setService + 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) {
						if(isNeedLogin) {
							return
						}
						if(res.status == 998) {
							isNeedLogin = true
							top.toastr.error(res.errorMsg || "登录失效请重新登录！")
							setTimeout(function() {
								location.href = "../login/login.html"
							}, 500)
							return
						}
						resolve(res)
					}
				}, options));
		})
	}

	// 获取异步请求处理Promise对象
	function getReqPromise(url, options, reqType, judge) {
		var setService = judge ? openServer : server
		return new Promise(function(resolve, reject) {
			$.ajax(setService + url, {
				data: options,
				type: reqType,
				dataType: 'JSON',
				beforeSend: function(request) {
					var userAgent = window.localStorage.getItem(agentName);
					request.setRequestHeader(agentName, userAgent);
				},
				error: function(res) {
					top.toastr.error("网络错误无法请求")
					reject(res)
				},
				success: function(res) {
					if(isNeedLogin) {
						return
					}
					if(res.status == 998) {
						isNeedLogin = true
						top.toastr.error(res.errorMsg || "登录失效请重新登录！")
						setTimeout(function() {
							location.href = "../login/login.html"
						}, 500)
						return
					}
					resolve(res)
				}
			})
		});

	}

	/*
	 * reqs: 请求的参数数组，格式：[{url:'a/xxx', reqType: 'POST', data:{...}},{url:'b/xxx',data:{...}}]
	 * */
	function getReqPromises(reqs, judge) {
		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.reqType, judge);
		}));
	};

	httpRequest = {
		server: server,
		openServer: openServer,
		userInfo: userInfo,
		agentName: agentName,
		saveAgent: function(obj) {
    		return saveUserAgent(obj)
    	},
		get: function(url, options, judge) {
			return httpGet(url, options, judge)
		},
		post: function(url, options, judge) {
			return httpPost(url, options, judge)
		},
		getReqPromises: function(reqs, judge) {
			return getReqPromises(reqs, judge);
		}
	}

	exports.httpRequest = httpRequest;
})(window)