forked from Gitlink/build
238 lines
6.4 KiB
JavaScript
238 lines
6.4 KiB
JavaScript
import axios from 'axios';
|
||
import { requestProxy } from "./indexEduplus2RequestProxy";
|
||
import { broadcastChannelOnmessage , isDev, queryString } from 'educoder';
|
||
import { notification } from 'antd';
|
||
import cookie from 'react-cookies';
|
||
import './index.css';
|
||
const $ = window.$;
|
||
let timestamp;
|
||
let checkSubmitFlg = false;
|
||
let message501 = false;
|
||
|
||
broadcastChannelOnmessage('refreshPage', () => {
|
||
window.location.reload()
|
||
})
|
||
|
||
function locationurl(list) {
|
||
if (window.location.port === "3007") {
|
||
|
||
} else {
|
||
window.location.href = list
|
||
}
|
||
}
|
||
// TODO 开发期多个身份切换
|
||
let debugType = ""
|
||
if (isDev) {
|
||
const _search = window.location.search;
|
||
let parsed = {};
|
||
if (_search) {
|
||
parsed = queryString.parse(_search);
|
||
}
|
||
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'
|
||
}
|
||
function clearAllCookie() {
|
||
cookie.remove('_educoder_session', { path: '/' });
|
||
cookie.remove('autologin_trustie', { path: '/' });
|
||
setpostcookie()
|
||
}
|
||
clearAllCookie();
|
||
function setpostcookie() {
|
||
|
||
const str = window.location.pathname;
|
||
let newdomain = ".educoder.net"
|
||
if (str.indexOf("/wxcode") !== -1) {
|
||
console.log("123")
|
||
cookie.remove('_educoder_session', { path: '/' });
|
||
cookie.remove('autologin_trustie', { path: '/' });
|
||
const _params = window.location.search;
|
||
if (_params) {
|
||
let _search = _params.split('?')[1];
|
||
let _educoder_sessions = _search.split('&')[0].split('=');
|
||
cookie.save('_educoder_session', _educoder_sessions[1], { domain: '.educoder.net', path: '/' });
|
||
let autologin_trusties = _search.split('&')[1].split('=');
|
||
cookie.save('autologin_trustie', autologin_trusties[1], { domain: '.educoder.net', path: '/' });
|
||
|
||
}
|
||
}
|
||
}
|
||
setpostcookie();
|
||
|
||
|
||
function railsgettimes(proxy) {
|
||
clearAllCookie()
|
||
if (timestamp && checkSubmitFlg === false) {
|
||
$.ajax({
|
||
url: proxy, async: true, success: function (data) {
|
||
if (data.status === 0) {
|
||
timestamp = data.message;
|
||
setpostcookie();
|
||
}
|
||
}
|
||
})
|
||
checkSubmitFlg = true
|
||
window.setTimeout(() => {
|
||
checkSubmitFlg = false;
|
||
}, 2000);
|
||
} else if (checkSubmitFlg === false) {
|
||
$.ajax({
|
||
url: proxy, async: true, success: function (data) {
|
||
if (data.status === 0) {
|
||
timestamp = data.message;
|
||
setpostcookie();
|
||
}
|
||
}
|
||
})
|
||
checkSubmitFlg = true
|
||
window.setTimeout(() => {
|
||
checkSubmitFlg = false;
|
||
}, 2000);
|
||
}
|
||
|
||
}
|
||
|
||
window._debugType = debugType;
|
||
export function initAxiosInterceptors(props) {
|
||
initOnlineOfflineListener()
|
||
|
||
// TODO 避免重复的请求 https://github.com/axios/axios#cancellation
|
||
// TODO 读取到package.json中的配置?
|
||
var proxy = "http://localhost:3000"
|
||
// proxy = "https://pre-newweb.educoder.net"
|
||
// proxy = "https://testforgeplus.trustie.net/"
|
||
|
||
// 在这里使用requestMap控制,避免用户通过双击等操作发出重复的请求;
|
||
// 如果需要支持重复的请求,考虑config里面自定义一个allowRepeat参考来控制
|
||
const requestMap = {};
|
||
window.setfalseInRequestMap = function (keyName) {
|
||
requestMap[keyName] = false;
|
||
}
|
||
//响应前的设置
|
||
axios.interceptors.request.use(
|
||
config => {
|
||
setpostcookie()
|
||
clearAllCookie()
|
||
|
||
if (config.url.indexOf(proxy) != -1 || config.url.indexOf(':') != -1) {
|
||
return config
|
||
}
|
||
requestProxy(config)
|
||
|
||
let url = `/api${config.url}`;
|
||
|
||
//qq登录去掉api
|
||
if (config.params && config.params.redirect_uri != undefined) {
|
||
if (config.params.redirect_uri.indexOf('otherloginqq') != -1) {
|
||
url = `${config.url}`;
|
||
}
|
||
}
|
||
if (`${config[0]}` != `true`) {
|
||
if (window.location.port === "3007") {
|
||
config.url = `${proxy}${url}`;
|
||
if (config.url.indexOf('?') == -1) {
|
||
config.url = `${config.url}?debug=${debugType}`;
|
||
} else {
|
||
config.url = `${config.url}&debug=${debugType}`;
|
||
}
|
||
} else {
|
||
config.url = url;
|
||
}
|
||
setpostcookie();
|
||
}
|
||
if (config.url.indexOf('update_file') === -1) {
|
||
requestMap[config.url] = true;
|
||
|
||
window.setTimeout("setfalseInRequestMap('" + config.url + "')", 900)
|
||
}
|
||
return config;
|
||
},
|
||
err => {
|
||
return Promise.reject(err);
|
||
});
|
||
|
||
axios.interceptors.response.use(function (response) {
|
||
|
||
// console.log(".............")
|
||
if (response === undefined) {
|
||
return
|
||
}
|
||
const config = response.config
|
||
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.');
|
||
}
|
||
|
||
if (response.data.status === 403 || response.data.status === "403") {
|
||
|
||
locationurl('/403');
|
||
}
|
||
|
||
if (response.data.status === 404) {
|
||
locationurl('/nopage');
|
||
}
|
||
|
||
if (response.data.status === 500) {
|
||
locationurl('/500');
|
||
}
|
||
|
||
if (response.data.status === 501) {
|
||
if (message501 === false) {
|
||
message501 = true
|
||
notification.open({
|
||
message: "提示",
|
||
description: response.data.message || '访问异常,请求不合理',
|
||
style: {
|
||
zIndex: 99999999
|
||
}
|
||
})
|
||
}
|
||
window.setTimeout(function () {
|
||
message501 = false
|
||
}, 2000);
|
||
}
|
||
|
||
|
||
requestMap[response.config.url] = false;
|
||
setpostcookie();
|
||
return response;
|
||
}, function (error) {
|
||
return Promise.reject(error);
|
||
});
|
||
}
|
||
|
||
|
||
function initOnlineOfflineListener() {
|
||
const $ = window.$
|
||
$(window).bind("online", () => {
|
||
notification.destroy()
|
||
notification.success({
|
||
duration: 2,
|
||
message: '网络恢复正常',
|
||
description:
|
||
'网络恢复正常,感谢使用。',
|
||
})
|
||
});
|
||
$(window).bind("offline", () => {
|
||
notification.destroy()
|
||
|
||
notification.warning({
|
||
duration: null,
|
||
message: '网络异常',
|
||
description:
|
||
'网络异常,请检测网络后重试。',
|
||
})
|
||
});
|
||
}
|