'use strict'; /** * * * @author Mai * @date * @version */ const path = require('path'); const fs = require('fs'); module.exports = app => { class TemplateController extends app.BaseController { /** * 下载 各种模板 * @param ctx * @returns {Promise} */ async download(ctx) { const file = ctx.params.file; if (file) { try { let fileName; switch (file) { case '导入分项清单EXCEL格式.xlsx': fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', 'ledger', '导入分项清单EXCEL格式.xlsx'); break; case '导入工程量清单EXCEL格式.xls': fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', 'ledger', '导入工程量清单EXCEL格式.xls'); break; case '估概预算示例EXCEL格式.xlsx': fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', file); break; case '控制价示例EXCEL格式.xlsx': fileName = path.join(this.app.baseDir, 'app', 'public', 'files', 'template', file); break; default: throw '参数错误' } ctx.body = await fs.readFileSync(fileName); } catch (err) { this.log(err); if (err.stack) { ctx.body = '您下载的示例文件不存在'; } else { ctx.body = err; } } } else { ctx.body = '参数错误'; } } async posCalc(ctx) { try { // if (!ctx.subProject.page_show.posCalc) throw '该功能已关闭'; const renderData = { validColInfo: ctx.service.calcTmpl.TemplateRela.posCalc.ValidColInfo, jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.template.posCalc), }; renderData.templateList = await ctx.service.calcTmpl.getAllTemplate(ctx.tender.id, 'posCalc'); await this.layout('template/pos_calc.ejs', renderData, 'template/preview_modal.ejs'); } catch (err) { ctx.log(err); ctx.postError(err, '查看模板数据错误'); ctx.redirect(ctx.request.header.referer); } } // ------------ 以下方法为所有模板共用 -------------- async load(ctx) { try { const data = JSON.parse(ctx.request.body.data); const filter = data.filter ? data.filter.split(';') : []; const result = {}; for (const f of filter) { switch(f) { case 'detail': result[f] = await this.ctx.service.calcTmpl.getTemplate(data.id, data.type); break; default: throw '未知数据类型'; } } ctx.body = { err: 0, msg: '', data: result }; } catch (error) { ctx.log(error); ctx.ajaxErrorBody(error, '加载数据失败'); } } async saveTemplate(ctx) { try { const data = JSON.parse(ctx.request.body.data); const result = await ctx.service.calcTmpl.saveTemplate(data); ctx.body = { err: 0, msg: '', data: result }; } catch (err) { ctx.log(err); ctx.ajaxErrorBody(err, '修改数据失败'); } } async preview(ctx) { try { const data = JSON.parse(ctx.request.body.data); const spreadSetting = this.ctx.service.calcTmpl.calcSpreadCache(data.col_set, data.type); const testData = this.ctx.service.calcTmpl.getCalcTestData(data.col_set, data.type); ctx.body = { err: 0, msg: '', data: { spreadSetting, testData} }; } catch (error) { ctx.log(error); ctx.ajaxErrorBody(error, '预览失败'); } } // --------------------------------------------------- } return TemplateController; };