forgeplus-react/src/AppConfig.js

354 lines
10 KiB
JavaScript
Raw Normal View History

2020-03-16 14:12:11 +08:00
import axios from 'axios';
import md5 from 'md5';
import { requestProxy } from "./indexEduplus2RequestProxy";
2020-04-28 17:23:34 +08:00
import { broadcastChannelOnmessage, SetAppModel, isDev, queryString } from 'educoder';
import { notification } from 'antd';
2020-03-16 14:12:11 +08:00
import cookie from 'react-cookies';
import './index.css';
const $ = window.$;
2020-04-28 17:23:34 +08:00
const opens = "79e33abd4b6588941ab7622aed1e67e8";
2020-03-16 14:12:11 +08:00
let timestamp;
let checkSubmitFlg = false;
2020-04-28 17:23:34 +08:00
let message501 = false;
2020-03-16 14:12:11 +08:00
broadcastChannelOnmessage('refreshPage', () => {
2020-04-28 17:23:34 +08:00
window.location.reload()
2020-03-16 14:12:11 +08:00
})
2020-04-28 17:23:34 +08:00
function locationurl(list) {
2020-03-16 14:12:11 +08:00
if (window.location.port === "3007") {
} else {
2020-04-28 17:23:34 +08:00
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);
}
debugType = window.location.search.indexOf('debug=t') != -1 ? 'teacher' :
window.location.search.indexOf('debug=s') != -1 ? 'student' :
2020-03-25 17:08:27 +08:00
window.location.search.indexOf('debug=a') != -1 ? 'admin' : parsed.debug || 'admin'
2020-03-16 14:12:11 +08:00
}
// 超管
2020-04-28 17:23:34 +08:00
//debugType = "admin";
2020-03-16 14:12:11 +08:00
// 老师
2020-04-28 17:23:34 +08:00
// debugType="teacher";
2020-03-16 14:12:11 +08:00
// 学生
2020-04-28 17:23:34 +08:00
// debugType = "student";
2020-03-16 14:12:11 +08:00
function clearAllCookie() {
2020-04-28 17:23:34 +08:00
cookie.remove('_educoder_session', { path: '/' });
cookie.remove('autologin_trustie', { path: '/' });
2020-03-16 14:12:11 +08:00
setpostcookie()
}
clearAllCookie();
function setpostcookie() {
2020-04-28 17:23:34 +08:00
const str = window.location.pathname;
let newdomain = ".educoder.net"
if (str.indexOf("/wxcode") !== -1) {
2020-03-16 14:12:11 +08:00
console.log("123")
2020-04-28 17:23:34 +08:00
cookie.remove('_educoder_session', { path: '/' });
cookie.remove('autologin_trustie', { path: '/' });
2020-03-16 14:12:11 +08:00
const _params = window.location.search;
if (_params) {
let _search = _params.split('?')[1];
2020-04-28 17:23:34 +08:00
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: '/' });
2020-03-16 14:12:11 +08:00
}
}
}
setpostcookie();
2020-04-28 17:23:34 +08:00
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);
}
2020-03-16 14:12:11 +08:00
}
2020-04-28 17:23:34 +08:00
2020-03-16 14:12:11 +08:00
window._debugType = debugType;
export function initAxiosInterceptors(props) {
2020-04-28 17:23:34 +08:00
initOnlineOfflineListener()
2020-03-16 14:12:11 +08:00
2020-04-28 17:23:34 +08:00
// TODO 避免重复的请求 https://github.com/axios/axios#cancellation
// https://github.com/axios/axios/issues/1497
2020-03-16 14:12:11 +08:00
2020-04-28 17:23:34 +08:00
// TODO 读取到package.json中的配置
2020-04-29 16:02:48 +08:00
var proxy = "http://localhost:3000"
2020-04-28 17:23:34 +08:00
// proxy = "http://testbdweb.trustie.net"
// proxy = "http://testbdweb.educoder.net"
// proxy = "https://testeduplus2.educoder.net"
//proxy="http://47.96.87.25:48080"
2020-04-29 16:02:48 +08:00
proxy = "https://pre-newweb.educoder.net"
proxy = "https://testforgeplus.trustie.net/"
2020-04-28 17:23:34 +08:00
// proxy="https://test-jupyterweb.educoder.net"
// proxy="https://test-newweb.educoder.net"
// proxy="https://test-jupyterweb.educoder.net"
2020-03-16 14:12:11 +08:00
2020-04-20 15:37:01 +08:00
2020-04-28 17:23:34 +08:00
// 在这里使用requestMap控制避免用户通过双击等操作发出重复的请求
// 如果需要支持重复的请求考虑config里面自定义一个allowRepeat参考来控制
const requestMap = {};
window.setfalseInRequestMap = function (keyName) {
requestMap[keyName] = false;
}
2020-03-16 14:12:11 +08:00
2020-04-28 17:23:34 +08:00
//响应前的设置
axios.interceptors.request.use(
config => {
setpostcookie()
clearAllCookie()
// config.headers['Content-Type']= 'no-cache'
// if (token) { // 每次发送请求之前判断是否存在token如果存在则统一在http请求的header都加上token不用每次请求都手动添加了
// config.headers.Authorization = token;
// }
// --------------------------------------------- 測試3007连测试服的代码
// if (url.indexOf('file_update') != -1 || url.indexOf('game_build') != -1 || url.indexOf('game_status') != -1) {
// proxy = 'https://testbdweb.trustie.net'
// } else {
// proxy = 'http://localhost:3000'
// }
// ---------------------------------------------
// console.log("开始请求了");
// console.log(config.url);
// console.log(window.location.pathname);
//
// try {
// const str =window.location.pathname;
// if(str.indexOf("/wxcode") !== -1){
// // console.log("开始重写cookis");
// const _params = window.location.search;
// // console.log("1111");
// if (_params) {
// // console.log("22222");
// let _search = _params.split('?')[1];
// var _educoder_sessionmys="";
// var autologin_trusties="";
// _search.split('&').forEach(item => {
// const _arr = item.split('=');
// if(_arr[0]==='_educoder_session'){
// cookie.save('_educoder_session',_arr[1], { domain: '.educoder.net', path: '/'});
// _educoder_sessionmys=_arr[1];
// }else{
// cookie.save('autologin_trustie',_arr[1], { domain: '.educoder.net', path: '/'});
// autologin_trusties=_arr[1];
// }
// });
// try {
// const autlogins= `_educoder_session=${_educoder_sessionmys}; autologin_trustie=${autologin_trusties} `;
// config.params = {'Cookie': autlogins}
// config.headers['Cookie'] =autlogins;
// // console.log("设置了cookis");
// } catch (e) {
//
// }
// try {
// const autloginysls= `_educoder_session=${_educoder_sessionmys}; autologin_trustie=${autologin_trusties} `;
// config.params = {'autloginysls': autloginysls}
// config.headers['Cookie'] =autloginysls;
// // console.log("设置了cookis");
// }catch (e) {
//
// }
// }
// }
// }catch (e) {
//
// }
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}`;
2020-03-16 14:12:11 +08:00
}
2020-04-28 17:23:34 +08:00
}
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}`;
}
2020-04-29 16:02:48 +08:00
}else{
config.url=url;
2020-04-28 17:23:34 +08:00
}
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) {
// console.error('error:', response.data.message)
// throw new Error()
// https://github.com/axios/axios/issues?utf8=%E2%9C%93&q=cancel+request+in+response+interceptors+
// https://github.com/axios/axios/issues/583
// message.info(response.data.message || '服务端返回status -1请联系管理员。');
// props.showSnackbar( response.data.message || '服务器异常,请联系管理员。' )
if (window.location.pathname.startsWith('/tasks/')) {
props.showSnackbar(response.data.message || '服务器异常,请联系管理员。')
} else {
notification.open({
message: "提示",
description: response.data.message || '服务器异常,请联系管理员。',
style: {
zIndex: 99999999
},
});
// notification['error']({
// message:"提示",
// description: response.data.message || '服务器异常,请联系管理员。',
// });
}
2020-03-16 14:12:11 +08:00
2020-04-28 17:23:34 +08:00
throw new axios.Cancel('Operation canceled by the user.');
} else {
// hash跳转
// var hash = window.location.hash;
// if (hash) {
// hashTimeout && window.clearTimeout(hashTimeout)
// hashTimeout = setTimeout(() => {
// var element = document.querySelector(hash);
// if (element) {
// element.scrollIntoView();
// }
// }, 400)
// }
2020-04-20 15:37:01 +08:00
}
2020-04-28 17:23:34 +08:00
// if(response.data.status === 401){
// console.log("401401401")
// }
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);
}
2020-04-28 17:23:34 +08:00
2020-04-20 15:37:01 +08:00
requestMap[response.config.url] = false;
setpostcookie();
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
}