request.ts 8.1 KB

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