user.js 1.2 KB

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