table.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. // 测试接口
  3. const rKey = 'table'; // routerKey,路由前缀变量
  4. const cKey = 'table'; // controllerKey,controller名
  5. const zhKey = '桌台表';
  6. const routes = [
  7. {
  8. method: 'get',
  9. path: `/${rKey}`,
  10. controller: `${cKey}.index`,
  11. name: `${cKey}Query`,
  12. zh: `${zhKey}列表查询`,
  13. },
  14. {
  15. method: 'get',
  16. path: `/${rKey}/:id`,
  17. controller: `${cKey}.show`,
  18. name: `${cKey}Show`,
  19. zh: `${zhKey}查询`,
  20. },
  21. {
  22. method: 'post',
  23. path: `/${rKey}`,
  24. controller: `${cKey}.create`,
  25. name: `${cKey}Create`,
  26. zh: `创建${zhKey}`,
  27. },
  28. {
  29. method: 'post',
  30. path: `/${rKey}/:id`,
  31. controller: `${cKey}.update`,
  32. name: `${cKey}Update`,
  33. zh: `修改${zhKey}`,
  34. },
  35. {
  36. method: 'delete',
  37. path: `/${rKey}/:id`,
  38. controller: `${cKey}.destroy`,
  39. name: `${cKey}Delete`,
  40. zh: `删除${zhKey}`,
  41. },
  42. ];
  43. module.exports = app => {
  44. const { router, config } = app;
  45. const mwares = app.middleware;
  46. for (const route of routes) {
  47. const { method, path, controller: ctl, zh } = route;
  48. let { middleware = [] } = route;
  49. if (!method || !path || !ctl) continue;
  50. // 拼全路径
  51. const allPath = `${config.routePrefix}${path}`;
  52. // 处理中间件
  53. if (middleware.length > 0) middleware = middleware.map(i => mwares[i]());
  54. // 注册路由
  55. router[method](zh, allPath, ...middleware, ctl);
  56. }
  57. };