mock.router.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. const Router = require('@koa/router');
  2. const router = new Router({ prefix: '/firefly' });
  3. const util = require('../utils/util');
  4. const fireflyService = require('../service/firefly.service');
  5. /**
  6. * mock服务接口
  7. * 此接口主要用于线上展示CRUD功能
  8. */
  9. /**
  10. * 数据列表
  11. */
  12. router.get('/list', async (ctx) => {
  13. const { pageNum, pageSize, name, status } = ctx.request.query;
  14. if (!pageNum || !pageSize) {
  15. util.fail(ctx, '分页参数不能为空');
  16. return;
  17. }
  18. if (!util.isNumber(pageNum) || !util.isNumber(pageSize)) {
  19. util.fail(ctx, '分页参数格式错误');
  20. return;
  21. }
  22. const list = await fireflyService.list(pageNum, pageSize, name, status);
  23. const { total } = await fireflyService.listCount(name, status);
  24. util.success(ctx, {
  25. list,
  26. total,
  27. pageSize: +pageSize,
  28. pageNum: +pageNum,
  29. });
  30. });
  31. /**
  32. * 删除数据
  33. */
  34. router.post('/delete', async (ctx) => {
  35. const { id } = ctx.request.query;
  36. if (!id || !util.isNumber(id)) {
  37. util.fail(ctx, 'ID参数错误');
  38. return;
  39. }
  40. await fireflyService.deleteById(id);
  41. util.success(ctx, '删除成功');
  42. });
  43. /**
  44. * 新增数据
  45. */
  46. router.post('/create', async (ctx) => {
  47. const { name, type, avatar, time, skill, sales, status, area } = ctx.request.body;
  48. if (!name) {
  49. util.fail(ctx, 'name参数不能为空');
  50. return;
  51. }
  52. if (!type) {
  53. util.fail(ctx, 'type参数不能为空');
  54. return;
  55. }
  56. if (!time) {
  57. util.fail(ctx, 'time参数不能为空');
  58. return;
  59. }
  60. if (!skill || skill.length === 0) {
  61. util.fail(ctx, 'skill参数不能为空');
  62. return;
  63. }
  64. if (!sales) {
  65. util.fail(ctx, 'sales参数不能为空');
  66. return;
  67. }
  68. if (!status) {
  69. util.fail(ctx, 'status参数不能为空');
  70. return;
  71. }
  72. if (!area) {
  73. util.fail(ctx, 'area参数不能为空');
  74. return;
  75. }
  76. await fireflyService.create({ ...ctx.request.body, skill: skill.join(',') });
  77. util.success(ctx, '新增成功');
  78. });
  79. /**
  80. * 更新数据
  81. */
  82. router.post('/update', async (ctx) => {
  83. const { id, skill } = ctx.request.body;
  84. if (!id || !util.isNumber(id)) {
  85. util.fail(ctx, 'ID参数错误');
  86. return;
  87. }
  88. await fireflyService.update({ ...ctx.request.body, skill: skill.join(',') });
  89. util.success(ctx, '更新成功');
  90. });
  91. /**
  92. * 测试文件下载
  93. */
  94. router.post('/download', (ctx) => {
  95. // 这里我们模拟一个CSV文件的内容
  96. const csvData = `Name,Email,Phone
  97. John Doe,john@example.com,123-456-7890
  98. Jane Smith,jane@example.com,987-654-3210`;
  99. // 设置响应头
  100. ctx.set('Content-Type', 'text/csv');
  101. ctx.set('Content-Disposition', 'attachment; filename=example.csv');
  102. // 发送文件内容
  103. ctx.body = csvData;
  104. });
  105. module.exports = router;