index.ts 724 B

1234567891011121314151617181920212223242526
  1. import { message } from 'ant-design-vue';
  2. import HttpService from './axios';
  3. import { AxiosRequestConfig } from 'axios';
  4. const baseURL = (import.meta as ImportMeta & { env: any}).env.VITE_APP_BASE_URL as string;
  5. type ResponseResult<T> = {
  6. code: number;
  7. isAuthorized: boolean;
  8. isSuccess: boolean;
  9. result: T;
  10. error?: string;
  11. }
  12. const transformResponse = <T>(response: any & ResponseResult<T>, config: AxiosRequestConfig) => {
  13. if (config.responseType === 'blob') {
  14. return response;
  15. }
  16. if(response.code === 1) {
  17. return response.result;
  18. } else {
  19. message.warning(response.error as string);
  20. throw new Error(response.error)
  21. }
  22. };
  23. export const http = new HttpService(baseURL, {transformResponse});