httpRequest.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import tool from "@/utils/tool";
  2. import sysConfig from "@/config";
  3. import { clearLoginInfo } from "@/utils";
  4. import { ElNotification, ElMessageBox } from "element-plus";
  5. import axios from "axios";
  6. import router from "@/router";
  7. // import axiosRetry from 'axios-retry'
  8. import qs from "qs";
  9. import { ElLoading } from "element-plus";
  10. // 超时时间
  11. axios.defaults.timeout = 100000;
  12. // 跨域请求,允许保存cookie
  13. axios.defaults.withCredentials = true;
  14. axios.defaults.headers = { "Content-Type": "application/json; charset=utf-8" };
  15. // axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
  16. // 非生产环境 && 开启代理, 接口前缀统一使用[/api]前缀做代理拦截!import.meta.env.VITE_APP_BASE
  17. const BASE_URL =
  18. process.env.NODE_ENV !== "production"
  19. ? import.meta.env.VITE_APP_API
  20. : import.meta.env.VITE_APP_BASE;
  21. // 对面暴露的基础请求路径
  22. axios.BASE_URL = BASE_URL;
  23. let showAlert = true;
  24. /**
  25. * 请求拦截
  26. */
  27. let loading;
  28. axios.interceptors.request.use(
  29. (config) => {
  30. let showLoading = false;
  31. if (config.loading === true) {
  32. showLoading = true;
  33. }
  34. if (showLoading) {
  35. loading = ElLoading.service({
  36. text: config.loadingText || "Loading...",
  37. spinner: "el-icon-loading",
  38. background: "rgba(0, 0, 0, 0.7)",
  39. });
  40. }
  41. // 请求头带上token
  42. if (tool.data.get(sysConfig.TOKEN)) {
  43. config.headers.token = tool.data.get(sysConfig.TOKEN);
  44. }
  45. // 请求头带上当前域名
  46. config.headers.domain = window.location.hostname;
  47. // 请求地址处理
  48. if (
  49. !config.url.startsWith("http") &&
  50. !config.url.startsWith(BASE_URL)
  51. ) {
  52. config.url = BASE_URL + config.url;
  53. }
  54. const type = config.method;
  55. const arrayFormat = config.headers.arrayFormat || "indices";
  56. if (
  57. type === "post" &&
  58. config.headers["Content-Type"] ===
  59. "application/x-www-form-urlencoded; charset=utf-8"
  60. ) {
  61. // post请求参数处理
  62. config.data = qs.stringify(config.data, {
  63. allowDots: true,
  64. arrayFormat: arrayFormat,
  65. });
  66. } else if (type === "get") {
  67. // get请求参数处理
  68. config.paramsSerializer = (params) => {
  69. return qs.stringify(params, {
  70. allowDots: true,
  71. arrayFormat: arrayFormat,
  72. });
  73. };
  74. }
  75. return config;
  76. },
  77. (error) => {
  78. return Promise.reject(error);
  79. }
  80. );
  81. /**
  82. * 响应拦截
  83. */
  84. axios.interceptors.response.use(
  85. (response) => {
  86. if (loading) {
  87. loading.close();
  88. }
  89. return response.data;
  90. },
  91. (error) => {
  92. if (loading) {
  93. loading.close();
  94. }
  95. // eslint-disable-next-line no-debugger
  96. if (error.response) {
  97. // eslint-disable-next-line no-debugger
  98. if (error.response.status === 408) {
  99. if (showAlert){
  100. // 超时自动刷新
  101. ElMessageBox.confirm(
  102. "当前用户已被登出或无权限访问当前资源,请尝试重新登录后再操作。",
  103. "无权限访问",
  104. {
  105. type: "error",
  106. closeOnClickModal: false,
  107. center: true,
  108. confirmButtonText: "重新登录",
  109. }
  110. ).then(() => {
  111. clearLoginInfo();
  112. router.replace({ path: "/login" });
  113. });
  114. // 将标识变量设置为 false,表示已经弹出了提示
  115. showAlert = false;
  116. }
  117. } else if (error.response.status === 401) {
  118. // 需要重新登录
  119. ElNotification.error({
  120. title: "登录失败",
  121. message: error.response.data,
  122. });
  123. clearLoginInfo();
  124. router.replace({ path: "/login" });
  125. } else if (error.response.status === 404) {
  126. // 路径找不到
  127. ElNotification.error({
  128. title: "404",
  129. message: "路径找不到" + ": " + error.config.url,
  130. });
  131. } else if (error.response.status === 503) {
  132. ElNotification.error({
  133. title: "503",
  134. message: "服务不可用" + ": " + error.config.url,
  135. });
  136. } else if (error.response.status === 504) {
  137. ElNotification.error({
  138. title: "504",
  139. message: "网络连接错误" + ": " + error.data,
  140. });
  141. } else if (error.response.status === 400) {
  142. ElNotification.error({
  143. title: "请求失败",
  144. message: error.response.data || error.data || error,
  145. });
  146. } else {
  147. ElNotification.error({
  148. title: "系统错误",
  149. message:
  150. (error.response && error.response.data) ||
  151. error.data ||
  152. error,
  153. });
  154. }
  155. }
  156. return Promise.reject(error);
  157. }
  158. );
  159. // 配置axios
  160. // axiosRetry(axios, {
  161. // retries: 3, // 设置自动发送请求次数
  162. // retryCondition: () => {
  163. // // true为打开自动发送请求,false为关闭自动发送请求
  164. // // 这里的意思是当请求方式为get时打开自动发送请求功能
  165. // return false
  166. // }
  167. // })
  168. export default axios;