App.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React, { Component } from 'react';
  2. import { Router, Route, Switch, Redirect } from 'react-router-dom';
  3. import { Provider, connect } from 'react-redux';
  4. import Async from './Async';
  5. const StoreAsync = connect(state => state)(Async);
  6. const UserStoreAsync = connect(state => {
  7. return { user: state.user };
  8. })(Async);
  9. export default class App extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.moduleList = [];
  13. this.moduleMap = {};
  14. this.init();
  15. }
  16. init() {
  17. const { routes } = this.props;
  18. for (let i = 0; i < routes.length; i += 1) {
  19. this.pageRegister(routes[i]);
  20. }
  21. }
  22. pageRegister(config) {
  23. if (!config.index) return;
  24. if (!config.module) return;
  25. if (!this.moduleMap[config.module.key]) {
  26. this.moduleList.push(config.module.key);
  27. this.moduleMap[config.module.key] = { groupList: [], groupMap: {}, ...config.module, path: config.path };
  28. }
  29. if (!config.group) return;
  30. if (!this.moduleMap[config.module.key].groupMap[config.group.key] && config.group.key) {
  31. this.moduleMap[config.module.key].groupList.push(config.group.key);
  32. this.moduleMap[config.module.key].groupMap[config.group.key] = {
  33. subList: [],
  34. subMap: {},
  35. ...config.group,
  36. path: config.path,
  37. };
  38. }
  39. if (!this.moduleMap[config.module.key].groupMap[config.group.key].subMap[config.key]) {
  40. this.moduleMap[config.module.key].groupMap[config.group.key].subList.push(config.key);
  41. this.moduleMap[config.module.key].groupMap[config.group.key].subMap[config.key] = { ...config };
  42. }
  43. }
  44. render() {
  45. const { store } = this.props;
  46. return <Provider store={store}>{this.renderRouter()}</Provider>;
  47. }
  48. renderRouter() {
  49. const { project, routes, store, history } = this.props;
  50. return (
  51. <Router history={history}>
  52. <Switch>
  53. {project.rootPath && (
  54. <Route exact path="/">
  55. <Redirect to={project.rootPath} />
  56. </Route>
  57. )}
  58. {routes.map(route => {
  59. return (
  60. <Route
  61. exact
  62. key={route.key}
  63. path={route.matchPath || route.path}
  64. render={props => {
  65. if (project.loginAuth && !project.loginAuth(route, store.getState())) {
  66. return <Redirect to={project.loginPath || '/login'} />;
  67. }
  68. if (project.powerAuth && !project.powerAuth(route, store.getState())) {
  69. return <Redirect to={project.powerPath || '/power'} />;
  70. }
  71. return this.renderMode(route, props);
  72. }}
  73. />
  74. );
  75. })}
  76. <Route path="*">
  77. <Redirect to={project.emptyPath || '/empty'} />
  78. </Route>
  79. </Switch>
  80. </Router>
  81. );
  82. }
  83. renderMode(route, props) {
  84. const { project } = this.props;
  85. let c;
  86. if (typeof project.mode === 'function') {
  87. c = project.mode;
  88. } else {
  89. switch (project.mode) {
  90. case 'admin':
  91. c = () => import('./Admin');
  92. break;
  93. case 'adminLeft':
  94. c = () => import('./AdminLeft');
  95. break;
  96. default:
  97. return this.renderPage(route, props);
  98. }
  99. }
  100. return (
  101. <UserStoreAsync
  102. config={route}
  103. {...props}
  104. key={props.location.key}
  105. moduleList={this.moduleList}
  106. moduleMap={this.moduleMap}
  107. project={project}
  108. component={c}
  109. >
  110. {this.renderPage(route, props)}
  111. </UserStoreAsync>
  112. );
  113. }
  114. renderPage(route, props) {
  115. return <StoreAsync isForm config={route} {...props} component={() => route.component()} />;
  116. }
  117. }