http.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { MessagePlugin } from "tdesign-vue-next";
  2. import axios from "axios";
  3. import { getCookie } from "@/services/cookie";
  4. import router from "./router";
  5. // axios 配置
  6. axios.defaults.timeout = 60000;
  7. axios.defaults.withCredentials = false;
  8. const requestDebounceMap = new Map();
  9. const requestThrottleSet = new Set();
  10. // http request 拦截器
  11. axios.interceptors.request.use(
  12. (config: any) => {
  13. config.headers.Authorization =
  14. "Bearer " + (localStorage.token || getCookie("token") || "");
  15. if (config.params) {
  16. // 防抖, 比如输入搜索
  17. if (config.params.debounce > 0) {
  18. const url = config.method + config.url;
  19. const cache: any = requestDebounceMap.get(url);
  20. if (cache) {
  21. clearTimeout(cache.timer);
  22. cache.reject();
  23. }
  24. delete config.params.debounce;
  25. delete config.params.throttle;
  26. return new Promise((resolve, reject) => {
  27. const newCache: any = { reject };
  28. newCache.timer = setTimeout(() => {
  29. requestDebounceMap.delete(url);
  30. resolve(config);
  31. }, config.params.debounce);
  32. requestDebounceMap.set(url, newCache);
  33. });
  34. }
  35. // 节流,避免短时间重复请求,比如点击确定
  36. else if (config.params.throttle > 0) {
  37. const url = config.method + config.url;
  38. // 已经存在,取消重复请求
  39. if (requestThrottleSet.has(url)) {
  40. return Promise.reject("Repeated request.");
  41. }
  42. requestThrottleSet.add(url);
  43. setTimeout(() => {
  44. requestThrottleSet.delete(url);
  45. }, config.params.throttle);
  46. }
  47. delete config.params.debounce;
  48. delete config.params.throttle;
  49. }
  50. return config;
  51. },
  52. (err: any) => Promise.reject(err)
  53. );
  54. // http response 拦截器
  55. axios.interceptors.response.use(
  56. (response: any) => {
  57. if (response && response.data && response.data.error) {
  58. MessagePlugin.error(response.data.error);
  59. return;
  60. }
  61. if (response) {
  62. return response.data;
  63. }
  64. return;
  65. },
  66. (error: any) => {
  67. if (error && !error.response) {
  68. return;
  69. }
  70. if (error && error.response) {
  71. if (error.response.config.url === "/api/account/profile") {
  72. return;
  73. }
  74. if (
  75. error.response.status !== 401 &&
  76. error.response &&
  77. error.response.data &&
  78. error.response.data.error
  79. ) {
  80. MessagePlugin.error(error.response.data.error);
  81. return;
  82. }
  83. switch (error.response.status) {
  84. case 401:
  85. sessionStorage.setItem("cb", encodeURIComponent(location.href));
  86. router.replace({ path: "/login" });
  87. break;
  88. case 403:
  89. MessagePlugin.error("请求错误,不合法的请求!");
  90. break;
  91. case 404:
  92. if (error.response.config.url.indexOf("/data/") !== 0) {
  93. MessagePlugin.error("访问数据不存在,请检查后重试!");
  94. }
  95. break;
  96. case 500:
  97. MessagePlugin.error("请求服务错误,请稍后重试!");
  98. break;
  99. case 504:
  100. MessagePlugin.error("网络超时,请检测你的网络!");
  101. break;
  102. default:
  103. MessagePlugin.error("未知网络错误!");
  104. break;
  105. }
  106. }
  107. // return error.response ? error.response.data : error;
  108. }
  109. );
  110. export default axios;