request.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import axios from 'axios';
  2. import router from '@/router';
  3. import { useUserStore } from '@/stores/modules/userStore';
  4. import { createDiscreteApi } from 'naive-ui';
  5. import type { Result } from '@/types/data';
  6. import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig, AxiosError } from 'axios';
  7. import type { NotificationApi } from "naive-ui";
  8. const { notification } = createDiscreteApi(["notification"]);
  9. // const useStore = useUserStore();
  10. export const url = import.meta.env.VITE_BASE_URL;
  11. export const prefix = import.meta.env.VITE_BASE_PREFIX;
  12. export const baseURL = url + prefix;
  13. enum errorCode {
  14. '请求错误' = 400,
  15. '未授权,请重新登录' = 401,
  16. '拒绝访问' = 403,
  17. '请求出错' = 404,
  18. '请求超时' = 408,
  19. '服务器错误' = 500,
  20. '服务未实现' = 501,
  21. '网络错误' = 502,
  22. '服务不可用' = 503,
  23. '网络超时' = 504,
  24. 'HTTP版本不受支持' = 505
  25. }
  26. const showNotification = (type: keyof NotificationApi = 'error', meta: string) => {
  27. notification[type]({
  28. content: '提示',
  29. meta,
  30. duration: 3 * 1000,
  31. keepAliveOnHover: true
  32. })
  33. }
  34. export class Request {
  35. private instance: AxiosInstance;
  36. private baseConfig: AxiosRequestConfig = { baseURL, timeout: 30 * 1000 };
  37. constructor(config: AxiosRequestConfig = {}) {
  38. this.instance = axios.create({ ...this.baseConfig, ...config });
  39. this.instance.interceptors.request.use((config: InternalAxiosRequestConfig<Result>) => {
  40. // const { token } = useStore.userInfo;
  41. /**
  42. * 环境区分 - 仅限于测试环境使用
  43. * */
  44. // config.headers.port = localStorage.getItem("ENV");
  45. // token && (config.headers.Authorization = 'Bearer ' + token);
  46. return config;
  47. }, (err: any) => {
  48. return Promise.reject(err)
  49. });
  50. this.instance.interceptors.response.use(res => {
  51. if ( res.config.onDownloadProgress ) return res;
  52. const { code } = res.data;
  53. if ( code == 200 ) {
  54. return res.data;
  55. }
  56. if ( code == 401 ) {
  57. showNotification('error', 'Token已失效')
  58. router.push("/login");
  59. }
  60. return Promise.reject(res.data);
  61. }, (error: AxiosError) => {
  62. if (error.code === 'ECONNABORTED' ) {
  63. showNotification('warning', '链接超时,请刷新后重试')
  64. }
  65. return Promise.reject(error);
  66. })
  67. }
  68. public request(config: AxiosRequestConfig): Promise<AxiosResponse> {
  69. return this.instance.request(config);
  70. }
  71. public get<T = any>(
  72. url: string,
  73. config?: AxiosRequestConfig
  74. ): Promise<Result<T>> {
  75. return this.instance.get(url, config);
  76. }
  77. public post<T = any>(
  78. url: string,
  79. data?: any,
  80. config?: AxiosRequestConfig
  81. ): Promise<Result<T>> {
  82. return this.instance.post(url, data, config);
  83. }
  84. public put<T = any>(
  85. url: string,
  86. data?: any,
  87. config?: AxiosRequestConfig
  88. ): Promise<Result<T>> {
  89. return this.instance.put(url, data, config);
  90. }
  91. public delete<T = any>(
  92. url: string,
  93. config?: AxiosRequestConfig
  94. ): Promise<Result<T>> {
  95. return this.instance.delete(url, config);
  96. }
  97. }
  98. export const streamHttp = new Request({
  99. baseURL,
  100. timeout: 3 * 60 * 1000
  101. })
  102. export default new Request({
  103. baseURL,
  104. timeout: 3 * 60 * 1000
  105. });