|
|
import React from "react";
|
|
|
|
|
|
import axios from 'axios';
|
|
|
import { requestProxy } from "./indexEduplus2RequestProxy";
|
|
|
import { broadcastChannelOnmessage } from 'educoder'
|
|
|
import { notification } from 'antd';
|
|
|
broadcastChannelOnmessage('refreshPage', () => {
|
|
|
window.location.reload()
|
|
|
})
|
|
|
|
|
|
function locationurl(list){
|
|
|
if (window.location.port === "3007") {
|
|
|
|
|
|
} else {
|
|
|
window.location.replace(list)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// TODO 开发期多个身份切换
|
|
|
const debugType = window.location.search.indexOf('debug=t') != -1 ? 'teacher' :
|
|
|
window.location.search.indexOf('debug=s') != -1 ? 'student' : 'admin'
|
|
|
window._debugType = debugType;
|
|
|
export function initAxiosInterceptors(props) {
|
|
|
|
|
|
// TODO 避免重复的请求 https://github.com/axios/axios#cancellation
|
|
|
// https://github.com/axios/axios/issues/1497
|
|
|
|
|
|
// TODO 读取到package.json中的配置?
|
|
|
var proxy = "http://localhost:3000"
|
|
|
// proxy = "http://testbdweb.trustie.net"
|
|
|
// proxy = "http://testbdweb.educoder.net"
|
|
|
proxy = "https://testeduplus2.educoder.net"
|
|
|
|
|
|
|
|
|
// 在这里使用requestMap控制,避免用户通过双击等操作发出重复的请求;
|
|
|
// 如果需要支持重复的请求,考虑config里面自定义一个allowRepeat参考来控制
|
|
|
const requestMap = {};
|
|
|
|
|
|
window.setfalseInRequestMap = function(keyName) {
|
|
|
requestMap[keyName] = false;
|
|
|
}
|
|
|
axios.interceptors.request.use(
|
|
|
config => {
|
|
|
|
|
|
// 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'
|
|
|
// }
|
|
|
// ---------------------------------------------
|
|
|
if (config.url.indexOf(proxy) != -1) {
|
|
|
return config
|
|
|
}
|
|
|
requestProxy(config)
|
|
|
|
|
|
var url = `/api${config.url}`;
|
|
|
|
|
|
if(`${config[0]}`!=`true`){
|
|
|
if (window.location.port === "3007") {
|
|
|
if (url.indexOf('.json') == -1) {
|
|
|
|
|
|
alert('开发提示:请给接口加.json:' + url)
|
|
|
|
|
|
}
|
|
|
config.url = `${proxy}${url}`;
|
|
|
if (config.url.indexOf('?') == -1) {
|
|
|
config.url = `${config.url}?debug=${debugType}`
|
|
|
} else {
|
|
|
config.url = `${config.url}&debug=${debugType}`
|
|
|
}
|
|
|
} else {
|
|
|
// 加api前缀
|
|
|
config.url = url;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (requestMap[config.url] === true) { // 避免重复的请求
|
|
|
return false;
|
|
|
}
|
|
|
// 非file_update请求
|
|
|
if (config.url.indexOf('update_file') === -1) {
|
|
|
requestMap[config.url] = true;
|
|
|
|
|
|
window.setTimeout("setfalseInRequestMap('"+config.url+"')", 900)
|
|
|
}
|
|
|
// setTimeout("setfalseInRequestMap(" + config.url + ")", 1200)
|
|
|
return config;
|
|
|
},
|
|
|
err => {
|
|
|
return Promise.reject(err);
|
|
|
});
|
|
|
|
|
|
axios.interceptors.response.use(function (response) {
|
|
|
// console.log(".............")
|
|
|
if(response===undefined){
|
|
|
return
|
|
|
}
|
|
|
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 || '服务器异常,请联系管理员。',
|
|
|
});
|
|
|
// notification['error']({
|
|
|
// message:"提示",
|
|
|
// description: response.data.message || '服务器异常,请联系管理员。',
|
|
|
// });
|
|
|
}
|
|
|
|
|
|
throw new axios.Cancel('Operation canceled by the user.');
|
|
|
}
|
|
|
if (response.data.status === 403) {
|
|
|
// props.history.replace('/403')
|
|
|
// 这里会分2个情况,1、刚进入页面发请求返回的403;2、进入页面后,其他用户操作触发请求返回的403;
|
|
|
// TODO 这里做一个403弹框比较好?
|
|
|
locationurl('/403');
|
|
|
}
|
|
|
|
|
|
if (response.data.status === 404) {
|
|
|
locationurl('/nopage');
|
|
|
}
|
|
|
|
|
|
if (response.data.status === 500) {
|
|
|
locationurl('/500');
|
|
|
}
|
|
|
|
|
|
if (response.data.status === 402) {
|
|
|
locationurl(response.data.url);
|
|
|
}
|
|
|
// if (response.data.status === 407) {
|
|
|
// 在app js 中解决 Trialapplication
|
|
|
// // </Trialapplication>
|
|
|
// ///在appjs
|
|
|
// notification.open({
|
|
|
// message:"提示",
|
|
|
// description: "账号未认证",
|
|
|
// });
|
|
|
// throw new axios.Cancel('Operation canceled by the user.');
|
|
|
// //
|
|
|
// }
|
|
|
|
|
|
requestMap[response.config.url] = false;
|
|
|
|
|
|
return response;
|
|
|
}, function (error) {
|
|
|
return Promise.reject(error);
|
|
|
});
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
|
|
} |