template_controller.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const path = require('path');
  10. const fs = require('fs');
  11. module.exports = app => {
  12. class TemplateController extends app.BaseController {
  13. /**
  14. * 下载 各种模板
  15. * @param ctx
  16. * @returns {Promise<void>}
  17. */
  18. async download(ctx) {
  19. const file = ctx.params.file;
  20. if (file) {
  21. try {
  22. let fileName;
  23. switch (file) {
  24. case '导入分项清单EXCEL格式.xlsx':
  25. fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', 'ledger', '导入分项清单EXCEL格式.xlsx');
  26. break;
  27. case '导入工程量清单EXCEL格式.xls':
  28. fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', 'ledger', '导入工程量清单EXCEL格式.xls');
  29. break;
  30. default:
  31. throw '参数错误'
  32. }
  33. ctx.body = await fs.readFileSync(fileName);
  34. } catch (err) {
  35. this.log(err);
  36. if (err.stack) {
  37. ctx.body = '您下载的示例文件不存在';
  38. } else {
  39. ctx.body = err;
  40. }
  41. }
  42. } else {
  43. ctx.body = '参数错误';
  44. }
  45. }
  46. }
  47. return TemplateController;
  48. };