forgeplus-react/src/App.js

498 lines
13 KiB
JavaScript
Raw Normal View History

2020-04-28 17:23:34 +08:00
import React, { Component } from 'react';
2020-03-16 14:12:11 +08:00
import './App.css';
2020-04-28 17:23:34 +08:00
import { ConfigProvider } from 'antd'
2020-03-16 14:12:11 +08:00
import zhCN from 'antd/lib/locale-provider/zh_CN';
import {
2021-08-30 10:31:51 +08:00
// BrowserRouter as Router,
2020-03-16 14:12:11 +08:00
Route,
Switch
} from 'react-router-dom';
import axios from 'axios';
import LoginDialog from './modules/login/LoginDialog';
2020-11-25 17:25:59 +08:00
import 'babel-polyfill';
2020-03-16 14:12:11 +08:00
import Loading from './Loading'
import Loadable from 'react-loadable';
2020-06-18 11:36:05 +08:00
import marked from './common/marked';
2020-03-16 14:12:11 +08:00
import moment from 'moment'
2020-04-28 17:23:34 +08:00
import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles';
import SiderBar from './forge/Component/SiderBar'
2020-03-16 14:12:11 +08:00
2020-04-28 17:23:34 +08:00
import { SnackbarHOC } from 'educoder'
import { initAxiosInterceptors } from './AppConfig'
2020-03-16 14:12:11 +08:00
import { Provider } from 'react-redux';
import configureStore from './redux/stores/configureStore';
const store = configureStore();
2020-05-09 11:54:17 +08:00
window.marked = marked;
2020-03-16 14:12:11 +08:00
const theme = createMuiTheme({
palette: {
primary: {
main: '#4CACFF',
contrastText: 'rgba(255, 255, 255, 0.87)'
},
2020-04-28 17:23:34 +08:00
secondary: { main: '#4CACFF' }, // #11cb5f This is just green.A700 as hex.
2020-03-16 14:12:11 +08:00
},
});
2020-04-28 17:23:34 +08:00
//forge项目
const Projects = Loadable({
loader: () => import('./forge/Index'),
loading: Loading,
})
2021-07-22 17:44:12 +08:00
//forge安全设置
const Security = Loadable({
loader: () => import('./forge/SecuritySetting/Index'),
loading: Loading,
})
2020-07-10 15:42:57 +08:00
//forge项目-devOps详情
const OpsDetail = Loadable({
loader: () => import('./forge/DevOps/opsDetail'),
loading: Loading,
})
2020-03-16 14:12:11 +08:00
//403页面
const Shixunauthority = Loadable({
loader: () => import('./modules/403/Shixunauthority'),
loading: Loading,
})
//404页面
const Shixunnopage = Loadable({
loader: () => import('./modules/404/Shixunnopage'),
loading: Loading,
})
//500页面
const http500 = Loadable({
loader: () => import('./modules/500/http500'),
loading: Loading,
})
2020-05-08 10:36:31 +08:00
const InfosIndex = Loadable({
2020-06-15 15:31:21 +08:00
loader: () => import('./forge/users/Index'),
2020-05-08 10:36:31 +08:00
loading: Loading,
})
2020-06-04 18:27:19 +08:00
// 组织
const OrganizeIndex = Loadable({
loader: () => import('./forge/Team/Index'),
loading: Loading,
})
2020-11-18 11:31:52 +08:00
const EducoderLogin = Loadable({
loader: () => import('./modules/login/EducoderLogin'),
loading: Loading,
})
2021-06-03 10:37:03 +08:00
const Search = Loadable({
loader: () => import('./modules/search/'),
loading: Loading,
})
2021-08-04 11:18:21 +08:00
const WikiPreview = Loadable({
loader: () => import('./forge/Wiki/Preview'),
loading: Loading,
})
2020-05-08 10:36:31 +08:00
const ProjectIndex = Loadable({
2021-08-04 11:18:21 +08:00
loader: () => import("./forge/Index"),
loading: Loading,
});
2021-09-02 13:37:24 +08:00
// 此处仅维护前端可能的一级路由,不用进行项目或者组织判断的字段。
const keyWord = ["explore", "settings", "setting", "CCF", "mulan", "wiki", "issues", "setting", "trending", "code", "projects", "pulls", "mine", "login", "register", "email", "export", "nopage", "404", "403", "500", "501", "search"];
2020-03-16 14:12:11 +08:00
class App extends Component {
constructor(props) {
2020-06-12 14:21:56 +08:00
super(props);
2020-03-16 14:12:11 +08:00
this.state = {
2020-04-28 17:23:34 +08:00
Addcoursestype: false,
Addcoursestypes: false,
mydisplay: false,
occupation: 0,
2020-03-16 14:12:11 +08:00
mygetHelmetapi: null,
2021-08-26 14:18:14 +08:00
pathType: null,
2021-09-02 17:20:26 +08:00
pathName: null,
2021-08-26 14:18:14 +08:00
}
}
UNSAFE_componentWillMount() {
initAxiosInterceptors(this.props);
let pathname = window.location.pathname ? window.location.pathname.split('/')[1] : '';
pathname && this.getPathnameType(pathname);
2021-08-30 10:31:51 +08:00
2021-08-30 17:42:13 +08:00
// 添加路由监听,决定组织还是个人
this.unlisten = this.props.history.listen((location) => {
let newPathname = location.pathname.split('/')[1];
2021-09-02 13:37:24 +08:00
if (this.state.pathName !== newPathname) {
2021-09-03 11:38:22 +08:00
// this.setState({ pathType: '' });
2021-08-30 17:42:13 +08:00
newPathname && this.getPathnameType(newPathname);
}
2021-08-30 10:31:51 +08:00
});
2021-08-26 14:18:14 +08:00
}
shouldComponentUpdate(nextProps, nextState) {
2021-09-02 13:37:24 +08:00
// (!keyWord.includes(this.props.location.pathname.split('/')[1])) &&
if (nextProps.location.pathname.split('/')[1] !== this.props.location.pathname.split('/')[1] && nextState.pathType === this.state.pathType) {
return false;
} else {
return true;
}
}
2021-08-26 14:18:14 +08:00
getPathnameType = (pathname) => {
2021-08-30 17:42:13 +08:00
if (!keyWord.includes(pathname)) {
2021-08-26 14:18:14 +08:00
let url = `/owners/${pathname}.json`;
axios.get(url).then((response) => {
if (response && response.status === 200) {
this.setState({
2021-08-30 17:42:13 +08:00
pathType: response.data.type || '404',
2021-09-02 13:37:24 +08:00
pathName: pathname,
2021-08-26 14:18:14 +08:00
})
}
});
2021-09-02 13:37:24 +08:00
}else{
this.setState({
pathType: pathname,
pathName: pathname,
});
2020-03-16 14:12:11 +08:00
}
}
2020-06-18 11:36:05 +08:00
2020-04-28 17:23:34 +08:00
HideAddcoursestypess = (i) => {
2020-03-16 14:12:11 +08:00
this.setState({
2020-04-28 17:23:34 +08:00
Addcoursestype: false,
Addcoursestypes: false,
mydisplay: true,
occupation: i,
2020-03-16 14:12:11 +08:00
})
};
2020-04-28 17:23:34 +08:00
hideAddcoursestypes = () => {
2020-03-16 14:12:11 +08:00
this.setState({
2020-04-28 17:23:34 +08:00
Addcoursestypes: false
2020-03-16 14:12:11 +08:00
})
};
componentDidMount() {
document.title = "loading...";
2021-08-26 14:18:14 +08:00
2020-04-28 17:23:34 +08:00
this.getAppdata();
2020-03-16 14:12:11 +08:00
window.addEventListener('error', (event) => {
const msg = `${event.type}: ${event.message}`;
});
2021-08-30 10:31:51 +08:00
2020-03-16 14:12:11 +08:00
}
2021-08-30 10:31:51 +08:00
componentWillUnmount() {
this.unlisten && this.unlisten(); // 执行解绑
}
2020-03-16 14:12:11 +08:00
//修改登录方法
2020-04-28 17:23:34 +08:00
Modifyloginvalue = () => {
2020-03-16 14:12:11 +08:00
this.setState({
2020-04-28 17:23:34 +08:00
isRender: false,
2020-03-16 14:12:11 +08:00
})
};
//获取数据为空的时候
gettablogourlnull = () => {
this.setState({
mygetHelmetapi: undefined
});
2020-06-12 11:13:45 +08:00
document.title = "Forge";
2020-03-16 14:12:11 +08:00
var link = document.createElement('link'),
oldLink = document.getElementById('dynamic-favicon');
link.id = 'dynamic-favicon';
link.rel = 'shortcut icon';
link.href = "/react/build/./favicon.ico";
2020-03-16 14:12:11 +08:00
if (oldLink) {
document.head.removeChild(oldLink);
}
document.head.appendChild(link);
};
//获取数据的时候
gettablogourldata = (response) => {
document.title = response.data.setting.name;
var link = document.createElement('link'),
oldLink = document.getElementById('dynamic-favicon');
link.id = 'dynamic-favicon';
link.rel = 'shortcut icon';
link.href = '/' + response.data.setting.tab_logo_url;
if (oldLink) {
document.head.removeChild(oldLink);
}
document.head.appendChild(link);
}
2020-04-28 17:23:34 +08:00
//获取当前定制信息
getAppdata = () => {
2020-03-16 14:12:11 +08:00
let url = "/setting.json";
axios.get(url).then((response) => {
2020-04-28 17:23:34 +08:00
if (response) {
if (response.data) {
2020-03-16 14:12:11 +08:00
this.setState({
2020-04-28 17:23:34 +08:00
mygetHelmetapi: response.data.setting
2020-03-16 14:12:11 +08:00
});
//存储配置到游览器
2020-04-28 17:23:34 +08:00
localStorage.setItem('chromesetting', JSON.stringify(response.data.setting));
localStorage.setItem('chromesettingresponse', JSON.stringify(response));
2020-03-16 14:12:11 +08:00
try {
if (response.data.setting.tab_logo_url) {
this.gettablogourldata(response);
} else {
this.gettablogourlnull();
}
} catch (e) {
this.gettablogourlnull();
}
} else {
this.gettablogourlnull();
}
} else {
this.gettablogourlnull();
}
}).catch((error) => {
this.gettablogourlnull();
});
};
render() {
2021-09-03 09:23:12 +08:00
const { mygetHelmetapi, pathType} = this.state;
let personal = mygetHelmetapi && mygetHelmetapi.personal;
2020-03-16 14:12:11 +08:00
return (
<Provider store={store}>
<ConfigProvider locale={zhCN}>
<MuiThemeProvider theme={theme}>
2020-04-28 17:23:34 +08:00
<LoginDialog {...this.props} {...this.state} Modifyloginvalue={() => this.Modifyloginvalue()}></LoginDialog>
2021-08-04 11:18:21 +08:00
<SiderBar />
2021-08-30 10:31:51 +08:00
{/* <Router> */}
<Switch>
{/* wiki预览 */}
2021-09-01 09:33:22 +08:00
<Route path="/:owner/:projectsId/wiki/preview/:projectName/:projectId" render={
2021-08-30 10:31:51 +08:00
(props) => {
return (<WikiPreview {...this.props} {...props} {...this.state} />)
}
} />
{/*项目*/}
<Route
2021-09-01 09:33:22 +08:00
path={"/:owner/:projectId/devops/:opsId/detail"}
2021-08-30 10:31:51 +08:00
render={
2021-08-04 11:18:21 +08:00
(props) => {
2021-08-30 10:31:51 +08:00
return (<OpsDetail {...this.props} {...props} {...this.state} />)
2021-08-04 11:18:21 +08:00
}
2021-08-30 10:31:51 +08:00
}>
</Route>
<Route
path={"/settings"}
render={
(props) => {
return (<Security {...this.props} {...props} {...this.state} />)
2020-11-18 11:31:52 +08:00
}
2021-08-30 10:31:51 +08:00
}>
</Route>
2021-09-02 13:37:24 +08:00
2021-08-30 10:31:51 +08:00
<Route
path="/register"
render={
(props) => {
return (<EducoderLogin {...this.props} {...props} {...this.state} />)
2021-08-26 14:18:14 +08:00
}
}
2021-08-30 10:31:51 +08:00
/>
{/*403*/}
<Route path="/403" component={Shixunauthority} />
2021-08-26 14:18:14 +08:00
2021-08-30 10:31:51 +08:00
<Route path="/500" component={http500} />
2021-08-26 14:18:14 +08:00
2021-08-30 10:31:51 +08:00
{/*404*/}
<Route path="/nopage" component={Shixunnopage} />
2021-08-04 11:18:21 +08:00
2021-08-30 10:31:51 +08:00
{/* 查询 */}
<Route path="/search" component={Search} />
<Route exact path="/explore"
render={
(props) => (
<ProjectIndex {...this.props} {...props} />
)
}
/>
2021-08-26 14:18:14 +08:00
2021-09-03 11:38:22 +08:00
{/* 组织 */}
<Route path={"/organize"}
render={
(props) => {
return (<OrganizeIndex {...props} {...this.props} {...this.state} />)
}
}>
</Route>
2021-09-02 17:20:26 +08:00
{/*新建项目等*/}
<Route
path={"/projects"}
render={
(props) => {
return (<Projects {...this.props} {...props} {...this.state} />)
}
}>
</Route>
2021-08-30 10:31:51 +08:00
{/* 判断为用户/组织,并进入对应页面 */}
{
pathType === 'User' ?
<Route exact path="/:username"
render={
(props) => {
return (<InfosIndex {...this.props} {...this.state} />)
}
}
/> : pathType === 'Organization' ? <Route path={"/:OIdentifier"}
render={
(props) => {
return (<OrganizeIndex {...props} {...this.props} {...this.state} />)
}
}>
2021-09-03 09:23:12 +08:00
</Route> : pathType === '404' ? <Route path="/" component={Shixunnopage} /> :
2021-09-02 13:37:24 +08:00
<Route exact path="/"
render={
(props) => (
personal && personal.length > 0 ?
<InfosIndex {...this.props} {...props} />
:
<ProjectIndex {...this.props} {...props} />
)
}
/>
// <Route path="/" component={Loading} />
2021-08-30 10:31:51 +08:00
}
2021-09-02 13:37:24 +08:00
2021-08-30 10:31:51 +08:00
{/* 个人主页 */}
<Route path="/:username"
render={
(props) => {
return (<InfosIndex {...this.props} {...this.state} />)
2020-04-28 17:23:34 +08:00
}
2021-08-30 10:31:51 +08:00
}></Route>
<Route component={Shixunnopage} />
</Switch>
{/* </Router> */}
2020-03-16 14:12:11 +08:00
</MuiThemeProvider>
</ConfigProvider>
</Provider>
);
}
}
// moment国际化设置为中文
moment.defineLocale('zh-cn', {
months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'),
monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'),
weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'),
weekdaysShort: '周日_周一_周二_周三_周四_周五_周六'.split('_'),
weekdaysMin: '日_一_二_三_四_五_六'.split('_'),
longDateFormat: {
LT: 'Ah点mm分',
LTS: 'Ah点m分s秒',
L: 'YYYY-MM-DD',
LL: 'YYYY年MMMD日',
LLL: 'YYYY年MMMD日Ah点mm分',
LLLL: 'YYYY年MMMD日ddddAh点mm分',
l: 'YYYY-MM-DD',
ll: 'YYYY年MMMD日',
lll: 'YYYY年MMMD日Ah点mm分',
llll: 'YYYY年MMMD日ddddAh点mm分'
},
meridiemParse: /凌晨|早上|上午|中午|下午|晚上/,
meridiemHour: function (hour, meridiem) {
if (hour === 12) {
hour = 0;
}
if (meridiem === '凌晨' || meridiem === '早上' ||
meridiem === '上午') {
return hour;
} else if (meridiem === '下午' || meridiem === '晚上') {
return hour + 12;
} else {
// '中午'
return hour >= 11 ? hour : hour + 12;
}
},
meridiem: function (hour, minute, isLower) {
var hm = hour * 100 + minute;
if (hm < 600) {
return '凌晨';
} else if (hm < 900) {
return '早上';
} else if (hm < 1130) {
return '上午';
} else if (hm < 1230) {
return '中午';
} else if (hm < 1800) {
return '下午';
} else {
return '晚上';
}
},
calendar: {
sameDay: function () {
return this.minutes() === 0 ? '[今天]Ah[点整]' : '[今天]LT';
},
nextDay: function () {
return this.minutes() === 0 ? '[明天]Ah[点整]' : '[明天]LT';
},
lastDay: function () {
return this.minutes() === 0 ? '[昨天]Ah[点整]' : '[昨天]LT';
},
nextWeek: function () {
var startOfWeek, prefix;
startOfWeek = moment().startOf('week');
prefix = this.unix() - startOfWeek.unix() >= 7 * 24 * 3600 ? '[下]' : '[本]';
return this.minutes() === 0 ? prefix + 'dddAh点整' : prefix + 'dddAh点mm';
},
lastWeek: function () {
var startOfWeek, prefix;
startOfWeek = moment().startOf('week');
prefix = this.unix() < startOfWeek.unix() ? '[上]' : '[本]';
return this.minutes() === 0 ? prefix + 'dddAh点整' : prefix + 'dddAh点mm';
},
sameElse: 'LL'
},
ordinalParse: /\d{1,2}(日|月|周)/,
ordinal: function (number, period) {
switch (period) {
case 'd':
case 'D':
case 'DDD':
return number + '日';
case 'M':
return number + '月';
case 'w':
case 'W':
return number + '周';
default:
return number;
}
},
relativeTime: {
future: '%s内',
past: '%s前',
s: '几秒',
m: '1分钟',
mm: '%d分钟',
h: '1小时',
hh: '%d小时',
d: '1天',
dd: '%d天',
M: '1个月',
MM: '%d个月',
y: '1年',
yy: '%d年'
},
week: {
// GB/T 7408-1994《数据元和交换格式·信息交换·日期和时间表示法》与ISO 8601:1988等效
dow: 1, // Monday is the first day of the week.
doy: 4 // The week that contains Jan 4th is the first week of the year.
}
});
2020-04-28 17:23:34 +08:00
export default SnackbarHOC()(App);