123456789101112131415161718192021222324252627282930 |
- 'use strict';
- // 路由配置
- const rKey = 'user'; // routerKey,路由前缀变量
- const cKey = 'user'; // controllerKey,controller名
- const zhKey = '用户';
- const routes = [
- { method: 'get', path: `/${rKey}`, controller: `${cKey}.index`, name: `${cKey}Query`, zh: `${zhKey}列表查询` },
- { method: 'get', path: `/${rKey}/:id`, controller: `${cKey}.show`, name: `${cKey}Show`, zh: `${zhKey}查询` },
- { method: 'post', path: `/${rKey}/login`, controller: `${cKey}.login`, name: `${cKey}Login`, zh: `${zhKey}登陆` },
- { method: 'post', path: `/${rKey}/resetPwd/:id`, controller: `${cKey}.resetPwd`, name: `${cKey}ResetPwd`, zh: `重置密码${zhKey}` },
- { method: 'post', path: `/${rKey}`, controller: `${cKey}.create`, middleware: [ 'password' ], name: `${cKey}Create`, zh: `创建${zhKey}` },
- { method: 'post', path: `/${rKey}/:id`, controller: `${cKey}.update`, middleware: [ 'password' ], name: `${cKey}Update`, zh: `修改${zhKey}` },
- { method: 'delete', path: `/${rKey}/:id`, controller: `${cKey}.destroy`, name: `${cKey}Delete`, zh: `删除${zhKey}` },
- ];
- module.exports = app => {
- const { router, config } = app;
- const mwares = app.middleware;
- for (const route of routes) {
- const { method, path, controller: ctl, zh } = route;
- let { middleware = [] } = route;
- if (!method || !path || !ctl) continue;
- // 拼全路径
- const allPath = `${config.routePrefix}${path}`;
- // 处理中间件
- if (middleware.length > 0) middleware = middleware.map(i => mwares[i]({ enable: true }));
- // 注册路由
- router[method](zh, allPath, ...middleware, ctl);
- }
- };
|