Ajax.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. case 0:
  77. return;
  78. }
  79. Dcat.error(_msg || (xhr.status + ' ' + msg));
  80. }
  81. // 处理接口返回数据
  82. handleJsonResponse(response, options) {
  83. let Dcat = this.dcat,
  84. data = response.data;
  85. if (! response) {
  86. return;
  87. }
  88. if (typeof response !== 'object') {
  89. return Dcat.error('error', 'Oops!');
  90. }
  91. var then = function (then) {
  92. switch (then.action) {
  93. case 'refresh':
  94. Dcat.reload();
  95. break;
  96. case 'download':
  97. window.open(then.value, '_blank');
  98. break;
  99. case 'redirect':
  100. Dcat.reload(then.value || null);
  101. break;
  102. case 'location':
  103. setTimeout(function () {
  104. if (then.value) {
  105. window.location = then.value;
  106. } else {
  107. window.location.reload();
  108. }
  109. }, 1000);
  110. break;
  111. case 'script':
  112. (function () {
  113. eval(then.value);
  114. })();
  115. break;
  116. }
  117. };
  118. if (typeof response.html === 'string' && response.html && options.target) {
  119. if (typeof options.html === 'function') {
  120. // 处理api返回的HTML代码
  121. options.html(options.target, response.html, response);
  122. } else {
  123. $(target).html(response.html);
  124. }
  125. }
  126. let message = data.message || response.message;
  127. // 判断默认弹窗类型.
  128. if (! data.type) {
  129. data.type = response.status ? 'success' : 'error';
  130. }
  131. if (typeof message === 'string' && data.type && message) {
  132. if (data.alert) {
  133. Dcat.swal[data.type](message, data.detail);
  134. } else {
  135. Dcat[data.type](message, null, data.timeout ? {timeOut: data.timeout*1000} : {});
  136. }
  137. }
  138. if (data.then) {
  139. then(data.then);
  140. }
  141. }
  142. }