Ajax.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. export default class Ajax {
  2. constructor(Dcat) {
  3. this.dcat = Dcat;
  4. Dcat.handleAjaxError = this.handleAjaxError.bind(this);
  5. Dcat.handleJsonResponse = this.handleJsonResponse.bind(this);
  6. this.init(Dcat)
  7. }
  8. init(Dcat) {
  9. $.get = function (url, data, success, dataType) {
  10. let options = {
  11. type: 'GET',
  12. url: url,
  13. };
  14. if (typeof data === 'function') {
  15. dataType = success;
  16. success = data;
  17. data = null
  18. }
  19. if (typeof success === 'function') {
  20. options.success = success;
  21. }
  22. if (typeof data === 'object') {
  23. options.data = data
  24. }
  25. if (dataType) {
  26. options.dataType = dataType;
  27. }
  28. return $.ajax(options)
  29. };
  30. $.post = function (options) {
  31. options.type = 'POST';
  32. return $.ajax(options);
  33. };
  34. $.delete = function (options) {
  35. options.type = 'POST';
  36. options.data = {_method: 'DELETE'};
  37. return $.ajax(options);
  38. };
  39. $.put = function (options) {
  40. options.type = 'POST';
  41. Object.assign(options.data, {_method: 'PUT'});
  42. return $.ajax(options);
  43. };
  44. }
  45. handleAjaxError(xhr, text, msg) {
  46. let Dcat = this.dcat,
  47. json = xhr.responseJSON || {},
  48. _msg = json.message;
  49. Dcat.NP.done();
  50. Dcat.loading(false);// 关闭所有loading效果
  51. $('.btn-loading').buttonLoading(false);
  52. switch (xhr.status) {
  53. case 500:
  54. return Dcat.error(_msg || (Dcat.lang['500'] || 'Server internal error.'));
  55. case 403:
  56. return Dcat.error(_msg || (Dcat.lang['403'] || 'Permission deny!'));
  57. case 401:
  58. if (json.login) {
  59. return location.href = json.login;
  60. }
  61. return Dcat.error(Dcat.lang['401'] || 'Unauthorized.');
  62. case 419:
  63. return Dcat.error(Dcat.lang['419'] || 'Sorry, your page has expired.');
  64. case 422:
  65. if (json.errors) {
  66. try {
  67. var err = [], i;
  68. for (i in json.errors) {
  69. err.push(json.errors[i].join('<br/>'));
  70. }
  71. Dcat.error(err.join('<br/>'));
  72. } catch (e) {}
  73. return;
  74. }
  75. }
  76. Dcat.error(_msg || (xhr.status + ' ' + msg));
  77. }
  78. // 处理接口返回数据
  79. handleJsonResponse(response, options) {
  80. let Dcat = this.dcat,
  81. data = response.data;
  82. if (! response) {
  83. return;
  84. }
  85. if (typeof response !== 'object') {
  86. return Dcat.error('error', 'Oops!');
  87. }
  88. var then = function (then) {
  89. switch (then.action) {
  90. case 'refresh':
  91. Dcat.reload();
  92. break;
  93. case 'download':
  94. window.open(then.value, '_blank');
  95. break;
  96. case 'redirect':
  97. Dcat.reload(then.value || null);
  98. break;
  99. case 'location':
  100. setTimeout(function () {
  101. if (then.value) {
  102. window.location = then.value;
  103. } else {
  104. window.location.reload();
  105. }
  106. }, 1000);
  107. break;
  108. case 'script':
  109. (function () {
  110. eval(then.value);
  111. })();
  112. break;
  113. }
  114. };
  115. if (typeof response.html === 'string' && response.html && options.target) {
  116. if (typeof options.html === 'function') {
  117. // 处理api返回的HTML代码
  118. options.html(options.target, response.html, response);
  119. } else {
  120. $(target).html(response.html);
  121. }
  122. }
  123. let message = data.message || response.message;
  124. // 判断默认弹窗类型.
  125. if (! data.type) {
  126. data.type = response.status ? 'success' : 'error';
  127. }
  128. if (typeof message === 'string' && data.type && message) {
  129. if (data.alert) {
  130. Dcat.swal[data.type](message, data.detail);
  131. } else {
  132. Dcat[data.type](message, null, data.timeout ? {timeOut: data.timeout*1000} : {});
  133. }
  134. }
  135. if (data.then) {
  136. then(data.then);
  137. }
  138. }
  139. }