wms-ajax.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * 描 述:ajax操作方法
  3. */
  4. (function ($, learun) {
  5. "use strict";
  6. var httpCode = {
  7. success: 1,
  8. fail: 0,
  9. exception: -1,
  10. nologin: 410 // 没有登录者信息
  11. };
  12. var exres = { code: -1, info: '通信异常,请联系管理员!' }
  13. function isLogin(res) {
  14. if (res.code == learun.httpCode.nologin) {
  15. var _topUrl = top.$.rootUrl + '/Login/Index';
  16. switch (res.info) {
  17. case 'nologin':
  18. break;
  19. case 'noip':
  20. _topUrl += '?error=ip';
  21. break;
  22. case 'notime':
  23. _topUrl += '?error=time';
  24. break;
  25. case 'other':
  26. _topUrl += '?error=other';
  27. break;
  28. }
  29. top.window.location.href = _topUrl;
  30. return false;
  31. }
  32. return true;
  33. }
  34. function httpHeaders() {
  35. var headers = {
  36. token: $.lcoreUser.token,
  37. /* "Content-Type": "application/json"*/
  38. }
  39. return headers;
  40. }
  41. $.extend(learun, {
  42. // http 通信异常的时候调用此方法
  43. httpErrorLog: function (msg) {
  44. learun.log(msg);
  45. },
  46. // http请求返回数据码
  47. httpCode: httpCode,
  48. // get请求方法(异步):url地址,callback回调函数
  49. httpAsyncGet: function (url, callback) {
  50. $.ajax({
  51. url: url,
  52. headers: httpHeaders(),
  53. type: "GET",
  54. dataType: "json",
  55. async: true,
  56. cache: false,
  57. success: function (res) {
  58. isLogin(res);
  59. callback(res);
  60. },
  61. error: function (XMLHttpRequest, textStatus, errorThrown) {
  62. callback(exres);
  63. },
  64. beforeSend: function () {
  65. },
  66. complete: function () {
  67. }
  68. });
  69. },
  70. // get请求方法(同步):url地址,param参数
  71. httpGet: function (url, param) {
  72. var res = {};
  73. $.ajax({
  74. url: url,
  75. headers: httpHeaders(),
  76. data: param,
  77. type: "GET",
  78. dataType: "json",
  79. async: false,
  80. cache: false,
  81. success: function (res) {
  82. isLogin(res);
  83. return res;
  84. },
  85. error: function (XMLHttpRequest, textStatus, errorThrown) {
  86. return exres;
  87. },
  88. beforeSend: function () {
  89. },
  90. complete: function () {
  91. }
  92. });
  93. return res;
  94. },
  95. // post请求方法(异步):url地址,param参数,callback回调函数
  96. httpAsyncPost: function (url, param, callback) {
  97. $.ajax({
  98. url: url,
  99. headers: httpHeaders(),
  100. data: param,
  101. type: "POST",
  102. dataType: "json",
  103. async: true,
  104. cache: false,
  105. success: function (res) {
  106. isLogin(res);
  107. callback(res);
  108. },
  109. error: function (XMLHttpRequest, textStatus, errorThrown) {
  110. callback(exres);
  111. },
  112. beforeSend: function () {
  113. },
  114. complete: function () {
  115. }
  116. });
  117. },
  118. // post请求方法(同步步):url地址,param参数,callback回调函数
  119. httpPost: function (url, param, callback) {
  120. $.ajax({
  121. url: url,
  122. headers: httpHeaders(),
  123. data: param,
  124. type: "POST",
  125. dataType: "json",
  126. async: false,
  127. cache: false,
  128. success: function (res) {
  129. isLogin(res);
  130. callback(res);
  131. },
  132. error: function (XMLHttpRequest, textStatus, errorThrown) {
  133. callback(exres);
  134. },
  135. beforeSend: function () {
  136. },
  137. complete: function () {
  138. }
  139. });
  140. },
  141. // ajax 异步封装
  142. httpAsync: function (type, url, param, callback) {
  143. $.ajax({
  144. url: url,
  145. headers: httpHeaders(),
  146. data: param,
  147. type: type,
  148. dataType: "json",
  149. async: true,
  150. cache: false,
  151. success: function (res) {
  152. if (!isLogin(res)) {
  153. callback(null);
  154. } else {
  155. callback(res);
  156. }
  157. },
  158. error: function (XMLHttpRequest, textStatus, errorThrown) {
  159. callback(exres);
  160. },
  161. beforeSend: function () {
  162. },
  163. complete: function () {
  164. }
  165. });
  166. },
  167. // ajax 同步封装
  168. httpSync: function (type, url, param, callback) {
  169. $.ajax({
  170. url: url,
  171. headers: httpHeaders(),
  172. data: param,
  173. type: type,
  174. dataType: "json",
  175. async: false,
  176. cache: false,
  177. success: function (res) {
  178. if (!isLogin(res)) {
  179. callback(null);
  180. }
  181. else {
  182. callback(res);
  183. }
  184. },
  185. error: function (XMLHttpRequest, textStatus, errorThrown) {
  186. callback(exres);
  187. },
  188. beforeSend: function () {
  189. },
  190. complete: function () {
  191. }
  192. });
  193. },
  194. deleteForm: function (url, param, callback) {
  195. learun.loading(true, '正在删除数据');
  196. learun.httpAsyncPost(url, param, function (res) {
  197. learun.loading(false);
  198. if (res.code > 0) {
  199. if (!!callback) {
  200. callback(res);
  201. }
  202. learun.alert.success(res.info);
  203. }
  204. else {
  205. learun.alert.error(res.info);
  206. }
  207. layer.close(layer.index);
  208. });
  209. },
  210. postForm: function (url, param, callback) {
  211. learun.loading(true, '正在提交数据');
  212. learun.httpAsyncPost(url, param, function (res) {
  213. learun.loading(false);
  214. if (res.code > 0) {
  215. if (!!callback) {
  216. callback(res);
  217. }
  218. learun.alert.success(res.info);
  219. }
  220. else {
  221. learun.alert.error(res.info);
  222. }
  223. layer.close(layer.index);
  224. });
  225. }
  226. });
  227. })(window.jQuery, top.learun);