template_controller.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. case '估概预算示例EXCEL格式.xlsx':
  31. fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', file);
  32. break;
  33. case '控制价示例EXCEL格式.xlsx':
  34. fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', file);
  35. break;
  36. default:
  37. throw '参数错误'
  38. }
  39. ctx.body = await fs.readFileSync(fileName);
  40. } catch (err) {
  41. this.log(err);
  42. if (err.stack) {
  43. ctx.body = '您下载的示例文件不存在';
  44. } else {
  45. ctx.body = err;
  46. }
  47. }
  48. } else {
  49. ctx.body = '参数错误';
  50. }
  51. }
  52. async posCalc(ctx) {
  53. try {
  54. // if (!ctx.subProject.page_show.posCalc) throw '该功能已关闭';
  55. const renderData = {
  56. validColInfo: ctx.service.calcTmpl.TemplateRela.posCalc.ValidColInfo,
  57. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.template.posCalc),
  58. };
  59. renderData.templateList = await ctx.service.calcTmpl.getAllTemplate(ctx.tender.id, 'posCalc');
  60. await this.layout('template/pos_calc.ejs', renderData, 'template/preview_modal.ejs');
  61. } catch (err) {
  62. ctx.log(err);
  63. ctx.postError(err, '查看模板数据错误');
  64. ctx.redirect(ctx.request.header.referer);
  65. }
  66. }
  67. // ------------ 以下方法为所有模板共用 --------------
  68. async load(ctx) {
  69. try {
  70. const data = JSON.parse(ctx.request.body.data);
  71. const filter = data.filter ? data.filter.split(';') : [];
  72. const result = {};
  73. for (const f of filter) {
  74. switch(f) {
  75. case 'detail':
  76. result[f] = await this.ctx.service.calcTmpl.getTemplate(data.id, data.type);
  77. break;
  78. default:
  79. throw '未知数据类型';
  80. }
  81. }
  82. ctx.body = { err: 0, msg: '', data: result };
  83. } catch (error) {
  84. ctx.log(error);
  85. ctx.ajaxErrorBody(error, '加载数据失败');
  86. }
  87. }
  88. async saveTemplate(ctx) {
  89. try {
  90. const data = JSON.parse(ctx.request.body.data);
  91. const result = await ctx.service.calcTmpl.saveTemplate(data);
  92. ctx.body = { err: 0, msg: '', data: result };
  93. } catch (err) {
  94. ctx.log(err);
  95. ctx.ajaxErrorBody(err, '修改数据失败');
  96. }
  97. }
  98. async preview(ctx) {
  99. try {
  100. const data = JSON.parse(ctx.request.body.data);
  101. const spreadSetting = this.ctx.service.calcTmpl.calcSpreadCache(data.col_set, data.type);
  102. const testData = this.ctx.service.calcTmpl.getCalcTestData(data.col_set, data.type);
  103. ctx.body = { err: 0, msg: '', data: { spreadSetting, testData} };
  104. } catch (error) {
  105. ctx.log(error);
  106. ctx.ajaxErrorBody(error, '预览失败');
  107. }
  108. }
  109. // ---------------------------------------------------
  110. }
  111. return TemplateController;
  112. };