init project
This commit is contained in:
37
src/utils/Network.ts
Normal file
37
src/utils/Network.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import axios from "axios";
|
||||
import { Response } from "~/types/Model";
|
||||
|
||||
axios.defaults.withCredentials = true;
|
||||
axios.interceptors.response.use((response: any) => {
|
||||
if (!response.config.responseType) {
|
||||
// 服务端返回
|
||||
const data = response.data as Response;
|
||||
if (data.code < 40000) {
|
||||
// 200 或 300 HTTP 状态段视为成功
|
||||
return data.data;
|
||||
} else {
|
||||
// 由调用方处理
|
||||
return Promise.reject(data.msg);
|
||||
}
|
||||
}
|
||||
return response.data;
|
||||
}, (error: any) => {
|
||||
// 请求错误
|
||||
if (error) {
|
||||
if (error.response && error.response.status) {
|
||||
throw error;
|
||||
}
|
||||
if (error.request) {
|
||||
if (error.message.startsWith("timeout")) {
|
||||
throw new Error("time out");
|
||||
} else {
|
||||
throw new Error(error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
});
|
||||
|
||||
export default {
|
||||
axios,
|
||||
};
|
||||
Reference in New Issue
Block a user