request.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * axios 实例
  3. */
  4. import axios from 'axios';
  5. import router from '@/router';
  6. import store from '@/store';
  7. import { MessageBox, Message } from 'element-ui';
  8. import { API_BASE_URL, TOKEN_HEADER_NAME, LAYOUT_PATH, TENANT_HEADER_NAME } from '@/config/setting';
  9. import { getToken, setToken } from './token-util';
  10. import { getTenantId } from './tenant-util';
  11. import { logout } from './page-tab-util';
  12. import JSONBIG from 'json-bigint';
  13. const service = axios.create({
  14. baseURL: API_BASE_URL,
  15. transformResponse: [
  16. function (data) {
  17. if (data instanceof Blob) {
  18. return data;
  19. }
  20. const json = JSONBIG({
  21. storeAsString: true
  22. });
  23. const res = json.parse(data);
  24. return res;
  25. }
  26. ]
  27. });
  28. /**
  29. * 是否为签发登录凭证的入口(对应后端 TenantIsolationAspect 的 LOGIN_URIS)。
  30. * @param {string} url 请求地址
  31. * @returns {boolean} 是登录入口返回 true
  32. */
  33. function isLoginEntry(url) {
  34. return typeof url === 'string' && /(^|\/)user\/login(\?|$)/.test(url);
  35. }
  36. /**
  37. * 添加请求拦截器
  38. */
  39. service.interceptors.request.use(
  40. (config) => {
  41. // 添加 token 到 header
  42. // 登录入口必须以匿名身份发出:残留的旧凭证在服务端优先级高于请求头
  43. // (token 的租户声明才是权威),它既会顶掉本次选择的租户,又会让 X-Tenant-ID
  44. // 被当成外部伪造而忽略 —— 表现为"选了租户却登进平台库"。
  45. const token = isLoginEntry(config.url) ? '' : getToken();
  46. if (token && config.headers) {
  47. config.headers.common[TOKEN_HEADER_NAME] = token;
  48. }
  49. // 多租户:未登录的请求(登录接口、登录页品牌标识等匿名入口)租户只能靠
  50. // 请求头传递 —— 服务端必须在认证之前就知道连哪个租户库。
  51. // 已登录时租户已写入 token 声明、服务端以声明为准,这里不重复携带,
  52. // 避免为"多余且会被忽略"的头部逐个打告警日志。
  53. if (!token && config.headers) {
  54. const tenantId = getTenantId();
  55. if (tenantId) {
  56. config.headers.common[TENANT_HEADER_NAME] = tenantId;
  57. }
  58. }
  59. return config;
  60. },
  61. (error) => {
  62. return Promise.reject(error);
  63. }
  64. );
  65. /**
  66. * 添加响应拦截器
  67. */
  68. service.interceptors.response.use(
  69. (res) => {
  70. // token 自动续期
  71. if (res.data.code == '-1' && res.config?.showErrorToast !== false) {
  72. Message.error(res.data.message);
  73. }
  74. const token = res.headers[TOKEN_HEADER_NAME.toLowerCase()];
  75. if (token) {
  76. setToken(token);
  77. }
  78. return res;
  79. },
  80. (error) => {
  81. // 登录过期处理
  82. if (error?.response?.status === 401) {
  83. const currentPath = router.currentRoute.path;
  84. // if (currentPath === LAYOUT_PATH) {
  85. // logout(true);
  86. // } else {
  87. MessageBox.alert('登录状态已过期, 请退出重新登录!', '系统提示', {
  88. confirmButtonText: '重新登录',
  89. callback: (action) => {
  90. if (action === 'confirm') {
  91. logout(false, currentPath);
  92. }
  93. },
  94. beforeClose: () => {
  95. MessageBox.close();
  96. }
  97. });
  98. // }
  99. return Promise.reject(new Error(error.response.data?.message));
  100. }else if(!error?.response?.status){
  101. Message.error('服务调用失败,请联系管理员!');
  102. }
  103. return Promise.reject(error);
  104. }
  105. );
  106. export default service;