request.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import axios, { AxiosInstance, AxiosResponse, AxiosError, AxiosRequestConfig } from 'axios';
  2. import { ElMessage, ElMessageBox, ElLoading, LoadingOptionsResolved } from 'element-plus';
  3. import { Session, Local } from '/@/utils/storage';
  4. import router from "/@/router/index"
  5. // 全局配置
  6. import {apiUrl} from "/@/utils/appConfig";
  7. // 重复请求队列
  8. const pendingMap = new Map();
  9. // loading
  10. const LoadingInstance = {
  11. _target: null as any,
  12. _count: 0
  13. };
  14. export default function myAxios(axiosConfig: any, customOptions?: any, loadingOptions?: LoadingOptionsResolved) {
  15. // 配置新建一个 axios 实例
  16. const service: AxiosInstance = axios.create({
  17. baseURL: apiUrl,
  18. timeout: 50000,
  19. headers: { 'Content-Type': 'application/json' },
  20. });
  21. // 自定义配置
  22. let custom_options = Object.assign({
  23. repeat_request_cancel: true, // 是否开启取消重复请求, 默认为 true
  24. loading: false, // 是否开启全屏loading层效果, 默认为false
  25. reduct_data_format: true, // 是否开启简洁的数据结构响应 减少一层data, 默认为true
  26. error_message_show: true, // 是否开启接口错误信息展示,默认为true
  27. code_message_show: false, // 是否开启code不为0时的信息提示, 默认为false
  28. }, customOptions);
  29. // 添加请求拦截器
  30. service.interceptors.request.use(
  31. (config: AxiosRequestConfig) => {
  32. removePending(config);
  33. custom_options.repeat_request_cancel && addPending(config);
  34. // 创建loading实例
  35. if (custom_options.loading) {
  36. LoadingInstance._count++;
  37. if (LoadingInstance._count === 1) {
  38. LoadingInstance._target = ElLoading.service(loadingOptions);
  39. }
  40. }
  41. // 在发送请求之前做些什么 token
  42. if (Session.get('token')) {
  43. (<any>config.headers)['Authorization'] = `Bearer ${Session.get('token')}`;
  44. }
  45. return config;
  46. },
  47. async (error: AxiosError) => {
  48. return Promise.reject(error);
  49. }
  50. );
  51. // 添加响应拦截器
  52. service.interceptors.response.use(
  53. (response: AxiosResponse): any => {
  54. removePending(response.config);
  55. custom_options.loading && closeLoading(custom_options); // 关闭loading
  56. // 对响应数据做点什么
  57. if (custom_options.code_message_show && response.data && response.data.code !== 0) {
  58. ElMessage({
  59. type: 'error',
  60. message: response.data.message
  61. })
  62. return Promise.reject(response.data); // code不等于0, 页面具体逻辑就不执行了
  63. }
  64. return custom_options.reduct_data_format ? response.data : response;
  65. },
  66. async (error: AxiosError): Promise<never> => {
  67. error.config && removePending(error.config);
  68. custom_options.loading && closeLoading(custom_options); // 关闭loading
  69. custom_options.error_message_show && httpErrorStatusHandle(error); // 处理错误状态码
  70. return Promise.reject(error); // 错误继续返回给到具体页面
  71. }
  72. );
  73. return service(axiosConfig)
  74. }
  75. /**
  76. * 处理异常
  77. * @param {*} error
  78. */
  79. function httpErrorStatusHandle(error: any) {
  80. // 设置一个变量 处理同一时间多个错误重复弹窗口
  81. let tokenAbnormal: boolean = false;
  82. // 处理被取消的请求
  83. if (axios.isCancel(error)) return;
  84. let message = '';
  85. if (error && error.response) {
  86. switch (error.response.status) {
  87. case 302: message = '接口重定向了!'; break;
  88. case 400: message = '参数不正确!'; break;
  89. case 401:
  90. if (!tokenAbnormal) {
  91. tokenAbnormal = true;
  92. // 弹出框
  93. ElMessageBox.alert('你已被登出,请重新登录', '提示', { type: 'warning' }).then(() => {
  94. Session.clear(); // 清除浏览器全部临时缓存
  95. Local.clear(); // 清除浏览器全部临时缓存
  96. router.replace(`/login?redirect=${router.currentRoute.value.path}&params=${JSON.stringify(router.currentRoute.value.query ? router.currentRoute.value.query : router.currentRoute.value.params)}`); // 去登录页
  97. location.reload(); //刷新页面
  98. }).catch((): void => { });
  99. // 设置定时器,确保下次异常时弹出框正常弹出
  100. setTimeout(() => {
  101. tokenAbnormal = false;
  102. }, 3000);
  103. }
  104. break;
  105. case 403: message = '您没有权限操作!'; break;
  106. case 404: message = `请求地址出错: ${error.response.config.url}`; break; // 在正确域名下
  107. case 408: message = '请求超时!'; break;
  108. case 409: message = '系统已存在相同数据!'; break;
  109. case 500:
  110. if (error.response?.data.message) message = error.response.data.message;
  111. else message = '服务器内部错误!';
  112. break;
  113. case 501: message = '服务未实现!'; break;
  114. case 502: message = '网关错误!'; break;
  115. case 503: message = '服务不可用!'; break;
  116. case 504: message = '服务暂时无法访问,请稍后再试!'; break;
  117. case 505: message = 'HTTP版本不受支持!'; break;
  118. default: message = '异常问题,请联系管理员!'; break
  119. }
  120. }
  121. if (error.message.includes('timeout')) message = '网络请求超时!';
  122. if (error.message.includes('Network')) message = window.navigator.onLine ? '服务端异常!' : '您断网了!';
  123. ElMessage({
  124. type: 'error',
  125. message
  126. })
  127. }
  128. /**
  129. * 关闭Loading层实例
  130. * @param {*} _options
  131. */
  132. function closeLoading(_options: any) {
  133. if (_options.loading && LoadingInstance._count > 0) LoadingInstance._count--;
  134. if (LoadingInstance._count === 0) {
  135. LoadingInstance._target.close();
  136. LoadingInstance._target = null;
  137. }
  138. }
  139. /**
  140. * 储存每个请求的唯一cancel回调, 以此为标识
  141. * @param {*} config
  142. */
  143. function addPending(config: AxiosRequestConfig) {
  144. const pendingKey = getPendingKey(config);
  145. config.cancelToken = config.cancelToken || new axios.CancelToken((cancel) => {
  146. if (!pendingMap.has(pendingKey)) {
  147. pendingMap.set(pendingKey, cancel);
  148. }
  149. });
  150. }
  151. /**
  152. * 删除重复的请求
  153. * @param {*} config
  154. */
  155. function removePending(config: AxiosRequestConfig) {
  156. const pendingKey = getPendingKey(config);
  157. if (pendingMap.has(pendingKey)) {
  158. const cancelToken = pendingMap.get(pendingKey);
  159. // 如你不明白此处为什么需要传递pendingKey可以看文章下方的补丁解释
  160. cancelToken(pendingKey);
  161. pendingMap.delete(pendingKey);
  162. }
  163. }
  164. /**
  165. * 生成唯一的每个请求的唯一key
  166. * @param {*} config
  167. * @returns
  168. */
  169. function getPendingKey(config: AxiosRequestConfig) {
  170. let { url, method, params, data } = config;
  171. if (typeof data === 'string') data = JSON.parse(data); // response里面返回的config.data是个字符串对象
  172. return [url, method, JSON.stringify(params), JSON.stringify(data)].join('&');
  173. }