request.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import axios, { AxiosRequestConfig } from 'axios';
  2. import qs from 'querystring';
  3. import { ElMessage } from 'element-plus';
  4. import useMainStore from '@/store/useMainStore';
  5. import { LOGIN_URL,BASE_URL} from "./constant"
  6. import { indexOf } from 'lodash';
  7. const baseURL =
  8. process.env.NODE_ENV === 'prod' || process.env.NODE_ENV === 'test'
  9. ? (import.meta.env.VITE_API_SERVER as string) + '/YJZH-SQ/api'
  10. : '/api';
  11. const CancelToken = axios.CancelToken;
  12. const source = CancelToken.source();
  13. axios.interceptors.request.use((config) => {
  14. const main = useMainStore();
  15. config.cancelToken = new axios.CancelToken((cancel) => {
  16. main.pushReqToken(cancel);
  17. });
  18. return config;
  19. });
  20. axios.interceptors.response.use(
  21. (res) => {
  22. if (res.status >= 200 && res.status < 300) {
  23. if (res.data?.code === 200) {
  24. return res.data;
  25. }
  26. if (res.data?.code === 401) {
  27. ElMessage.error({
  28. // message: `401. 没有权限访问该接口: ${res.config.url}`,
  29. message: `401. 没有权限访问该接口`,
  30. });
  31. if (res.data.data.indexOf('Jwt expired') != -1) {
  32. window.localStorage.removeItem('userdata');
  33. window.location.href = LOGIN_URL;
  34. }
  35. throw Error(res.statusText);
  36. }
  37. if (res.data?.code === 404) {
  38. ElMessage.error({ message: '404. 未找到该接口!' });
  39. throw Error(res.statusText);
  40. }
  41. if (res.data?.code >= 500) {
  42. if (res.data?.message == 'invalid ticket') {
  43. window.localStorage.removeItem('userdata');
  44. window.location.href = LOGIN_URL;
  45. }
  46. ElMessage.error({
  47. message: res.data?.msg ?? '系统异常, 请稍后重试!',
  48. });
  49. throw Error(res.data.msg);
  50. }
  51. }
  52. if (res.status === 401) {
  53. ElMessage.error({
  54. message: `401. 没有权限访问该接口: ${res.config.url}`,
  55. });
  56. throw Error(res.statusText);
  57. }
  58. if (res.status === 404) {
  59. ElMessage.error({ message: '404. 未找到该接口!' });
  60. throw Error(res.statusText);
  61. }
  62. if (res.status >= 500) {
  63. ElMessage.error({ message: '系统异常, 请稍后重试!' });
  64. throw Error(res.statusText);
  65. }
  66. },
  67. (error) => {
  68. // ElMessage.error({ message: '系统异常, 请稍后重试!' });
  69. // throw Error(error);
  70. },
  71. );
  72. export default function request<T>(
  73. method: AxiosRequestConfig['method'] = 'GET',
  74. confifg: AxiosRequestConfig & {},
  75. ): Promise<T> {
  76. var tokenkjson = window.localStorage.getItem('userdata');
  77. tokenkjson = tokenkjson == null ? "{}" : tokenkjson;
  78. tokenkjson = tokenkjson == undefined ? "{}" : tokenkjson;
  79. tokenkjson = JSON.parse(tokenkjson);
  80. return axios.request({
  81. method,
  82. baseURL,
  83. ...confifg,
  84. headers: {
  85. Accept: 'application/json',
  86. AppId: '3bcb760743ea456faba29a1dfb247bf4',
  87. Authorization: 'Bearer ' + (tokenkjson["accessToken"]),
  88. 'Content-Type': 'application/json',
  89. ...confifg.headers,
  90. },
  91. });
  92. }