user.js 1.6 KB

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. // 路由配置
  3. const rKey = 'user'; // routerKey,路由前缀变量
  4. const cKey = 'user'; // controllerKey,controller名
  5. const zhKey = '用户';
  6. const routes = [
  7. { method: 'get', path: `/${rKey}`, controller: `${cKey}.index`, name: `${cKey}Query`, zh: `${zhKey}列表查询` },
  8. { method: 'get', path: `/${rKey}/:id`, controller: `${cKey}.show`, name: `${cKey}Show`, zh: `${zhKey}查询` },
  9. { method: 'post', path: `/${rKey}/login`, controller: `${cKey}.login`, name: `${cKey}Login`, zh: `${zhKey}登陆` },
  10. { method: 'post', path: `/${rKey}/resetPwd/:id`, controller: `${cKey}.resetPwd`, name: `${cKey}ResetPwd`, zh: `重置密码${zhKey}` },
  11. { method: 'post', path: `/${rKey}`, controller: `${cKey}.create`, middleware: [ 'password' ], name: `${cKey}Create`, zh: `创建${zhKey}` },
  12. { method: 'post', path: `/${rKey}/:id`, controller: `${cKey}.update`, middleware: [ 'password' ], name: `${cKey}Update`, zh: `修改${zhKey}` },
  13. { method: 'delete', path: `/${rKey}/:id`, controller: `${cKey}.destroy`, name: `${cKey}Delete`, zh: `删除${zhKey}` },
  14. ];
  15. module.exports = app => {
  16. const { router, config } = app;
  17. const mwares = app.middleware;
  18. for (const route of routes) {
  19. const { method, path, controller: ctl, zh } = route;
  20. let { middleware = [] } = route;
  21. if (!method || !path || !ctl) continue;
  22. // 拼全路径
  23. const allPath = `${config.routePrefix}${path}`;
  24. // 处理中间件
  25. if (middleware.length > 0) middleware = middleware.map(i => mwares[i]({ enable: true }));
  26. // 注册路由
  27. router[method](zh, allPath, ...middleware, ctl);
  28. }
  29. };