forgeplus-react/src/AppConfig.js

139 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-03-16 14:12:11 +08:00
import axios from 'axios';
import { requestProxy } from "./indexEduplus2RequestProxy";
2020-09-29 11:21:00 +08:00
import { broadcastChannelOnmessage, isDev, queryString } from 'educoder';
2020-04-28 17:23:34 +08:00
import { notification } from 'antd';
2020-03-16 14:12:11 +08:00
import './index.css';
2021-01-05 14:27:35 +08:00
let message501 = false;
2020-03-16 14:12:11 +08:00
broadcastChannelOnmessage('refreshPage', () => {
2021-01-05 14:27:35 +08:00
window.location.reload();
2020-03-16 14:12:11 +08:00
})
2020-04-28 17:23:34 +08:00
function locationurl(list) {
if (window.location.port !== "3007") {
window.location.href = list
2020-03-16 14:12:11 +08:00
}
}
// TODO 开发期多个身份切换
2020-04-28 17:23:34 +08:00
let debugType = ""
2020-03-16 14:12:11 +08:00
if (isDev) {
const _search = window.location.search;
let parsed = {};
if (_search) {
parsed = queryString.parse(_search);
}
2020-05-11 14:18:32 +08:00
debugType = window.location.search.indexOf('debug=t') !== -1 ? 'teacher' :
window.location.search.indexOf('debug=s') !== -1 ? 'student' :
window.location.search.indexOf('debug=a') !== -1 ? 'admin' : parsed.debug || 'admin'
2020-11-24 18:00:34 +08:00
}
2020-03-16 14:12:11 +08:00
window._debugType = debugType;
export function initAxiosInterceptors(props) {
2021-04-01 19:03:56 +08:00
// 判断网络是否连接
2021-01-05 14:27:35 +08:00
initOnlineOfflineListener();
2020-04-20 15:37:01 +08:00
2021-05-14 14:37:59 +08:00
var proxy = "http://192.168.1.37:3000";
2020-04-28 17:23:34 +08:00
//响应前的设置
axios.interceptors.request.use(
config => {
2021-04-02 14:08:59 +08:00
if(config.url.indexOf("http") !== -1) {
2020-04-28 17:23:34 +08:00
return config
}
requestProxy(config);
2020-04-28 17:23:34 +08:00
let url = `/api${config.url}`;
2020-06-18 11:36:05 +08:00
if (`${config[0]}` !== `true`) {
2020-04-28 17:23:34 +08:00
if (window.location.port === "3007") {
config.url = `${proxy}${url}`;
2020-06-18 11:36:05 +08:00
if (config.url.indexOf('?') === -1) {
2020-04-28 17:23:34 +08:00
config.url = `${config.url}?debug=${debugType}`;
} else {
config.url = `${config.url}&debug=${debugType}`;
}
2020-05-11 14:18:32 +08:00
} else {
config.url = url;
2020-04-28 17:23:34 +08:00
}
}
return config;
},
err => {
return Promise.reject(err);
2021-05-08 14:24:02 +08:00
});
2020-04-28 17:23:34 +08:00
axios.interceptors.response.use(function (response) {
if (response === undefined) {
return
}
2020-09-29 11:21:00 +08:00
const config = response.config;
2020-04-28 17:23:34 +08:00
if (response.data.status === -1) {
if (window.location.pathname.startsWith('/tasks/')) {
props.showSnackbar(response.data.message || '服务器异常,请联系管理员。')
} else {
notification.open({
message: "提示",
description: response.data.message || '服务器异常,请联系管理员。',
style: {
zIndex: 99999999
},
});
}
throw new axios.Cancel('Operation canceled by the user.');
2020-04-20 15:37:01 +08:00
}
2020-05-11 14:18:32 +08:00
2020-04-28 17:23:34 +08:00
if (response.data.status === 403 || response.data.status === "403") {
2020-04-20 15:37:01 +08:00
locationurl('/403');
}
if (response.data.status === 404) {
2020-04-28 17:23:34 +08:00
locationurl('/nopage');
2020-04-20 15:37:01 +08:00
}
if (response.data.status === 500) {
2020-04-28 17:23:34 +08:00
locationurl('/500');
2020-04-20 15:37:01 +08:00
}
2020-03-16 14:12:11 +08:00
2020-04-20 15:37:01 +08:00
if (response.data.status === 501) {
2020-04-28 17:23:34 +08:00
if (message501 === false) {
message501 = true
2020-04-20 15:37:01 +08:00
notification.open({
2020-04-28 17:23:34 +08:00
message: "提示",
description: response.data.message || '访问异常,请求不合理',
2020-04-20 15:37:01 +08:00
style: {
zIndex: 99999999
}
})
}
window.setTimeout(function () {
2020-04-28 17:23:34 +08:00
message501 = false
2020-04-20 15:37:01 +08:00
}, 2000);
}
return response;
2020-04-28 17:23:34 +08:00
}, function (error) {
return Promise.reject(error);
});
2020-03-16 14:12:11 +08:00
}
function initOnlineOfflineListener() {
2020-04-28 17:23:34 +08:00
const $ = window.$
$(window).bind("online", () => {
notification.destroy()
notification.success({
duration: 2,
message: '网络恢复正常',
description:
'网络恢复正常,感谢使用。',
})
});
$(window).bind("offline", () => {
notification.destroy()
notification.warning({
duration: null,
message: '网络异常',
description:
'网络异常,请检测网络后重试。',
})
});
2020-03-16 14:12:11 +08:00
}