personnel.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const Personnel = require('../module/Personnel');
  2. const Router = require('koa-router');
  3. const permission = require('../intercept/permission');
  4. const router = new Router();
  5. /**
  6. * 获取案例数据
  7. */
  8. router.get('/', async ctx => {
  9. let personnel = new Personnel();
  10. personnel.hide = 0;
  11. for (let key in ctx.request.query) {
  12. personnel[key] = ctx.request.query[key]
  13. }
  14. let result = await personnel.query()
  15. ctx.body = {
  16. msg: '成功获取数据',
  17. content: result
  18. }
  19. });
  20. /**
  21. * 添加案例
  22. */
  23. router.post('/', permission, async ctx => {
  24. let body = ctx.request.body;
  25. let personnel = new Personnel();
  26. personnel.type = body.type;
  27. personnel.address = body.address;
  28. personnel.count = body.count;
  29. personnel.time = Date.now().toString();
  30. personnel.describe = body.describe;
  31. personnel.requirement = body.requirement;
  32. personnel.bonus = body.bonus;
  33. personnel.title = body.title;
  34. personnel.language = body.language;
  35. let { insertId } = await personnel.insert()
  36. ctx.body = {
  37. msg: '成功添加!',
  38. insertId
  39. };
  40. });
  41. /**
  42. * 修改案例
  43. */
  44. router.put('/', permission, async ctx => {
  45. let body = ctx.request.body;
  46. let personnel = new Personnel();
  47. personnel.id = body.id;
  48. if (!await personnel.exists()) {
  49. ctx.error('该招聘不存在!')
  50. }
  51. personnel.type = body.type;
  52. personnel.address = body.address;
  53. personnel.count = body.count;
  54. personnel.describe = body.describe;
  55. personnel.requirement = body.requirement;
  56. personnel.bonus = body.bonus;
  57. personnel.title = body.title;
  58. personnel.language = body.language;
  59. await personnel.update()
  60. ctx.body = {
  61. msg: '成功修改!'
  62. };
  63. });
  64. /**
  65. * 删除案例
  66. */
  67. router.delete('/:id', permission, async ctx => {
  68. let id = ctx.params.id;
  69. (!id || !Number.isFinite(Number(id))) &&
  70. ctx.error('该案例不存在,请刷新后重试!');
  71. let personnel = new Personnel();
  72. personnel.id = id;
  73. personnel.hide = 0;
  74. (await personnel.exists()) ||
  75. ctx.error('该案例不存在,请刷新后重试!');
  76. personnel.hide = 1;
  77. await personnel.update();
  78. ctx.body = { msg: '成功删除!' };
  79. });
  80. module.exports = exports = { path: '/personnel', router };