request.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import axios, {AxiosInstance, AxiosRequestConfig, AxiosResponse} from 'axios';
  2. import {Session} from '/@/utils/storage';
  3. import {useMessageBox} from '/@/hooks/message';
  4. import qs from 'qs';
  5. import other from './other';
  6. /**
  7. * 创建并配置一个 Axios 实例对象
  8. */
  9. const service: AxiosInstance = axios.create({
  10. baseURL: import.meta.env.VITE_API_URL,
  11. timeout: 50000, // 全局超时时间
  12. paramsSerializer: (params: any) => {
  13. return qs.stringify(params, { arrayFormat: 'repeat' });
  14. }
  15. });
  16. /**
  17. * Axios请求拦截器,对请求进行处理
  18. * 1. 序列化get请求参数
  19. * 2. 统一增加Authorization和TENANT-ID请求头
  20. * 3. 自动适配单体、微服务架构不同的URL
  21. * @param config AxiosRequestConfig对象,包含请求配置信息
  22. */
  23. service.interceptors.request.use(
  24. (config: AxiosRequestConfig) => {
  25. // 统一增加Authorization请求头, skipToken 跳过增加token
  26. const token = Session.getToken();
  27. if (token && !config.headers?.skipToken) {
  28. config.headers![CommonHeaderEnum.AUTHORIZATION] = `Bearer ${token}`;
  29. }
  30. // 请求报文加密
  31. if(config.method == "get"){
  32. let newParam = config.params || {};
  33. const enc = other.encryption(JSON.stringify(newParam));
  34. config.params = {
  35. value: encodeURI(enc)
  36. }
  37. }
  38. if(config.method == "post" || config.method == "put"){
  39. let data = config.data;
  40. config.data = other.encryption(JSON.stringify(data))
  41. config.headers!['Content-Type'] = 'application/json'
  42. }
  43. // 自动适配单体和微服务架构不同的URL
  44. config.url = other.adaptationUrl(config.url);
  45. // 处理完毕,返回config对象
  46. return config;
  47. },
  48. (error) => {
  49. // 对请求错误进行处理
  50. return Promise.reject(error);
  51. }
  52. );
  53. /**
  54. * 响应拦截器处理函数
  55. * @param response 响应结果
  56. * @returns 如果响应成功,则返回响应的data属性;否则,抛出错误或者执行其他操作
  57. */
  58. const handleResponse = (response: AxiosResponse<any>) => {
  59. if (response.data.code === 1) {
  60. throw response.data;
  61. }
  62. // 针对密文返回解密
  63. if (response.data.encryption) {
  64. const originData = JSON.parse(other.decryption(response.data.encryption));
  65. response.data = originData;
  66. return response.data;
  67. }
  68. return response.data;
  69. };
  70. /**
  71. * 添加 Axios 的响应拦截器,用于全局响应结果处理
  72. */
  73. service.interceptors.response.use(handleResponse, (error) => {
  74. console.log(error)
  75. const status = Number(error.response.status) || 200;
  76. if (status === 424) {
  77. useMessageBox()
  78. .confirm('令牌状态已过期,请点击重新登录')
  79. .then(() => {
  80. Session.clear(); // 清除浏览器全部临时缓存
  81. window.location.href = '/'; // 去登录页
  82. return;
  83. });
  84. }
  85. return Promise.reject(error.response.data);
  86. });
  87. // 常用header
  88. export enum CommonHeaderEnum {
  89. 'TENANT_ID' = 'TENANT-ID',
  90. 'ENC_FLAG' = 'Enc-Flag',
  91. 'AUTHORIZATION' = 'Authorization',
  92. }
  93. // 导出 axios 实例
  94. export default service;