rpt_controller.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Created by Tony on 2017/3/13.
  3. */
  4. let JV = require('../rpt_component/jpc_value_define');
  5. let Template = require('../models/rpt_template');
  6. let TemplateData = require('../models/rpt_tpl_data_demo');
  7. let JpcEx = require('../rpt_component/jpc_ex');
  8. //let cache = require('../../../public/cache/cacheUtil');
  9. let rptUtil = require("../util/rpt_util");
  10. let rpt_xl_util = require('../util/rpt_excel_util');
  11. let fs = require('fs');
  12. let strUtil = require('../../../public/stringUtil');
  13. //统一回调函数
  14. let callback = function(req, res, err, data){
  15. if(err){
  16. res.json({success: false, error: err});
  17. }
  18. else{
  19. //res.send({success: true, data: data});
  20. res.json({success:true, data: data});
  21. }
  22. };
  23. function getAllPagesCommon(req, res, rpt_id, pageSize, cb) {
  24. let rptTpl = null;
  25. Template.findOne({ID: rpt_id}, '-_id').exec().then(function(rst) {
  26. rptTpl = rst;
  27. if (rptTpl) {
  28. if (rptTpl.ID_KEY) {
  29. return TemplateData.getPromise(rptTpl.ID_KEY);
  30. } else {
  31. callback(req, res, 'No report template data were found!', null);
  32. }
  33. } else {
  34. callback(req, res, 'No report template was found!', null);
  35. }
  36. }).then(function(tplData){
  37. if (tplData) {
  38. let printCom = JpcEx.createNew();
  39. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
  40. let defProperties = rptUtil.getReportDefaultCache();
  41. printCom.initialize(rptTpl);
  42. printCom.analyzeData(rptTpl, tplData, defProperties);
  43. let maxPages = printCom.totalPages;
  44. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
  45. if (pageRst) {
  46. cb(pageRst);
  47. } else {
  48. callback(req, res, "Have errors while on going...", null);
  49. }
  50. } else {
  51. callback(req, res, 'No report data were found!', null);
  52. }
  53. }
  54. );
  55. };
  56. module.exports = {
  57. getReportAllPages: function(req, res){
  58. let rpt_id = req.body.ID;
  59. let pageSize = req.body.pageSize;
  60. getAllPagesCommon(req, res, rpt_id, pageSize, function(pageRst){
  61. callback(req, res, null, pageRst);
  62. })
  63. },
  64. getExcel: function(req, res) {
  65. let rpt_id = req.params.id,
  66. pageSize = req.params.size,
  67. rptName = req.params.rptName;
  68. getAllPagesCommon(req, res, rpt_id, pageSize, function(pageRst){
  69. rpt_xl_util.exportExcel(pageRst, rptName, null, function(newName){
  70. res.setHeader('Content-Type', 'application/vnd.openxmlformats');
  71. res.setHeader("Content-Disposition", "attachment; filename=" + strUtil.getPinYinCamelChars(rptName) + ".xlsx");
  72. let filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
  73. filestream.on('data', function(chunk) {
  74. res.write(chunk);
  75. });
  76. filestream.on('end', function() {
  77. res.end();
  78. });
  79. });
  80. })
  81. }
  82. };