✨ Feat: [前台]:请求超时时间设置为15s
This commit is contained in:
parent
34d1b79d5c
commit
1ea496fcba
@ -1,60 +1,60 @@
|
|||||||
import { notification } from "antd";
|
import { notification } from "antd";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
//定义http实例
|
//定义http实例
|
||||||
const instance = axios.create({
|
const instance = axios.create({
|
||||||
baseURL: "/bookmark/api",
|
baseURL: "/bookmark/api",
|
||||||
timeout: 5000
|
timeout: 15000
|
||||||
});
|
});
|
||||||
|
|
||||||
//实例添加请求拦截器
|
//实例添加请求拦截器
|
||||||
instance.interceptors.request.use(
|
instance.interceptors.request.use(
|
||||||
req => {
|
req => {
|
||||||
req.headers["jwt-token"] = window.token;
|
req.headers["jwt-token"] = window.token;
|
||||||
return req;
|
return req;
|
||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
//实例添加响应拦截器
|
//实例添加响应拦截器
|
||||||
instance.interceptors.response.use(
|
instance.interceptors.response.use(
|
||||||
function(res) {
|
function(res) {
|
||||||
const data = res.data;
|
const data = res.data;
|
||||||
if (data.code === 1) {
|
if (data.code === 1) {
|
||||||
return data.data;
|
return data.data;
|
||||||
} else if (data.code === -2) {
|
} else if (data.code === -2) {
|
||||||
return Promise.reject(data.message);
|
return Promise.reject(data.message);
|
||||||
} else {
|
} else {
|
||||||
showError(data);
|
showError(data);
|
||||||
return Promise.reject(data.message);
|
return Promise.reject(data.message);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
function(error) {
|
function(error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
showError(error.response);
|
showError(error.response);
|
||||||
return Promise.reject(JSON.stringify(error));
|
return Promise.reject(JSON.stringify(error));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
function showError(response) {
|
function showError(response) {
|
||||||
let description,
|
let description,
|
||||||
message = "出问题啦";
|
message = "出问题啦";
|
||||||
if (response) {
|
if (response) {
|
||||||
description = response.message;
|
description = response.message;
|
||||||
if (response.code === -1) {
|
if (response.code === -1) {
|
||||||
let redirect = encodeURIComponent(window.location.pathname + window.location.search);
|
let redirect = encodeURIComponent(window.location.pathname + window.location.search);
|
||||||
window.reactHistory.replace("/public/login?redirect=" + redirect);
|
window.reactHistory.replace("/public/login?redirect=" + redirect);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
description = "无网络连接";
|
description = "无网络连接";
|
||||||
}
|
}
|
||||||
notification.open({
|
notification.open({
|
||||||
message,
|
message,
|
||||||
description,
|
description,
|
||||||
duration: 2
|
duration: 2
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export default instance;
|
export default instance;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user