mock平台

Application.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import React, { PureComponent as Component } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { connect } from 'react-redux';
  4. import PropTypes from 'prop-types';
  5. import { Route, BrowserRouter as Router } from 'react-router-dom';
  6. import { Home, Group, Project, Follows, AddProject, Login } from './containers/index';
  7. import { Alert } from 'antd';
  8. import User from './containers/User/User.js';
  9. import Header from './components/Header/Header';
  10. import Footer from './components/Footer/Footer';
  11. import Loading from './components/Loading/Loading';
  12. import MyPopConfirm from './components/MyPopConfirm/MyPopConfirm';
  13. import { checkLoginState } from './reducer/modules/user';
  14. import { requireAuthentication } from './components/AuthenticatedComponent';
  15. import Notify from './components/Notify/Notify';
  16. const plugin = require('client/plugin.js');
  17. const LOADING_STATUS = 0;
  18. const alertContent = () => {
  19. const ua = window.navigator.userAgent,
  20. isChrome = ua.indexOf('Chrome') && window.chrome;
  21. if (!isChrome) {
  22. return (
  23. <Alert
  24. style={{ zIndex: 99 }}
  25. message={'YApi 的接口测试等功能仅支持 Chrome 浏览器,请使用 Chrome 浏览器获得完整功能。'}
  26. banner
  27. closable
  28. />
  29. );
  30. }
  31. };
  32. let AppRoute = {
  33. home: {
  34. path: '/',
  35. component: Home
  36. },
  37. group: {
  38. path: '/group',
  39. component: Group
  40. },
  41. project: {
  42. path: '/project/:id',
  43. component: Project
  44. },
  45. user: {
  46. path: '/user',
  47. component: User
  48. },
  49. follow: {
  50. path: '/follow',
  51. component: Follows
  52. },
  53. addProject: {
  54. path: '/add-project',
  55. component: AddProject
  56. },
  57. login: {
  58. path: '/login',
  59. component: Login
  60. }
  61. };
  62. // 增加路由钩子
  63. plugin.emitHook('app_route', AppRoute);
  64. @connect(
  65. state => {
  66. return {
  67. loginState: state.user.loginState,
  68. curUserRole: state.user.role
  69. };
  70. },
  71. {
  72. checkLoginState
  73. }
  74. )
  75. export default class App extends Component {
  76. constructor(props) {
  77. super(props);
  78. this.state = {
  79. login: LOADING_STATUS
  80. };
  81. }
  82. static propTypes = {
  83. checkLoginState: PropTypes.func,
  84. loginState: PropTypes.number,
  85. curUserRole: PropTypes.string
  86. };
  87. componentDidMount() {
  88. this.props.checkLoginState();
  89. }
  90. showConfirm = (msg, callback) => {
  91. // 自定义 window.confirm
  92. // http://reacttraining.cn/web/api/BrowserRouter/getUserConfirmation-func
  93. let container = document.createElement('div');
  94. document.body.appendChild(container);
  95. ReactDOM.render(<MyPopConfirm msg={msg} callback={callback} />, container);
  96. };
  97. route = status => {
  98. let r;
  99. if (status === LOADING_STATUS) {
  100. return <Loading visible />;
  101. } else {
  102. r = (
  103. <Router getUserConfirmation={this.showConfirm}>
  104. <div className="g-main">
  105. <div className="router-main">
  106. {this.props.curUserRole === 'admin' && <Notify />}
  107. {alertContent()}
  108. {this.props.loginState !== 1 ? <Header /> : null}
  109. <div className="router-container">
  110. {Object.keys(AppRoute).map(key => {
  111. let item = AppRoute[key];
  112. return key === 'login' ? (
  113. <Route key={key} path={item.path} component={item.component} />
  114. ) : key === 'home' ? (
  115. <Route key={key} exact path={item.path} component={item.component} />
  116. ) : (
  117. <Route
  118. key={key}
  119. path={item.path}
  120. component={requireAuthentication(item.component)}
  121. />
  122. );
  123. })}
  124. </div>
  125. {/* <div className="router-container">
  126. <Route exact path="/" component={Home} />
  127. <Route path="/group" component={requireAuthentication(Group)} />
  128. <Route path="/project/:id" component={requireAuthentication(Project)} />
  129. <Route path="/user" component={requireAuthentication(User)} />
  130. <Route path="/follow" component={requireAuthentication(Follows)} />
  131. <Route path="/add-project" component={requireAuthentication(AddProject)} />
  132. <Route path="/login" component={Login} />
  133. {/* <Route path="/statistic" component={statisticsPage} /> */}
  134. {/* </div> */}
  135. </div>
  136. <Footer />
  137. </div>
  138. </Router>
  139. );
  140. }
  141. return r;
  142. };
  143. render() {
  144. return this.route(this.props.loginState);
  145. }
  146. }