rpt_controller.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Created by Tony on 2017/3/13.
  3. */
  4. let fsUtil = require("../../public/fsUtil");
  5. const rpt_pdf_util = require("../output/rpt_pdf_util");
  6. const fs = require('fs') ;
  7. //统一回调函数
  8. let callback = function(req, res, err, data){
  9. if(err){
  10. res.json({success: false, error: err});
  11. }
  12. else{
  13. //res.send({success: true, data: data});
  14. res.json({success:true, data: data});
  15. }
  16. };
  17. module.exports = {
  18. restfulTestResponse: function (req, res) {
  19. console.log('you are calling restfulTestResponse.');
  20. callback(req, res, null, 'good!');
  21. },
  22. restfulTestResponsePost: function (req, res) {
  23. console.log('you are calling restfulTestResponse by POST way.');
  24. callback(req, res, null, 'good!');
  25. },
  26. createExcelFilesInOneBookEx: function (req, res) {
  27. //采用了优化策略
  28. let params = JSON.parse(req.body.params),
  29. rptPageRstArray = params.rptPageRstArray,
  30. uuid = params.uuid
  31. ;
  32. // if (rpt_ids && rpt_ids.length > 0) {
  33. // getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_NORMAL, function (err, rptPageRstArray) {
  34. // if(err){
  35. // console.log('导出Excel错误(生成数据过程错误), userId: ' + user_id + ', prjId' + prj_id);
  36. // callback(req, res, '数据有误', null);
  37. // } else {
  38. // if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  39. // getSummaryComboPages(user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  40. // rpt_xl_util.exportExcelInOneBook(rptPageRstArray.concat(rptSumPageRstArray), pageSize, rptName, function(uuidName){
  41. // let fileRst = {uuid: uuidName, reportName: rptName};
  42. // callback(req, res, err, fileRst);
  43. // });
  44. // });
  45. // } else {
  46. // rpt_xl_util.exportExcelInOneBook(rptPageRstArray, pageSize, rptName, function(uuidName){
  47. // let fileRst = {uuid: uuidName, reportName: rptName};
  48. // callback(req, res, err, fileRst);
  49. // });
  50. // }
  51. // }
  52. // });
  53. // }
  54. },
  55. createExcelFilesEx: function (req, res) {
  56. let params = JSON.parse(req.body.params),
  57. pageSize = params.pageSize,
  58. isOneSheet = params.isOneSheet,
  59. rptPageRstArray = params.rptPageRstArray
  60. ;
  61. // if (rpt_ids && rpt_ids.length > 0) {
  62. // getMultiRptsCommon(user_id, prj_id, rpt_ids, pageSize, orientation, customizeCfg, dftOption, JV.OUTPUT_TYPE_NORMAL, function (err, rptPageRstArray) {
  63. // if (err) {
  64. // console.log('导出Excel错误(生成数据过程错误), userId: ' + user_id + ', prjId' + prj_id);
  65. // callback(req, res, '数据生成错误', null);
  66. // } else {
  67. // if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  68. // getSummaryComboPages(user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  69. // let parallelFunctions = [];
  70. // pri_Add_Parallel_Functions(parallelFunctions, rptPageRstArray, rpt_names, err);
  71. // pri_Add_Parallel_Functions(parallelFunctions, rptSumPageRstArray, sum_rpt_names, err);
  72. // pri_Run_Parallel_Functions(parallelFunctions);
  73. // });
  74. // } else {
  75. // let parallelFunctions = [];
  76. // pri_Add_Parallel_Functions(parallelFunctions, rptPageRstArray, rpt_names, err);
  77. // pri_Run_Parallel_Functions(parallelFunctions);
  78. // }
  79. // }
  80. // });
  81. // } else if ((rpt_bill_tpl_ids && rpt_bill_tpl_ids.length > 0) || (rpt_glj_tpl_ids && rpt_glj_tpl_ids.length > 0)) {
  82. // getSummaryComboPages(user_id, prjIds, rpt_bill_tpl_ids, rpt_glj_tpl_ids, pageSize, orientation, customizeCfg, option, JV.OUTPUT_TYPE_NORMAL, function (err, rptSumPageRstArray) {
  83. // let parallelFunctions = [];
  84. // pri_Add_Parallel_Functions(parallelFunctions, rptSumPageRstArray, sum_rpt_names, err);
  85. // pri_Run_Parallel_Functions(parallelFunctions);
  86. // });
  87. // }
  88. },
  89. createPdfFilesEx: function (req, res) {
  90. let params = JSON.parse(req.body.params),
  91. pageSize = params.pageSize,
  92. rptPageRstArray = params.rptPageRstArray,
  93. uuidArray = params.uuids
  94. ;
  95. // console.log('start!');
  96. let msg = 'sent the export request!';
  97. let err = null;
  98. if (rptPageRstArray instanceof Array && rptPageRstArray.length > 0) {
  99. for (let rIdx = 0; rIdx < rptPageRstArray.length; rIdx++) {
  100. try {
  101. rpt_pdf_util.export_pdf_file(rptPageRstArray[rIdx], pageSize, uuidArray[rIdx], null); // 创建线程来实现build PDF,但无call back
  102. } catch (ex) {
  103. if (err) {
  104. err = err + 'Has error on: ' + rIdx + ' ';
  105. } else {
  106. err = 'Has error on: ' + rIdx + ' ';
  107. }
  108. }
  109. }
  110. }
  111. callback(req, res, err, msg);
  112. },
  113. getFileByUUID: function (req, res) {
  114. let uuid = req.params.uuid,
  115. rptName = req.params.rptName,
  116. suffix = "." + req.params.suffix
  117. ;
  118. // let user_id = req.session.sessionUser.id; //未来要校验user id
  119. try {
  120. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  121. let rptNameURI = encodeURI(rptName);
  122. res.setHeader("Content-Disposition", "attachment; filename=\"" + rptNameURI + suffix + "\"; filename*=utf-8''" + rptNameURI + suffix );
  123. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + uuid + suffix);
  124. filestream.on('data', function(chunk) {
  125. res.write(chunk);
  126. });
  127. filestream.on('end', function() {
  128. res.end();
  129. });
  130. } catch (e) {
  131. console.log(e);
  132. }
  133. },
  134. };