router.js 8.29 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
let routerDataCache;

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

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

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

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

  // Route configuration data
  // eg. {name,authority ...routerConfig }
ddcat1115's avatar
ddcat1115 committed
219
  const routerData = {};
陈帅's avatar
陈帅 committed
220
  // The route matches the menu
jim's avatar
jim committed
221
  Object.keys(routerConfig).forEach(path => {
陈帅's avatar
陈帅 committed
222 223 224
    // Regular match item name
    // eg.  router /user/:id === /user/chen
    const pathRegexp = pathToRegexp(path);
jim's avatar
jim committed
225
    const menuKey = Object.keys(menuData).find(key => pathRegexp.test(`${key}`));
陈帅's avatar
陈帅 committed
226 227 228 229 230 231 232 233 234 235 236 237 238
    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,
239
      hideInBreadcrumb: router.hideInBreadcrumb || menuItem.hideInBreadcrumb,
ddcat1115's avatar
ddcat1115 committed
240
    };
陈帅's avatar
陈帅 committed
241
    routerData[path] = router;
ddcat1115's avatar
ddcat1115 committed
242
  });
ddcat1115's avatar
ddcat1115 committed
243
  return routerData;
ddcat1115's avatar
ddcat1115 committed
244
};