1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' type ResponseType = 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'
interface AxiosRequest { baseURL?: string; url: string; data?: any; params?: any; method?: Method; headers?: any; timeout?: number; responseType?: ResponseType; }
interface CustomResponse<T> { readonly status: boolean; readonly message: string; data: T; origin?: any; }
import axios, { AxiosRequestConfig } from 'axios';
interface PendingType { url?: string; method?: Method; params: any; data: any; cancel: Function; }
const pending: Array<PendingType> = []; const CancelToken = axios.CancelToken;
const instance = axios.create({ timeout: 10000, responseType: 'json' });
const removePending = (config: AxiosRequestConfig) => { for (const key in pending) { const item: number = +key; const list: PendingType = pending[key]; if (list.url === config.url && list.method === config.method && JSON.stringify(list.params) === JSON.stringify(config.params) && JSON.stringify(list.data) === JSON.stringify(config.data)) { list.cancel('操作太频繁,请稍后再试'); pending.splice(item, 1); } } };
instance.interceptors.request.use( (request:any) => {
removePending(request); request.cancelToken = new CancelToken((c) => { pending.push({ url: request.url, method: request.method, params: request.params, data: request.data, cancel: c }); }); return request; }, (error: any) => { return Promise.reject(error); } );
instance.interceptors.response.use( (response: any) => { removePending(response.config);
const errorCode = response?.data?.errorCode; switch (errorCode) { case '401': break; default: break; }
return response; }, (error: any) => { const response = error.response;
switch (response?.status) { case 401: break; case 403: break; case 500: break; case 503: break; default: break; }
return Promise.reject(response || {message: error.message}); } );
class BaseHttp { protected baseURL: string = process.env.VUE_APP_BaseURL as string; protected headers: object = { ContentType: 'application/json;charset=UTF-8' }
private apiAxios<T>({ baseURL = this.baseURL, headers = this.headers, method, url, data, params, responseType }: AxiosRequest): Promise<CustomResponse<T>> {
return new Promise((resolve, reject) => { instance({ baseURL, headers, method, url, params, data, responseType }).then((res: any) => { if (res.status === 200) { } else { resolve({ status: false, message: res.data?.errorMessage || (url + '请求失败'), data: null }); } }).catch((err: any) => { const message = err?.data?.errorMessage || err?.message || (url + '请求失败'); reject({ status: false, message, data: null}); }); }); }
protected getReq<T>({ baseURL, headers, url, data, params, responseType }: AxiosRequest) { return this.apiAxios<T>({ baseURL, headers, method: 'GET', url, data, params, responseType }); }
protected postReq<T>({ baseURL, headers, url, data, params, responseType }: AxiosRequest) { return this.apiAxios<T>({ baseURL, headers, method: 'POST', url, data, params, responseType }); }
protected putReq<T>({ baseURL, headers, url, data, params, responseType }: AxiosRequest) { return this.apiAxios<T>({ baseURL, headers, method: 'PUT', url, data, params, responseType }); }
protected deleteReq<T>({ baseURL, headers, url, data, params, responseType }: AxiosRequest) { return this.apiAxios<T>({ baseURL, headers, method: 'DELETE', url, data, params, responseType }); } }
|