template_controller.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. const sendToWormhole = require('stream-wormhole');
  12. module.exports = app => {
  13. class TemplateController extends app.BaseController {
  14. /**
  15. * 下载 各种模板
  16. * @param ctx
  17. * @returns {Promise<void>}
  18. */
  19. async download(ctx) {
  20. const file = ctx.params.file;
  21. if (file) {
  22. try {
  23. let fileName;
  24. switch (file) {
  25. case '导入分项清单EXCEL格式.xlsx':
  26. fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', 'ledger', '导入分项清单EXCEL格式.xlsx');
  27. break;
  28. case '导入工程量清单EXCEL格式.xls':
  29. fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', 'ledger', '导入工程量清单EXCEL格式.xls');
  30. break;
  31. case '估概预算示例EXCEL格式.xlsx':
  32. fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', file);
  33. break;
  34. case '控制价示例EXCEL格式.xlsx':
  35. fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', file);
  36. break;
  37. default:
  38. throw '参数错误'
  39. }
  40. ctx.body = await fs.readFileSync(fileName);
  41. } catch (err) {
  42. this.log(err);
  43. if (err.stack) {
  44. ctx.body = '您下载的示例文件不存在';
  45. } else {
  46. ctx.body = err;
  47. }
  48. }
  49. } else {
  50. ctx.body = '参数错误';
  51. }
  52. }
  53. async posCalc(ctx) {
  54. try {
  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.session.sessionProject.id, 'posCalc');
  60. await ctx.service.calcTmpl.checkTemplateUsed(renderData.templateList, 'posCalc');
  61. const specList = await ctx.service.stdExtraList.getList(0);
  62. renderData.specList = specList.map(x => { return { value: x.id, text: x.name }; });
  63. await this.layout('template/pos_calc.ejs', renderData, 'template/pos_calc_modal.ejs');
  64. } catch (err) {
  65. ctx.log(err);
  66. ctx.postError(err, '查看模板数据错误');
  67. ctx.redirect(ctx.request.header.referer);
  68. }
  69. }
  70. async cost(ctx) {
  71. try {
  72. const renderData = {
  73. validColInfo: ctx.service.calcTmpl.TemplateRela.cost.ValidColInfo,
  74. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.template.cost),
  75. };
  76. renderData.templateList = await ctx.service.calcTmpl.getAllTemplate(ctx.session.sessionProject.id, 'cost');
  77. await ctx.service.calcTmpl.checkTemplateUsed(renderData.templateList, 'cost');
  78. await this.layout('template/cost.ejs', renderData, 'template/cost_modal.ejs');
  79. } catch (err) {
  80. ctx.log(err);
  81. ctx.postError(err, '查看模板数据错误');
  82. ctx.redirect(ctx.request.header.referer);
  83. }
  84. }
  85. // ------------ 以下方法为所有模板共用 --------------
  86. async load(ctx) {
  87. try {
  88. const data = JSON.parse(ctx.request.body.data);
  89. const filter = data.filter ? data.filter.split(';') : [];
  90. const result = {};
  91. for (const f of filter) {
  92. switch(f) {
  93. case 'detail':
  94. result[f] = await this.ctx.service.calcTmpl.getTemplate(data.id, data.type);
  95. break;
  96. default:
  97. throw '未知数据类型';
  98. }
  99. }
  100. ctx.body = { err: 0, msg: '', data: result };
  101. } catch (error) {
  102. ctx.log(error);
  103. ctx.ajaxErrorBody(error, '加载数据失败');
  104. }
  105. }
  106. async saveTemplate(ctx) {
  107. try {
  108. const data = JSON.parse(ctx.request.body.data);
  109. const result = await ctx.service.calcTmpl.saveTemplate(data);
  110. ctx.body = { err: 0, msg: '', data: result };
  111. } catch (err) {
  112. ctx.log(err);
  113. ctx.ajaxErrorBody(err, '修改数据失败');
  114. }
  115. }
  116. async preview(ctx) {
  117. try {
  118. const data = JSON.parse(ctx.request.body.data);
  119. const spreadSetting = this.ctx.service.calcTmpl.calcSpreadCache(data.type, data.col_set, data.multi_header);
  120. const testData = await this.ctx.service.calcTmpl.getCalcTestData(data.col_set, data.type, 3);
  121. const calc = this.ctx.service.calcTmpl._getCalcExpr(spreadSetting.cols);
  122. ctx.body = { err: 0, msg: '', data: { spreadSetting, testData, calc } };
  123. } catch (error) {
  124. ctx.log(error);
  125. ctx.ajaxErrorBody(error, '预览失败');
  126. }
  127. }
  128. async reCalcTemplate(ctx) {
  129. try {
  130. const templateId = ctx.query.tid;
  131. await ctx.service.calcTmpl.reCalcTemplate(templateId, true);
  132. } catch(error) {
  133. ctx.log(error);
  134. ctx.postError(error, '重算模板失败');
  135. }
  136. ctx.redirect(ctx.request.header.referer);
  137. }
  138. async exportTemplate(ctx) {
  139. try {
  140. const template = await ctx.service.calcTmpl.getTemplate(ctx.query.tid);
  141. if (!template) throw '计算模板不存在';
  142. if (template.project_id !== ctx.session.sessionProject.id) throw '您无权导出该模板数据';
  143. const Cpd = require('../lib/crypt').cpd;
  144. const cpd = new Cpd();
  145. const filename = `${template.name}.ctd`;
  146. const filepath = path.join(this.ctx.app.baseDir, 'temp', filename);
  147. await cpd.encrypt({ type: template.type, col_set: template.col_set, multi_header: template.multi_header }, filepath);
  148. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  149. let disposition = '';
  150. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  151. disposition = 'attachment; filename=' + encodeURIComponent(filename);
  152. } else if (userAgent.indexOf('firefox') >= 0) {
  153. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(filename) + '"';
  154. } else {
  155. /* safari等其他非主流浏览器只能自求多福了 */
  156. disposition = 'attachment; filename=' + new Buffer(filename).toString('binary');
  157. }
  158. ctx.response.set({
  159. 'Content-Type': 'application/octet-stream',
  160. 'Content-Disposition': disposition,
  161. });
  162. ctx.body = await fs.readFileSync(filepath);
  163. } catch(err) {
  164. ctx.log(err);
  165. ctx.postError(err, '导出模板数据失败');
  166. }
  167. }
  168. async importTemplate(ctx) {
  169. const template = await ctx.service.calcTmpl.getTemplate(ctx.query.tid);
  170. if (!template) throw '计算模板不存在';
  171. const create_time = Date.parse(new Date()) / 1000;
  172. const filepath = path.join(this.ctx.app.baseDir, 'temp', `计算模板${create_time}.ctd`);
  173. let stream, index = 0;
  174. try {
  175. const parts = ctx.multipart({ autoFields: true });
  176. stream = await parts();
  177. while (stream !== undefined) {
  178. if (!stream.filename) throw '未发现上传文件!';
  179. // 保存文件
  180. await ctx.helper.saveStreamFile(stream, filepath);
  181. await sendToWormhole(stream);
  182. ++index;
  183. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  184. stream = await parts();
  185. } else {
  186. stream = undefined;
  187. }
  188. }
  189. const Cpd = require('../lib/crypt').cpd;
  190. const cpd = new Cpd();
  191. const data = await cpd.decrypt(filepath);
  192. if (template.type !== data.type) throw '导入的模板文件与当前模板类型不一致,不可导入';
  193. const result =await ctx.service.calcTmpl.saveTemplate({ update: { id: template.id, col_set: data.col_set, multi_header: data.multi_header } });
  194. ctx.body = { err: 0, mgs: '', data: result };
  195. } catch (err) {
  196. // 失败需要消耗掉stream 以防卡死
  197. if (stream) await sendToWormhole(stream);
  198. ctx.log(err);
  199. ctx.ajaxErrorBody(err, '导入模板文件失败');
  200. }
  201. }
  202. // ---------------------------------------------------
  203. async saveLedgerTemplate(ctx) {
  204. try {
  205. const data = JSON.parse(ctx.request.body.data);
  206. const result = await ctx.service.ledgerTemplate.saveTemplate(data);
  207. ctx.body = { err: 0, msg: '', data: result };
  208. } catch (err) {
  209. ctx.log(err);
  210. ctx.ajaxErrorBody(err, '修改数据失败');
  211. }
  212. }
  213. }
  214. return TemplateController;
  215. };