/** * Created by Tony on 2017/3/13. */ let fsUtil = require("../../public/fsUtil"); const rpt_pdf_util = require("../output/rpt_pdf_util"); const fs = require('fs') ; //统一回调函数 let callback = function(req, res, err, data){ if(err){ res.json({success: false, error: err}); } else{ //res.send({success: true, data: data}); res.json({success:true, data: data}); } }; module.exports = { restfulTestResponse: function (req, res) { console.log('you are calling restfulTestResponse.'); callback(req, res, null, 'good!'); }, restfulTestResponsePost: function (req, res) { console.log('you are calling restfulTestResponse by POST way.'); callback(req, res, null, 'good!'); }, createExcelFilesInOneBookEx: function (req, res) { //采用了优化策略 let params = JSON.parse(req.body.params), rptPageRstArray = params.rptPageRstArray, uuid = params.uuid ; // if (rpt_ids && rpt_ids.length > 0) { // getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_NORMAL, function (err, rptPageRstArray) { // if(err){ // console.log('导出Excel错误(生成数据过程错误), userId: ' + user_id + ', prjId' + prj_id); // callback(req, res, '数据有误', null); // } else { // if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) { // getSummaryComboPages(user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) { // rpt_xl_util.exportExcelInOneBook(rptPageRstArray.concat(rptSumPageRstArray), pageSize, rptName, function(uuidName){ // let fileRst = {uuid: uuidName, reportName: rptName}; // callback(req, res, err, fileRst); // }); // }); // } else { // rpt_xl_util.exportExcelInOneBook(rptPageRstArray, pageSize, rptName, function(uuidName){ // let fileRst = {uuid: uuidName, reportName: rptName}; // callback(req, res, err, fileRst); // }); // } // } // }); // } }, createExcelFilesEx: function (req, res) { let params = JSON.parse(req.body.params), pageSize = params.pageSize, isOneSheet = params.isOneSheet, rptPageRstArray = params.rptPageRstArray ; // if (rpt_ids && rpt_ids.length > 0) { // getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_NORMAL, function (err, rptPageRstArray) { // if (err) { // console.log('导出Excel错误(生成数据过程错误), userId: ' + user_id + ', prjId' + prj_id); // callback(req, res, '数据生成错误', null); // } else { // if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) { // getSummaryComboPages(user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) { // let parallelFunctions = []; // pri_Add_Parallel_Functions(parallelFunctions, rptPageRstArray, rpt_names, err); // pri_Add_Parallel_Functions(parallelFunctions, rptSumPageRstArray, sum_rpt_names, err); // pri_Run_Parallel_Functions(parallelFunctions); // }); // } else { // let parallelFunctions = []; // pri_Add_Parallel_Functions(parallelFunctions, rptPageRstArray, rpt_names, err); // pri_Run_Parallel_Functions(parallelFunctions); // } // } // }); // } else if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) { // getSummaryComboPages(user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) { // let parallelFunctions = []; // pri_Add_Parallel_Functions(parallelFunctions, rptSumPageRstArray, sum_rpt_names, err); // pri_Run_Parallel_Functions(parallelFunctions); // }); // } }, createPdfFilesEx: function (req, res) { let params = JSON.parse(req.body.params), pageSize = params.pageSize, rptPageRstArray = params.rptPageRstArray, uuidArray = params.uuids ; // console.log('start!'); let msg = 'sent the export request!'; let err = null; if (rptPageRstArray instanceof Array && rptPageRstArray.length > 0) { for (let rIdx = 0; rIdx < rptPageRstArray.length; rIdx++) { try { rpt_pdf_util.export_pdf_file(rptPageRstArray[rIdx], pageSize, uuidArray[rIdx], null); // 创建线程来实现build PDF,但无call back } catch (ex) { if (err) { err = err + 'Has error on: ' + rIdx + ' '; } else { err = 'Has error on: ' + rIdx + ' '; } } } } callback(req, res, err, msg); }, getFileByUUID: function (req, res) { let uuid = req.params.uuid, rptName = req.params.rptName, suffix = "." + req.params.suffix ; // let user_id = req.session.sessionUser.id; //未来要校验user id try { res.setHeader('Content-Type', 'application/vnd.openxmlformats'); let rptNameURI = encodeURI(rptName); res.setHeader("Content-Disposition", "attachment; filename=\"" + rptNameURI + suffix + "\"; filename*=utf-8''" + rptNameURI + suffix ); let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + uuid + suffix); filestream.on('data', function(chunk) { res.write(chunk); }); filestream.on('end', function() { res.end(); }); } catch (e) { console.log(e); } }, };