You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/public/react/src/index.js

126 lines
4.0 KiB

6 years ago
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { AppContainer } from 'react-hot-loader';
import registerServiceWorker from './registerServiceWorker';
import { configureUrlQuery } from 'react-url-query';
// import history from './history';
import { Provider } from 'react-redux'
import { ConnectedRouter } from 'connected-react-router'
import store, { history } from './store'
import axios from 'axios';
import { isDev } from 'educoder';
// import { initMock } from './axiosMock.js'
import './index.css'
// link the history used in our app to url-query so it can update the URL with it.
configureUrlQuery({ history });
// ----------------------------------------------------------------------------------- 请求配置
window.__useKindEditor = false;
// 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 = 'http://192.168.0.195:3000'
proxy ='https://testbdweb.educoder.net'
proxy ='http://testbdweb.educoder.net'
// proxy='https://www.educoder.net'
const requestMap={};
// 在这里使用requestMap控制避免用户通过双击等操作发出重复的请求
// 如果需要支持重复的请求考虑config里面自定义一个allowRepeat参考来控制
// 模拟请求,开启后可以使用假数据,脱离后台开发。 注意上线正式版时需要注释掉mock调用mock.js有30kb+的大小
if (isDev()) {
// initMock(true, proxy)
// console.error("******* 注意开启接口mock这会导致没mock配置的请求返回404 ******")
}
window.setfalseInRequestMap = function(keyName) {
requestMap[keyName] = false;
}
axios.interceptors.request.use(
config => {
// if (token) { // 每次发送请求之前判断是否存在token如果存在则统一在http请求的header都加上token不用每次请求都手动添加了
// config.headers.Authorization = token;
// }
var url = config.url;
// --------------------------------------------- 測試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 (window.location.port === "3007" && !url.startsWith('http')) { // 表示为开发模式
config.url = `${proxy}${url}`; // 开发模式下直接跨域请求
}
if (requestMap[config.url] === true) { // 避免重复的请求
return false;
}
// 非file_update请求 && 非open_webssh
if (config.url.indexOf('file_update') === -1 && config.url.indexOf('open_webssh') === -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) {
requestMap[response.config.url] = false;
return response;
}, function (error) {
return Promise.reject(error);
});
// -----------------------------------------------------------------------------------
const render = (Component) => {
ReactDOM.render(
<Provider store={store}>
<ConnectedRouter history={history}>
<AppContainer>
<Component />
</AppContainer>
</ConnectedRouter>
</Provider>,
document.getElementById('root')
);
}
// document.getElementById('root'));
registerServiceWorker();
render(App);
if (module.hot) {
module.hot.accept('./App', () => { render(App) });
}