router.js 11.9 KB
Newer Older
xiaohu's avatar
xiaohu committed
1 2 3 4 5
// TODO:remove
// import { createElement } from 'react';
// import dynamic from 'dva/dynamic';
// import pathToRegexp from 'path-to-regexp';
// import { getMenuData } from './menu';
ddcat1115's avatar
ddcat1115 committed
6

xiaohu's avatar
xiaohu committed
7
// let routerDataCache;
偏右's avatar
偏右 committed
8

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

xiaohu's avatar
xiaohu committed
15 16 17 18 19 20 21 22 23 24 25 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
// // wrapper of dynamic
// 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);
//       }
//     });
//     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,
//           });
//       });
//     },
//   });
// };

// function getFlatMenuData(menus) {
//   let keys = {};
//   menus.forEach(item => {
//     if (item.children) {
//       keys[item.path] = { ...item };
//       keys = { ...keys, ...getFlatMenuData(item.children) };
//     } else {
//       keys[item.path] = { ...item };
//     }
//   });
//   return keys;
// }

// export const getRouterData = app => {
//   const routerConfig = {
//     '/': {
//       component: dynamicWrapper(app, ['user', 'login', 'setting'], () =>
//         import('../layouts/LoadingPage')
//       ),
//     },
//     '/dashboard/analysis': {
//       component: dynamicWrapper(app, ['chart'], () => import('../pages/Dashboard/Analysis')),
//     },
//     '/dashboard/monitor': {
//       component: dynamicWrapper(app, ['monitor'], () => import('../pages/Dashboard/Monitor')),
//     },
//     '/dashboard/workplace': {
//       component: dynamicWrapper(app, ['user', 'project', 'activities', 'chart'], () =>
//         import('../pages/Dashboard/Workplace')
//       ),
//       // hideInBreadcrumb: true,
//       // name: '工作台',
//       // authority: 'admin',
//     },
//     '/form/basic-form': {
//       component: dynamicWrapper(app, ['form'], () => import('../pages/Forms/BasicForm')),
//     },
//     '/form/step-form': {
//       component: dynamicWrapper(app, ['form'], () => import('../pages/Forms/StepForm')),
//     },
//     '/form/step-form/info': {
//       name: '分步表单(填写转账信息)',
//       component: dynamicWrapper(app, ['form'], () => import('../pages/Forms/StepForm/Step1')),
//     },
//     '/form/step-form/confirm': {
//       name: '分步表单(确认转账信息)',
//       component: dynamicWrapper(app, ['form'], () => import('../pages/Forms/StepForm/Step2')),
//     },
//     '/form/step-form/result': {
//       name: '分步表单(完成)',
//       component: dynamicWrapper(app, ['form'], () => import('../pages/Forms/StepForm/Step3')),
//     },
//     '/form/advanced-form': {
//       component: dynamicWrapper(app, ['form'], () => import('../pages/Forms/AdvancedForm')),
//     },
//     '/list/table-list': {
//       component: dynamicWrapper(app, ['rule'], () => import('../pages/List/TableList')),
//     },
//     '/list/basic-list': {
//       component: dynamicWrapper(app, ['list'], () => import('../pages/List/BasicList')),
//     },
//     '/list/card-list': {
//       component: dynamicWrapper(app, ['list'], () => import('../pages/List/CardList')),
//     },
//     '/list/search': {
//       component: dynamicWrapper(app, ['list'], () => import('../pages/List/List')),
//     },
//     '/list/search/projects': {
//       component: dynamicWrapper(app, ['list'], () => import('../pages/List/Projects')),
//     },
//     '/list/search/applications': {
//       component: dynamicWrapper(app, ['list'], () => import('../pages/List/Applications')),
//     },
//     '/list/search/articles': {
//       component: dynamicWrapper(app, ['list'], () => import('../pages/List/Articles')),
//     },
//     '/profile/basic': {
//       component: dynamicWrapper(app, ['profile'], () => import('../pages/Profile/BasicProfile')),
//     },
//     '/profile/advanced': {
//       component: dynamicWrapper(app, ['profile'], () => import('../pages/Profile/AdvancedProfile')),
//     },
//     '/result/success': {
//       component: dynamicWrapper(app, [], () => import('../pages/Result/Success')),
//     },
//     '/result/fail': {
//       component: dynamicWrapper(app, [], () => import('../pages/Result/Error')),
//     },
//     '/exception/403': {
//       component: dynamicWrapper(app, [], () => import('../pages/Exception/403')),
//     },
//     '/exception/404': {
//       component: dynamicWrapper(app, [], () => import('../pages/Exception/404')),
//     },
//     '/exception/500': {
//       component: dynamicWrapper(app, [], () => import('../pages/Exception/500')),
//     },
//     '/exception/trigger': {
//       component: dynamicWrapper(app, ['error'], () =>
//         import('../pages/Exception/triggerException')
//       ),
//     },
//     '/user': {
//       component: dynamicWrapper(app, [], () => import('../layouts/UserLayout')),
//     },
//     '/user/login': {
//       component: dynamicWrapper(app, ['login'], () => import('../pages/User/Login')),
//     },
//     '/user/register': {
//       component: dynamicWrapper(app, ['register'], () => import('../pages/User/Register')),
//     },
//     '/user/register-result': {
//       component: dynamicWrapper(app, [], () => import('../pages/User/RegisterResult')),
//     },
//     '/account/center': {
//       component: dynamicWrapper(app, ['list', 'user', 'project'], () =>
//         import('../pages/Account/Center/Center')
//       ),
//     },
//     '/account/center/articles': {
//       component: dynamicWrapper(app, [], () => import('../pages/Account/Center/Articles')),
//     },
//     '/account/center/applications': {
//       component: dynamicWrapper(app, [], () => import('../pages/Account/Center/Applications')),
//     },
//     '/account/center/projects': {
//       component: dynamicWrapper(app, [], () => import('../pages/Account/Center/Projects')),
//     },
//     '/account/settings': {
//       component: dynamicWrapper(app, ['geographic'], () =>
//         import('../pages/Account/Settings/Info')
//       ),
//     },
//     '/account/settings/base': {
//       component: dynamicWrapper(app, ['geographic'], () =>
//         import('../pages/Account/Settings/BaseView')
//       ),
//     },
//     '/account/settings/security': {
//       component: dynamicWrapper(app, ['geographic'], () =>
//         import('../pages/Account/Settings/SecurityView')
//       ),
//     },
//     '/account/settings/binding': {
//       component: dynamicWrapper(app, ['geographic'], () =>
//         import('../pages/Account/Settings/BindingView')
//       ),
//     },
//     '/account/settings/notification': {
//       component: dynamicWrapper(app, ['geographic'], () =>
//         import('../pages/Account/Settings/NotificationView')
//       ),
//     },
//     // '/user/:id': {
//     //   component: dynamicWrapper(app, [], () => import('../pages/User/SomeComponent')),
//     // },
//   };
//   // Get name from ./menu.js or just set it in the router data.
//   const menuData = getFlatMenuData(getMenuData());

//   // Route configuration data
//   // eg. {name,authority ...routerConfig }
//   const routerData = {};
//   // The route matches the menu
//   Object.keys(routerConfig).forEach(path => {
//     // Regular match item name
//     // eg.  router /user/:id === /user/chen
//     const pathRegexp = pathToRegexp(path);
//     const menuKey = Object.keys(menuData).find(key => pathRegexp.test(`${key}`));
//     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,
//       hideInBreadcrumb: router.hideInBreadcrumb || menuItem.hideInBreadcrumb,
//     };
//     routerData[path] = router;
//   });
//   return routerData;
// };

import pathToRegexp from 'path-to-regexp';
import { getMenuData } from './menu';
ddcat1115's avatar
ddcat1115 committed
249 250 251

function getFlatMenuData(menus) {
  let keys = {};
jim's avatar
jim committed
252
  menus.forEach(item => {
ddcat1115's avatar
ddcat1115 committed
253
    if (item.children) {
ddcat1115's avatar
ddcat1115 committed
254
      keys[item.path] = { ...item };
ddcat1115's avatar
ddcat1115 committed
255 256
      keys = { ...keys, ...getFlatMenuData(item.children) };
    } else {
ddcat1115's avatar
ddcat1115 committed
257
      keys[item.path] = { ...item };
ddcat1115's avatar
ddcat1115 committed
258 259 260 261 262
    }
  });
  return keys;
}

xiaohu's avatar
xiaohu committed
263
export const getRouterData = () => {
ddcat1115's avatar
ddcat1115 committed
264
  const routerConfig = {
ddcat1115's avatar
ddcat1115 committed
265
    '/': {
xiaohu's avatar
xiaohu committed
266
      name: 'Ant-Design-Pro',
ddcat1115's avatar
ddcat1115 committed
267
    },
xiaohu's avatar
xiaohu committed
268 269
    '/Dashboard/Analysis': {
      name: '分析页',
ddcat1115's avatar
ddcat1115 committed
270
    },
xiaohu's avatar
xiaohu committed
271 272 273
    '/Dashboard/Monitor': {},
    '/Dashboard/Workplace': {
      // component: dynamicWrapper(app, ['project', 'activities', 'chart'], () => import('../routes/Dashboard/Workplace')),
ddcat1115's avatar
ddcat1115 committed
274 275
      // hideInBreadcrumb: true,
      // name: '工作台',
ddcat1115's avatar
ddcat1115 committed
276
      // authority: 'admin',
ddcat1115's avatar
ddcat1115 committed
277
    },
xiaohu's avatar
xiaohu committed
278 279 280
    '/Forms/BasicForm': {},
    '/Forms/StepForm': {},
    '/Forms/StepForm/Step1': {
jim's avatar
jim committed
281
      name: '分步表单(填写转账信息)',
WhatAKitty's avatar
WhatAKitty committed
282
    },
xiaohu's avatar
xiaohu committed
283
    '/Forms/StepForm/Step2': {
jim's avatar
jim committed
284
      name: '分步表单(确认转账信息)',
ddcat1115's avatar
ddcat1115 committed
285
    },
xiaohu's avatar
xiaohu committed
286
    '/Forms/StepForm/Step3': {
jim's avatar
jim committed
287
      name: '分步表单(完成)',
陈帅's avatar
陈帅 committed
288
    },
xiaohu's avatar
xiaohu committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    '/Forms/AdvancedForm': {},
    '/List/TableList': {},
    '/List/BasicList': {},
    '/List/CardList': {},
    '/List': {},
    '/List/Search/Projects': {},
    '/List/Search/Applications': {},
    '/List/Search/Articles': {},
    '/Profile/BasicProfile': {},
    '/Profile/AdvancedProfile': {},
    '/Result/success': {},
    '/Result/Error': {},
    '/Exception/403': {},
    '/Exception/404': {},
    '/Exception/500': {},
    '/Exception/triggerException': {},
    '/User': {},
    '/User/Login': {},
    '/User/Register': {},
    '/User/RegisterResult': {},
ddcat1115's avatar
ddcat1115 committed
309
    // '/user/:id': {
xiaohu's avatar
xiaohu committed
310
    //   component: dynamicWrapper(app, [], () => console.log('../routes/User/SomeComponent')),
ddcat1115's avatar
ddcat1115 committed
311 312 313 314
    // },
  };
  // Get name from ./menu.js or just set it in the router data.
  const menuData = getFlatMenuData(getMenuData());
陈帅's avatar
陈帅 committed
315 316 317

  // Route configuration data
  // eg. {name,authority ...routerConfig }
ddcat1115's avatar
ddcat1115 committed
318
  const routerData = {};
陈帅's avatar
陈帅 committed
319
  // The route matches the menu
jim's avatar
jim committed
320
  Object.keys(routerConfig).forEach(path => {
陈帅's avatar
陈帅 committed
321 322 323
    // Regular match item name
    // eg.  router /user/:id === /user/chen
    const pathRegexp = pathToRegexp(path);
jim's avatar
jim committed
324
    const menuKey = Object.keys(menuData).find(key => pathRegexp.test(`${key}`));
陈帅's avatar
陈帅 committed
325 326 327 328 329 330 331 332 333 334 335 336 337
    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,
ddcat1115's avatar
ddcat1115 committed
338
    };
陈帅's avatar
陈帅 committed
339
    routerData[path] = router;
ddcat1115's avatar
ddcat1115 committed
340
  });
xiaohu's avatar
xiaohu committed
341

ddcat1115's avatar
ddcat1115 committed
342
  return routerData;
ddcat1115's avatar
ddcat1115 committed
343
};