request.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 { BaseLoginUrl, DA_HUA_URL_PREFIX } from '@/constants/constants';
  6. import { useDaHuaStore } from '@/store';
  7. import { LOGIN_URL, BASE_URL } from '@/constants/constants';
  8. // const baseURL =
  9. // process.env.NODE_ENV === 'production'
  10. // ? 'http://sqpcbg.xt.wenhq.top:8083/api'
  11. // : '/api';
  12. const baseURL =
  13. process.env.NODE_ENV === 'production'
  14. ? 'http://61.147.254.211:30876/YJZH-SQ/api'
  15. : '/api';
  16. const CancelToken = axios.CancelToken;
  17. const source = CancelToken.source();
  18. let timer: NodeJS.Timeout;
  19. axios.interceptors.request.use((config) => {
  20. const main = useMainStore();
  21. config.cancelToken = new axios.CancelToken((cancel) => {
  22. main.pushReqToken(cancel);
  23. });
  24. return config;
  25. });
  26. axios.interceptors.response.use(
  27. (res) => {
  28. if (res.status >= 200 && res.status < 300) {
  29. if (res.data?.code === 200) {
  30. return res.data;
  31. }
  32. if (res.data?.code === 401) {
  33. ElMessage.error({
  34. // message: `401. 没有权限访问该接口: ${res.config.url}`,
  35. message: `401. 没有权限访问该接口`,
  36. });
  37. if (res.data.data.indexOf('Jwt expired') != -1) {
  38. debugger
  39. window.localStorage.removeItem('userdata');
  40. window.location.href = LOGIN_URL;
  41. }
  42. // const main = useMainStore();
  43. // main.clearReqToken();
  44. // window.localStorage.setItem('Authorization', '');
  45. // window.location.href = BaseLoginUrl;
  46. // throw Error(res.statusText);
  47. }
  48. if (res.data?.code === 404) {
  49. ElMessage.error({ message: '404. 未找到该接口!' });
  50. throw Error(res.statusText);
  51. }
  52. if (res.data?.code >= 500) {
  53. ElMessage.error({ message: res.data?.msg ?? '系统异常, 请稍后重试!' });
  54. throw Error(res.statusText);
  55. }
  56. }
  57. if (res.status === 401) {
  58. ElMessage.error({
  59. message: `401. 没有权限访问该接口: ${res.config.url}`,
  60. });
  61. throw Error(res.statusText);
  62. }
  63. if (res.status === 404) {
  64. ElMessage.error({ message: '404. 未找到该接口!' });
  65. throw Error(res.statusText);
  66. }
  67. if (res.status >= 500) {
  68. if (res.data?.message == 'invalid ticket') {
  69. debugger
  70. window.localStorage.removeItem('userdata');
  71. window.location.href = LOGIN_URL;
  72. }
  73. ElMessage.error({ message: '系统异常, 请稍后重试!' });
  74. throw Error(res.statusText);
  75. }
  76. return res.data;
  77. },
  78. (error) => {
  79. // ElMessage.error({ message: '系统异常, 请稍后重试!' });
  80. const {
  81. status,
  82. config: { url },
  83. } = error.response;
  84. if (status === 401 && url?.includes(DA_HUA_URL_PREFIX)) {
  85. clearTimeout(timer);
  86. const dahuaStore = useDaHuaStore();
  87. dahuaStore.timer && clearInterval(dahuaStore.timer);
  88. timer = setTimeout(() => {
  89. dahuaStore.DAHUALogin();
  90. }, 1000);
  91. }
  92. throw Error(error);
  93. },
  94. );
  95. export default function request<T>(
  96. method: AxiosRequestConfig['method'] = 'GET',
  97. confifg: AxiosRequestConfig & {},
  98. ): Promise<T> {
  99. var tokenkjson = window.localStorage.getItem('userdata');
  100. tokenkjson = tokenkjson == null ? '{}' : tokenkjson;
  101. tokenkjson = tokenkjson == undefined ? '{}' : tokenkjson;
  102. tokenkjson = JSON.parse(tokenkjson);
  103. return axios.request({
  104. method,
  105. baseURL,
  106. ...confifg,
  107. headers: {
  108. Accept: 'application/json',
  109. 'Content-Type': 'application/json',
  110. AppId: '3bcb760743ea456faba29a1dfb247bf4',
  111. Authorization: 'Bearer ' + tokenkjson['accessToken'],
  112. ...confifg.headers,
  113. },
  114. });
  115. }