admin.js 1.5 KB

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