123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- (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)
|