Showing
1 changed file
with
6 additions
and
9 deletions
| 1 | /** | 1 | /** |
| 2 | * custom http request | 2 | * custom http request |
| 3 | */ | 3 | */ |
| 4 | -function DefHttp() { | 4 | +function createAxios(options) { |
| 5 | /** | 5 | /** |
| 6 | * 创建实例 | 6 | * 创建实例 |
| 7 | */ | 7 | */ |
| 8 | const instance = axios.create({ | 8 | const instance = axios.create({ |
| 9 | - baseURL: DefHttp.baseURL, | ||
| 10 | - timeout: DefHttp.timeout, | 9 | + baseURL: options.baseURL, |
| 10 | + timeout: options.timeout, | ||
| 11 | headers: { | 11 | headers: { |
| 12 | "content-type": "application/json; charset=UTF-8", | 12 | "content-type": "application/json; charset=UTF-8", |
| 13 | - "X-Authorization": "Bearer " + DefHttp.token, | 13 | + "X-Authorization": "Bearer " + options.token, |
| 14 | }, | 14 | }, |
| 15 | }); | 15 | }); |
| 16 | /** | 16 | /** |
| @@ -18,17 +18,14 @@ function DefHttp() { | @@ -18,17 +18,14 @@ function DefHttp() { | ||
| 18 | */ | 18 | */ |
| 19 | instance.interceptors.response.use( | 19 | instance.interceptors.response.use( |
| 20 | function (response) { | 20 | function (response) { |
| 21 | - return response; | 21 | + return response.data; |
| 22 | }, | 22 | }, |
| 23 | function (error) { | 23 | function (error) { |
| 24 | if (error.response.status == 401) { | 24 | if (error.response.status == 401) { |
| 25 | - alert(error.response.data.message); | 25 | + layer.alert('登录超时,请重新登录'); |
| 26 | } | 26 | } |
| 27 | return Promise.reject(error); | 27 | return Promise.reject(error); |
| 28 | } | 28 | } |
| 29 | ); | 29 | ); |
| 30 | return instance; | 30 | return instance; |
| 31 | } | 31 | } |
| 32 | -DefHttp.timeout = 10 * 1000; | ||
| 33 | -DefHttp.token = ""; | ||
| 34 | -DefHttp.baseURL = "/api/"; |