Ajax.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.redirect) {
  60. return location.href = json.redirect;
  61. }
  62. return Dcat.error(Dcat.lang['401'] || 'Unauthorized.');
  63. case 301:
  64. case 302:
  65. console.log('admin redirect', json);
  66. if (json.redirect) {
  67. return location.href = json.redirect;
  68. }
  69. return;
  70. case 419:
  71. return Dcat.error(Dcat.lang['419'] || 'Sorry, your page has expired.');
  72. case 422:
  73. if (json.errors) {
  74. try {
  75. var err = [], i;
  76. for (i in json.errors) {
  77. err.push(json.errors[i].join('<br/>'));
  78. }
  79. Dcat.error(err.join('<br/>'));
  80. } catch (e) {}
  81. return;
  82. }
  83. case 0:
  84. return;
  85. }
  86. Dcat.error(_msg || (xhr.status + ' ' + msg));
  87. }
  88. // 处理接口返回数据
  89. handleJsonResponse(response, options) {
  90. let Dcat = this.dcat,
  91. data = response.data;
  92. if (! response) {
  93. return;
  94. }
  95. if (typeof response !== 'object') {
  96. return Dcat.error('error', 'Oops!');
  97. }
  98. var then = function (then) {
  99. switch (then.action) {
  100. case 'refresh':
  101. Dcat.reload();
  102. break;
  103. case 'download':
  104. window.open(then.value, '_blank');
  105. break;
  106. case 'redirect':
  107. Dcat.reload(then.value || null);
  108. break;
  109. case 'location':
  110. setTimeout(function () {
  111. if (then.value) {
  112. window.location = then.value;
  113. } else {
  114. window.location.reload();
  115. }
  116. }, 1000);
  117. break;
  118. case 'script':
  119. (function () {
  120. eval(then.value);
  121. })();
  122. break;
  123. }
  124. };
  125. if (typeof response.html === 'string' && response.html && options.target) {
  126. if (typeof options.html === 'function') {
  127. // 处理api返回的HTML代码
  128. options.html(options.target, response.html, response);
  129. } else {
  130. $(target).html(response.html);
  131. }
  132. }
  133. let message = data.message || response.message;
  134. // 判断默认弹窗类型.
  135. if (! data.type) {
  136. data.type = response.status ? 'success' : 'error';
  137. }
  138. if (typeof message === 'string' && data.type && message) {
  139. if (data.alert) {
  140. Dcat.swal[data.type](message, data.detail);
  141. } else {
  142. Dcat[data.type](message, null, data.timeout ? {timeOut: data.timeout*1000} : {});
  143. }
  144. }
  145. if (data.then) {
  146. then(data.then);
  147. }
  148. }
  149. }