import React, { Component } from 'react'; import { Router, Route, Switch, Redirect } from 'react-router-dom'; import { Provider, connect } from 'react-redux'; import Async from './Async'; const StoreAsync = connect(state => state)(Async); const UserStoreAsync = connect(state => { return { user: state.user }; })(Async); export default class App extends Component { constructor(props) { super(props); this.moduleList = []; this.moduleMap = {}; this.init(); } init() { const { routes } = this.props; for (let i = 0; i < routes.length; i += 1) { this.pageRegister(routes[i]); } } pageRegister(config) { if (!config.index) return; if (!config.module) return; if (!this.moduleMap[config.module.key]) { this.moduleList.push(config.module.key); this.moduleMap[config.module.key] = { groupList: [], groupMap: {}, ...config.module, path: config.path }; } if (!config.group) return; if (!this.moduleMap[config.module.key].groupMap[config.group.key] && config.group.key) { this.moduleMap[config.module.key].groupList.push(config.group.key); this.moduleMap[config.module.key].groupMap[config.group.key] = { subList: [], subMap: {}, ...config.group, path: config.path, }; } if (!this.moduleMap[config.module.key].groupMap[config.group.key].subMap[config.key]) { this.moduleMap[config.module.key].groupMap[config.group.key].subList.push(config.key); this.moduleMap[config.module.key].groupMap[config.group.key].subMap[config.key] = { ...config }; } } render() { const { store } = this.props; return {this.renderRouter()}; } renderRouter() { const { project, routes, store, history } = this.props; return ( {project.rootPath && ( )} {routes.map(route => { return ( { if (project.loginAuth && !project.loginAuth(route, store.getState())) { return ; } if (project.powerAuth && !project.powerAuth(route, store.getState())) { return ; } return this.renderMode(route, props); }} /> ); })} ); } renderMode(route, props) { const { project } = this.props; let c; if (typeof project.mode === 'function') { c = project.mode; } else { switch (project.mode) { case 'admin': c = () => import('./Admin'); break; case 'adminLeft': c = () => import('./AdminLeft'); break; default: return this.renderPage(route, props); } } return ( {this.renderPage(route, props)} ); } renderPage(route, props) { return route.component()} />; } }