http-request.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. (function(exports) {
  2. var publish_version = true;
  3. var server;
  4. var getServer;
  5. var agentName = "useragent"
  6. var userInfo = "userInfoForPay"
  7. var isNeedLogin = false
  8. var openServer,alertUrl;
  9. var asstoken = sessionStorage.getItem("asstoken")
  10. if(asstoken){
  11. sessionStorage.accessToken=asstoken
  12. }
  13. if(publish_version) { // 生产环境配置
  14. server = "http://jksr.srswjw.gov.cn:1235";
  15. alertUrl="http://jksr.srswjw.gov.cn:1235/bigScreenShow";
  16. getServer=server +'/gateway'
  17. } else { // 测试环境配置
  18. server = "http://172.19.103.70:1235"; // 测试环境
  19. // server = "http://192.168.131.121:10220/"; // 晓阳
  20. // server = "http://172.19.103.74:10220"; // 永兴
  21. getServer=server +'/gateway'
  22. alertUrl="http://172.19.103.70:1235/bigScreenShow";
  23. // server = "http://192.168.131.104:9088"; // 测试环境 周杰
  24. // server = "http://192.168.131.105:10001/quota"; // 测试环境 兴旺
  25. // server = "http://192.168.131.105:9088"; // 测试环境 兴旺
  26. }
  27. if(!top.toastr)top.toastr=toastr;
  28. //保存userAgent
  29. function saveUserAgent(obj) {
  30. if(window.localStorage.getItem(agentName)){
  31. window.localStorage.removeItem(agentName);
  32. }
  33. var userAgent = {
  34. id: obj.id,
  35. token: obj.token || null
  36. }
  37. userAgent = JSON.stringify(userAgent);
  38. window.localStorage.setItem(agentName, userAgent);
  39. return userAgent;
  40. }
  41. function httpGet(url, options, judge) {
  42. var setService = judge ? openServer : server;
  43. setService =options.getServer?getServer:setService;
  44. //发送ajax请求
  45. return new Promise(function(resolve, reject) {
  46. $.ajax(setService + url,
  47. $.extend({}, {
  48. type: 'GET',
  49. dataType: 'JSON',
  50. beforeSend: function(request) {
  51. var userAgent = window.localStorage.getItem(agentName);
  52. request.setRequestHeader(agentName, userAgent);
  53. if(asstoken) {
  54. request.setRequestHeader("token", asstoken);
  55. }
  56. },
  57. error: function(res) {
  58. reject(res)
  59. top.toastr.error("网络错误无法请求")
  60. },
  61. success: function(res) {
  62. if(isNeedLogin) {
  63. return
  64. }
  65. if(res.status == 998) {
  66. isNeedLogin = true
  67. top.toastr.error(res.errorMsg || "登录失效请重新登录!")
  68. setTimeout(function() {
  69. location.href = "../login/login.html"
  70. }, 500)
  71. return
  72. }
  73. resolve(res)
  74. }
  75. }, options));
  76. })
  77. }
  78. function httpPost(url, options, judge) {
  79. var setService = judge ? openServer : server
  80. //发送ajax请求
  81. return new Promise(function(resolve, reject) {
  82. $.ajax(setService + url,
  83. $.extend({}, {
  84. type: 'POST',
  85. dataType: 'JSON',
  86. beforeSend: function(request) {
  87. var userAgent = window.localStorage.getItem(agentName);
  88. request.setRequestHeader(agentName, userAgent);
  89. if(asstoken) {
  90. request.setRequestHeader("token", asstoken);
  91. }
  92. },
  93. error: function(res) {
  94. reject(res)
  95. },
  96. success: function(res) {
  97. if(isNeedLogin) {
  98. return
  99. }
  100. if(res.status == 998) {
  101. isNeedLogin = true
  102. top.toastr.error(res.errorMsg || "登录失效请重新登录!")
  103. setTimeout(function() {
  104. location.href = "../login/login.html"
  105. }, 500)
  106. return
  107. }
  108. resolve(res)
  109. }
  110. }, options));
  111. })
  112. }
  113. // 获取异步请求处理Promise对象
  114. function getReqPromise(url, options, reqType, judge) {
  115. var setService = judge ? openServer : server
  116. return new Promise(function(resolve, reject) {
  117. $.ajax(setService + url, {
  118. data: options,
  119. type: reqType,
  120. dataType: 'JSON',
  121. beforeSend: function(request) {
  122. var userAgent = window.localStorage.getItem(agentName);
  123. request.setRequestHeader(agentName, userAgent);
  124. if(asstoken) {
  125. request.setRequestHeader("token", asstoken);
  126. }
  127. },
  128. error: function(res) {
  129. top.toastr.error("网络错误无法请求")
  130. reject(res)
  131. },
  132. success: function(res) {
  133. if(isNeedLogin) {
  134. return
  135. }
  136. if(res.status == 998) {
  137. isNeedLogin = true
  138. top.toastr.error(res.errorMsg || "登录失效请重新登录!")
  139. setTimeout(function() {
  140. location.href = "../login/login.html"
  141. }, 500)
  142. return
  143. }
  144. resolve(res)
  145. }
  146. })
  147. });
  148. }
  149. /*
  150. * reqs: 请求的参数数组,格式:[{url:'a/xxx', reqType: 'POST', data:{...}},{url:'b/xxx',data:{...}}]
  151. * */
  152. function getReqPromises(reqs, judge) {
  153. if(!reqs || !reqs.length) {
  154. return new Promise(function(resolve, reject) {
  155. resolve([]);
  156. });
  157. }
  158. return Promise.all(_.map(reqs, function(param) {
  159. return getReqPromise(param.url, param.data, param.reqType, judge);
  160. }));
  161. };
  162. function httpGetJson(url, options) {
  163. //发送ajax请求
  164. return new Promise(function(resolve, reject) {
  165. $.ajax(url,
  166. $.extend({}, {
  167. type: 'GET',
  168. dataType: 'JSON',
  169. success: function(res) {
  170. resolve(res)
  171. }
  172. }, options));
  173. })
  174. }
  175. httpRequest = {
  176. server: server,
  177. openServer: openServer,
  178. userInfo: userInfo,
  179. agentName: agentName,
  180. alertUrl:alertUrl,
  181. saveAgent: function(obj) {
  182. return saveUserAgent(obj)
  183. },
  184. get: function(url, options, judge) {
  185. return httpGet(url, options, judge)
  186. },
  187. post: function(url, options, judge) {
  188. return httpPost(url, options, judge)
  189. },
  190. getReqPromises: function(reqs, judge) {
  191. return getReqPromises(reqs, judge);
  192. },
  193. getJson:function(url,options){
  194. return httpGetJson(url,options)
  195. },
  196. getReqPromise:getReqPromise
  197. }
  198. exports.httpRequest = httpRequest;
  199. })(window)