forked from Gitlink/forgeplus-react
76 lines
1.9 KiB
JavaScript
76 lines
1.9 KiB
JavaScript
import { notification } from 'antd';
|
|
import axios from 'axios';
|
|
import cookie from 'react-cookies';
|
|
|
|
|
|
// export const httpUrl = 'http://106.75.31.211:58088'; //可视化
|
|
// export const httpUrl = 'http://117.50.100.12:8008'; //测试环境
|
|
export const httpUrl = 'https://info.osredm.com/'; //生产环境
|
|
|
|
const TokenKey = 'autologin_forge_military';
|
|
axios.defaults.withCredentials = true;
|
|
|
|
// 创建axios实例
|
|
const service = axios.create({
|
|
baseURL: httpUrl,
|
|
timeout: 5000 // 请求超时时间
|
|
});
|
|
|
|
|
|
// request拦截器
|
|
service.interceptors.request.use(config => {
|
|
if (cookie.load(TokenKey)) {
|
|
config.headers['Authorization'] = cookie.load(TokenKey); // 让每个请求携带自定义token 请根据实际情况自行修改
|
|
}
|
|
return config;
|
|
}, error => {
|
|
// Do something with request error
|
|
console.log(error); // for debug
|
|
Promise.reject(error);
|
|
});
|
|
// respone拦截器
|
|
service.interceptors.response.use(
|
|
response => {
|
|
const res = response;
|
|
if (res.status === 400) {
|
|
notification.open({
|
|
message: "提示",
|
|
description: '请求错误',
|
|
});
|
|
return Promise.reject('error');
|
|
}
|
|
if (res.status === 401) {
|
|
notification.open({
|
|
message: "提示",
|
|
description: '未授权,请登录!',
|
|
});
|
|
return Promise.reject('error');
|
|
}
|
|
if (res.status === 40001) {
|
|
notification.open({
|
|
message: "提示",
|
|
description: '账户或密码错误!',
|
|
});
|
|
return Promise.reject('error');
|
|
}
|
|
if (response.status !== 200 && res.status !== 200) {
|
|
notification.open({
|
|
message: "提示",
|
|
description: res.message,
|
|
});
|
|
} else {
|
|
return response.data;
|
|
}
|
|
},
|
|
error => {
|
|
console.log(error);
|
|
notification.open({
|
|
message: "提示",
|
|
description: error.message,
|
|
});
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
export default service;
|