axios.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import axios from "axios";
  2. import qs from "qs";
  3. import ViewUI from "view-design";
  4. // import router from "../router";
  5. // import Vue from "vue";
  6. /**
  7. * errorCallback 请求失败的回调
  8. * @param {Object} error 请求失败时的错误信息
  9. */
  10. const errorCallback = error => {
  11. let content = "";
  12. if (error.response) {
  13. content =
  14. (error.response.data && error.response.data.message) || "服务错误";
  15. } else if (error.request) {
  16. content = "请求错误";
  17. if (error.message.indexOf("timeout") > -1) {
  18. content = "请求超时";
  19. }
  20. } else {
  21. return error;
  22. }
  23. content = content.indexOf("###") !== -1 ? "参数错误" : content;
  24. ViewUI.Notice.error({ title: "错误提示", desc: content });
  25. return error;
  26. };
  27. /**
  28. * errorDataCallback 请求成功,结果有误的回调
  29. * @param {Object} error Response中的data信息
  30. */
  31. // const errorDataCallback = error => {
  32. // let content = error.message || "数据错误";
  33. // content = content.indexOf("###") !== -1 ? "参数错误" : content;
  34. // // TODO:自定义处理逻辑,以下为epcc实例
  35. // if (error.code === -100) {
  36. // content = "身份验证失效,请重新登录";
  37. // ViewUI.Modal.confirm({
  38. // title: "重新登陆?",
  39. // content,
  40. // onOk: () => {
  41. // Vue.ls.clear();
  42. // router.push({ name: "Login" });
  43. // }
  44. // });
  45. // } else {
  46. // ViewUI.Notice.error({ title: "错误提示", desc: content });
  47. // }
  48. // return error;
  49. // };
  50. /**
  51. * response format
  52. * {
  53. config, header, data, request, status, statusText
  54. }
  55. *
  56. */
  57. /**
  58. * successCallback 请求成功的回调
  59. * @param {Object} data Response中的data信息
  60. */
  61. const successCallback = data => {
  62. return data;
  63. // if (data.code === 0) {
  64. // return data.data;
  65. // } else {
  66. // throw new Error(errorDataCallback(data));
  67. // }
  68. };
  69. /**
  70. * get请求
  71. * @param {String} url 请求地址
  72. * @param {Object} datas 请求数据
  73. */
  74. const $get = (url, datas) => {
  75. let sqDatas = "";
  76. if (datas) {
  77. sqDatas = qs.stringify(datas, {
  78. arrayFormat: "brackets"
  79. });
  80. url += "?" + sqDatas;
  81. }
  82. return axios
  83. .get(url)
  84. .then(rep => {
  85. return successCallback(rep.data);
  86. })
  87. .catch(error => {
  88. throw new Error(errorCallback(error));
  89. });
  90. };
  91. /**
  92. * post请求
  93. * @param {String} url 请求地址
  94. * @param {Object} datas 请求数据
  95. */
  96. const $post = (url, datas) => {
  97. return axios
  98. .post(url, datas)
  99. .then(rep => {
  100. return successCallback(rep.data);
  101. })
  102. .catch(error => {
  103. throw new Error(errorCallback(error));
  104. });
  105. };
  106. /**
  107. * delete请求
  108. * @param {String} url
  109. * @param {Object} datas
  110. */
  111. const $del = (url, datas) => {
  112. let sqDatas = "";
  113. if (datas) {
  114. sqDatas = qs.stringify(datas, { arrayFormat: "brackets" });
  115. url += "?" + sqDatas;
  116. }
  117. return axios
  118. .delete(url)
  119. .then(rep => {
  120. return rep.data;
  121. })
  122. .catch(error => {
  123. throw new Error(errorCallback(error));
  124. });
  125. };
  126. /**
  127. * put 请求
  128. * @param {String} url 请求地址
  129. * @param {Object} datas 请求数据
  130. */
  131. const $put = (url, datas) => {
  132. return axios
  133. .put(url, datas)
  134. .then(rep => {
  135. return rep.data;
  136. })
  137. .catch(error => {
  138. throw new Error(errorCallback(error));
  139. });
  140. };
  141. /**
  142. * patch请求
  143. * @param {String} url 请求地址
  144. * @param {Object} datas 请求数据
  145. */
  146. const $patch = (url, datas) => {
  147. return axios
  148. .patch(url, datas)
  149. .then(rep => {
  150. return rep.data;
  151. })
  152. .catch(error => {
  153. throw new Error(errorCallback(error));
  154. });
  155. };
  156. export { $get, $post, $del, $put, $patch };