beijingos/public/react/src/index.js

103 lines
3.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 axios from 'axios';
// TODO 避免重复的请求 https://github.com/axios/axios#cancellation
// https://github.com/axios/axios/issues/1497
// TODO 读取到package.json中的配置
var proxy = "http://localhost:3000"
// 在这里使用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;
// }
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") { // 表示为开发模式
config.url = `${proxy}${url}`; // 开发模式下直接跨域请求
}
if (requestMap[config.url] === true) { // 避免重复的请求
return false;
}
// 非file_update请求
if (config.url.indexOf('file_update') === -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;
<<<<<<< HEAD
return response;
}, function (error) {
=======
return response;
}, function (error) {
// if (error.response && error.response.data.error === '401 Unauthorized') {
// // 未登录 先跳转到登录页TODO 打开登录弹框
// window.location.href = window.location.host + '/login'
// }
>>>>>>> 6fb91208a2a706c32bda3b3da78d3c806b9ac36a
return Promise.reject(error);
});
// -----------------------------------------------------------------------------------
const render = (Component) => {
ReactDOM.render(
<AppContainer>
<Component />
</AppContainer>,
document.getElementById('root')
);
}
// ReactDOM.render(
// ,
// document.getElementById('root'));
registerServiceWorker();
render(App);
if (module.hot) {
module.hot.accept('./App', () => { render(App) });
}