router.js 8.2 KB
Newer Older
偏右's avatar
偏右 committed
1
import { createElement } from 'react';
ddcat1115's avatar
ddcat1115 committed
2
import dynamic from 'dva/dynamic';
陈帅's avatar
陈帅 committed
3
import pathToRegexp from 'path-to-regexp';
ddcat1115's avatar
ddcat1115 committed
4 5
import { getMenuData } from './menu';

偏右's avatar
偏右 committed
6 7 8 9
let routerDataCache;

const modelNotExisted = (app, model) => (
  // eslint-disable-next-line
PKAQ's avatar
PKAQ committed
10 11 12
  !app._models.some(({ namespace }) => {
    return namespace === model.substring(model.lastIndexOf('/') + 1);
  })
偏右's avatar
偏右 committed
13 14
);

ddcat1115's avatar
ddcat1115 committed
15
// wrapper of dynamic
偏右's avatar
偏右 committed
16 17 18 19 20 21 22 23 24
const dynamicWrapper = (app, models, component) => {
  // () => require('module')
  // transformed by babel-plugin-dynamic-import-node-sync
  if (component.toString().indexOf('.then(') < 0) {
    models.forEach((model) => {
      if (modelNotExisted(app, model)) {
        // eslint-disable-next-line
        app.model(require(`../models/${model}`).default);
      }
ddcat1115's avatar
ddcat1115 committed
25
    });
偏右's avatar
偏右 committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    return (props) => {
      if (!routerDataCache) {
        routerDataCache = getRouterData(app);
      }
      return createElement(component().default, {
        ...props,
        routerData: routerDataCache,
      });
    };
  }
  // () => import('module')
  return dynamic({
    app,
    models: () => models.filter(
      model => modelNotExisted(app, model)).map(m => import(`../models/${m}.js`)
    ),
    // add routerData prop
    component: () => {
      if (!routerDataCache) {
        routerDataCache = getRouterData(app);
      }
      return component().then((raw) => {
        const Component = raw.default || raw;
        return props => createElement(Component, {
          ...props,
          routerData: routerDataCache,
        });
      });
    },
  });
};
ddcat1115's avatar
ddcat1115 committed
57 58 59 60 61

function getFlatMenuData(menus) {
  let keys = {};
  menus.forEach((item) => {
    if (item.children) {
ddcat1115's avatar
ddcat1115 committed
62
      keys[item.path] = { ...item };
ddcat1115's avatar
ddcat1115 committed
63 64
      keys = { ...keys, ...getFlatMenuData(item.children) };
    } else {
ddcat1115's avatar
ddcat1115 committed
65
      keys[item.path] = { ...item };
ddcat1115's avatar
ddcat1115 committed
66 67 68 69 70 71
    }
  });
  return keys;
}

export const getRouterData = (app) => {
ddcat1115's avatar
ddcat1115 committed
72
  const routerConfig = {
ddcat1115's avatar
ddcat1115 committed
73
    '/': {
jim's avatar
jim committed
74
      component: dynamicWrapper(app, ['user', 'login', 'setting'], () => import('../layouts/LoadingPage')),
ddcat1115's avatar
ddcat1115 committed
75 76 77 78 79 80 81 82
    },
    '/dashboard/analysis': {
      component: dynamicWrapper(app, ['chart'], () => import('../routes/Dashboard/Analysis')),
    },
    '/dashboard/monitor': {
      component: dynamicWrapper(app, ['monitor'], () => import('../routes/Dashboard/Monitor')),
    },
    '/dashboard/workplace': {
ddcat1115's avatar
ddcat1115 committed
83
      component: dynamicWrapper(app, ['user', 'project', 'activities', 'chart'], () => import('../routes/Dashboard/Workplace')),
ddcat1115's avatar
ddcat1115 committed
84 85
      // hideInBreadcrumb: true,
      // name: '工作台',
ddcat1115's avatar
ddcat1115 committed
86
      // authority: 'admin',
ddcat1115's avatar
ddcat1115 committed
87 88 89 90 91 92 93
    },
    '/form/basic-form': {
      component: dynamicWrapper(app, ['form'], () => import('../routes/Forms/BasicForm')),
    },
    '/form/step-form': {
      component: dynamicWrapper(app, ['form'], () => import('../routes/Forms/StepForm')),
    },
WhatAKitty's avatar
WhatAKitty committed
94
    '/form/step-form/info': {
jim's avatar
jim committed
95
      name: '分步表单(填写转账信息)',
WhatAKitty's avatar
WhatAKitty committed
96 97
      component: dynamicWrapper(app, ['form'], () => import('../routes/Forms/StepForm/Step1')),
    },
ddcat1115's avatar
ddcat1115 committed
98
    '/form/step-form/confirm': {
jim's avatar
jim committed
99
      name: '分步表单(确认转账信息)',
ddcat1115's avatar
ddcat1115 committed
100 101 102
      component: dynamicWrapper(app, ['form'], () => import('../routes/Forms/StepForm/Step2')),
    },
    '/form/step-form/result': {
jim's avatar
jim committed
103
      name: '分步表单(完成)',
ddcat1115's avatar
ddcat1115 committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
      component: dynamicWrapper(app, ['form'], () => import('../routes/Forms/StepForm/Step3')),
    },
    '/form/advanced-form': {
      component: dynamicWrapper(app, ['form'], () => import('../routes/Forms/AdvancedForm')),
    },
    '/list/table-list': {
      component: dynamicWrapper(app, ['rule'], () => import('../routes/List/TableList')),
    },
    '/list/basic-list': {
      component: dynamicWrapper(app, ['list'], () => import('../routes/List/BasicList')),
    },
    '/list/card-list': {
      component: dynamicWrapper(app, ['list'], () => import('../routes/List/CardList')),
    },
    '/list/search': {
      component: dynamicWrapper(app, ['list'], () => import('../routes/List/List')),
    },
    '/list/search/projects': {
      component: dynamicWrapper(app, ['list'], () => import('../routes/List/Projects')),
    },
    '/list/search/applications': {
      component: dynamicWrapper(app, ['list'], () => import('../routes/List/Applications')),
    },
    '/list/search/articles': {
      component: dynamicWrapper(app, ['list'], () => import('../routes/List/Articles')),
    },
    '/profile/basic': {
      component: dynamicWrapper(app, ['profile'], () => import('../routes/Profile/BasicProfile')),
    },
    '/profile/advanced': {
      component: dynamicWrapper(app, ['profile'], () => import('../routes/Profile/AdvancedProfile')),
    },
    '/result/success': {
      component: dynamicWrapper(app, [], () => import('../routes/Result/Success')),
    },
    '/result/fail': {
      component: dynamicWrapper(app, [], () => import('../routes/Result/Error')),
    },
    '/exception/403': {
      component: dynamicWrapper(app, [], () => import('../routes/Exception/403')),
    },
    '/exception/404': {
      component: dynamicWrapper(app, [], () => import('../routes/Exception/404')),
    },
    '/exception/500': {
      component: dynamicWrapper(app, [], () => import('../routes/Exception/500')),
    },
陈帅's avatar
陈帅 committed
151 152 153
    '/exception/trigger': {
      component: dynamicWrapper(app, ['error'], () => import('../routes/Exception/triggerException')),
    },
ddcat1115's avatar
ddcat1115 committed
154 155 156 157 158 159 160 161 162 163 164 165
    '/user': {
      component: dynamicWrapper(app, [], () => import('../layouts/UserLayout')),
    },
    '/user/login': {
      component: dynamicWrapper(app, ['login'], () => import('../routes/User/Login')),
    },
    '/user/register': {
      component: dynamicWrapper(app, ['register'], () => import('../routes/User/Register')),
    },
    '/user/register-result': {
      component: dynamicWrapper(app, [], () => import('../routes/User/RegisterResult')),
    },
ddcat1115's avatar
ddcat1115 committed
166
    '/account/center': {
ddcat1115's avatar
ddcat1115 committed
167 168 169 170 171 172 173 174 175 176
      component: dynamicWrapper(app, ['list', 'user', 'project'], () => import('../routes/Account/Center/Center')),
    },
    '/account/center/articles': {
      component: dynamicWrapper(app, [], () => import('../routes/Account/Center/Articles')),
    },
    '/account/center/applications': {
      component: dynamicWrapper(app, [], () => import('../routes/Account/Center/Applications')),
    },
    '/account/center/projects': {
      component: dynamicWrapper(app, [], () => import('../routes/Account/Center/Projects')),
ddcat1115's avatar
ddcat1115 committed
177
    },
ddcat1115's avatar
ddcat1115 committed
178 179
    '/account/settings': {
      component: dynamicWrapper(app, ['geographic'], () => import('../routes/Account/Settings/Info')),
陈帅's avatar
陈帅 committed
180
    },
ddcat1115's avatar
ddcat1115 committed
181 182
    '/account/settings/base': {
      component: dynamicWrapper(app, ['geographic'], () => import('../routes/Account/Settings/BaseView')),
陈帅's avatar
陈帅 committed
183
    },
ddcat1115's avatar
ddcat1115 committed
184 185
    '/account/settings/security': {
      component: dynamicWrapper(app, ['geographic'], () => import('../routes/Account/Settings/SecurityView')),
陈帅's avatar
陈帅 committed
186
    },
ddcat1115's avatar
ddcat1115 committed
187 188
    '/account/settings/binding': {
      component: dynamicWrapper(app, ['geographic'], () => import('../routes/Account/Settings/BindingView')),
陈帅's avatar
陈帅 committed
189
    },
ddcat1115's avatar
ddcat1115 committed
190 191
    '/account/settings/notification': {
      component: dynamicWrapper(app, ['geographic'], () => import('../routes/Account/Settings/NotificationView')),
陈帅's avatar
陈帅 committed
192
    },
ddcat1115's avatar
ddcat1115 committed
193 194 195 196 197 198
    // '/user/:id': {
    //   component: dynamicWrapper(app, [], () => import('../routes/User/SomeComponent')),
    // },
  };
  // Get name from ./menu.js or just set it in the router data.
  const menuData = getFlatMenuData(getMenuData());
陈帅's avatar
陈帅 committed
199 200 201

  // Route configuration data
  // eg. {name,authority ...routerConfig }
ddcat1115's avatar
ddcat1115 committed
202
  const routerData = {};
陈帅's avatar
陈帅 committed
203 204 205 206 207
  // The route matches the menu
  Object.keys(routerConfig).forEach((path) => {
    // Regular match item name
    // eg.  router /user/:id === /user/chen
    const pathRegexp = pathToRegexp(path);
jim's avatar
jim committed
208
    const menuKey = Object.keys(menuData).find(key => pathRegexp.test(`${key}`));
陈帅's avatar
陈帅 committed
209 210 211 212 213 214 215 216 217 218 219 220 221
    let menuItem = {};
    // If menuKey is not empty
    if (menuKey) {
      menuItem = menuData[menuKey];
    }
    let router = routerConfig[path];
    // If you need to configure complex parameter routing,
    // https://github.com/ant-design/ant-design-pro-site/blob/master/docs/router-and-nav.md#%E5%B8%A6%E5%8F%82%E6%95%B0%E7%9A%84%E8%B7%AF%E7%94%B1%E8%8F%9C%E5%8D%95
    // eg . /list/:type/user/info/:id
    router = {
      ...router,
      name: router.name || menuItem.name,
      authority: router.authority || menuItem.authority,
222
      hideInBreadcrumb: router.hideInBreadcrumb || menuItem.hideInBreadcrumb,
ddcat1115's avatar
ddcat1115 committed
223
    };
陈帅's avatar
陈帅 committed
224
    routerData[path] = router;
ddcat1115's avatar
ddcat1115 committed
225
  });
ddcat1115's avatar
ddcat1115 committed
226
  return routerData;
ddcat1115's avatar
ddcat1115 committed
227
};