request.ts 7.4 KB

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