(function(exports) { var publish_version = true; var server; var getServer; var agentName = "useragent" var userInfo = "userInfoForPay" var isNeedLogin = false var openServer,alertUrl; var asstoken = sessionStorage.getItem("asstoken") if(asstoken){ sessionStorage.accessToken=asstoken } if(publish_version) { // 生产环境配置 server = "http://jksr.srswjw.gov.cn:1235"; alertUrl="http://jksr.srswjw.gov.cn:1235/bigScreenShow"; getServer=server +'/gateway' } else { // 测试环境配置 server = "http://172.19.103.70:1235"; // 测试环境 // server = "http://192.168.131.121:10220/"; // 晓阳 // server = "http://172.19.103.74:10220"; // 永兴 getServer=server +'/gateway' alertUrl="http://172.19.103.70:1235/bigScreenShow"; // server = "http://192.168.131.104:9088"; // 测试环境 周杰 // server = "http://192.168.131.105:10001/quota"; // 测试环境 兴旺 // server = "http://192.168.131.105:9088"; // 测试环境 兴旺 } if(!top.toastr)top.toastr=toastr; //保存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; setService =options.getServer?getServer:setService; //发送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); if(asstoken) { request.setRequestHeader("token", asstoken); } }, error: function(res) { reject(res) top.toastr.error("网络错误无法请求") }, 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); if(asstoken) { request.setRequestHeader("token", asstoken); } }, 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); if(asstoken) { request.setRequestHeader("token", asstoken); } }, 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); })); }; function httpGetJson(url, options) { //发送ajax请求 return new Promise(function(resolve, reject) { $.ajax(url, $.extend({}, { type: 'GET', dataType: 'JSON', success: function(res) { resolve(res) } }, options)); }) } httpRequest = { server: server, openServer: openServer, userInfo: userInfo, agentName: agentName, alertUrl:alertUrl, 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); }, getJson:function(url,options){ return httpGetJson(url,options) }, getReqPromise:getReqPromise } exports.httpRequest = httpRequest; })(window)