Ajax.js 4.8 KB

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