فهرست منبع

Merge branch 'master' of http://smartcost.f3322.net:3000/SmartCost/ConstructionCost

zhongzewei 8 سال پیش
والد
کامیت
e7a5550658

+ 15 - 0
modules/rationLib/models/rationRepository.js

@@ -0,0 +1,15 @@
+/**
+ * Created by Tony on 2017/4/19.
+ * 定额库分类,如重庆、广东等各个省的建筑定额,未来考虑与公路合并
+ */
+var mongoose = require('mongoose');
+var dbm = require("../../../config/db/db_manager");
+var rationLibdb = dbm.getCfgConnection("rationLibs");
+var Schema = mongoose.Schema;
+
+var RationLibSchema = new Schema({
+    "ID" : String,
+    "dispName" : String,
+    "type" : String,
+    "descr" : String
+});

+ 42 - 0
modules/reports/controllers/rpt_controller.js

@@ -8,6 +8,8 @@ var TemplateData = require('../models/rpt_temp_data');
 var JpcEx = require('../rpt_component/JpcEx');
 //var cache = require('../../../public/cache/cacheUtil');
 var rptUtil = require("../util/rpt_util");
+var rpt_xl_util = require('../util/rpt_excel_util');
+var fs = require('fs');
 
 //统一回调函数
 var callback = function(req, res, err, data){
@@ -54,6 +56,46 @@ module.exports = {
             }
         );
     },
+    getExcel: function(req, res) {
+        var grp_id = req.params.id, tpl_id = req.params.pm, pageSize = req.params.size, rptName = req.params.rptName;
+        var rptTpl = null;
+        Template.getPromise(grp_id, tpl_id).then(function(rst) {
+            rptTpl = rst;
+            if (rptTpl) {
+                return TemplateData.getPromise(tpl_id);
+            } else {
+                callback(req, res, 'No report template was found!', null);
+            }
+        }).then(function(tplData){
+                if (tplData) {
+                    var printCom = JpcEx.createNew();
+                    rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pageSize;
+                    var defProperties = rptUtil.getReportDefaultCache();
+                    printCom.initialize(rptTpl);
+                    printCom.analyzeData(rptTpl, tplData, defProperties);
+                    var maxPages = printCom.totalPages;
+                    var pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties);
+                    if (pageRst) {
+                        rpt_xl_util.exportExcel(pageRst, rptName, null, function(newName){
+                            res.setHeader('Content-Type', 'application/vnd.openxmlformats');
+                            res.setHeader("Content-Disposition", "attachment; filename=" + rptName + ".xlsx");
+                            var filestream = fs.createReadStream(__dirname.slice(0, __dirname.length - 28) + '/tmp/' + newName + '.xlsx');
+                            filestream.on('data', function(chunk) {
+                                res.write(chunk);
+                            });
+                            filestream.on('end', function() {
+                                res.end();
+                            });
+                        });
+                    } else {
+                        callback(req, res, "Have errors while on going...", null);
+                    }
+                } else {
+                    callback(req, res, 'No report data were found!', null);
+                }
+            }
+        );
+    },
     getDummyExcel: function(req, res) {
         var nodeExcel = require('excel-export'); //关联excel-export模块
         console.log("req.params.id:" + req.params.id);

+ 2 - 1
modules/reports/routes/report_router.js

@@ -9,6 +9,7 @@ var reportController = require('./../controllers/rpt_controller');
 
 rptRouter.post('/getReport', reportController.getReportAllPages);
 //rptRouter.post('/getPreview', pmController.updateProjects);
-rptRouter.get('/getExcel/:id/:pm', reportController.getDummyExcel);
+rptRouter.get('/getDummyExcel/:id/:pm', reportController.getDummyExcel);
+rptRouter.get('/getExcel/:id/:pm/:size/:rptName', reportController.getExcel);
 
 module.exports = rptRouter;

+ 22 - 11
modules/reports/util/rpt_excel_util.js

@@ -202,7 +202,12 @@ function writeSharedString(sharedStrList){
         rst.push(dftHeadXml + '\r\n');
         rst.push('<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="' + sharedStrList.length + '" uniqueCount="' + sharedStrList.length + '">');
         for (var i = 0; i < sharedStrList.length; i++) {
-            rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
+            //rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
+            if (typeof sharedStrList[i] === 'string') {
+                rst.push('<si><t>' + sharedStrList[i].replace('|','\r\n') + '</t></si>');
+            } else {
+                rst.push('<si><t>' + sharedStrList[i] + '</t></si>');
+            }
         }
         rst.push('</sst>');
     }
@@ -610,7 +615,7 @@ function writeSheet(pageData, sheetData, sharedStrList, stylesObj){
 }
 
 module.exports = {
-    exportExcel: function (pageData, fName, options) {
+    exportExcel: function (pageData, fName, options, callback) {
         var rptOptions = (options || {singlePage: false, fileName: 'report'});
         var sheets = [];
         for (var i = 0; i < pageData.items.length; i++) {
@@ -663,14 +668,20 @@ module.exports = {
         data = writeStyles(stylesObj);
         zip_xl.file(file, data.join(''), {compression: 'DEFLATE'});
 
-        zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
-            //.pipe(fs.createWriteStream('../../../tmp/outExcel.xlsx'))
-            .pipe(fs.createWriteStream('../../../tmp/' + fName + '.zip'))
-            .on('finish', function () {
-                // JSZip generates a readable stream with a "end" event,
-                // but is piped here in a writable stream which emits a "finish" event.
-                console.log("outExcel.xlsx was written.");
-            }
-        );
+        if (fName) {
+            var newName = '' + (new Date()).valueOf();
+            zip.generateNodeStream({type:'nodebuffer',streamFiles:true})
+                .pipe(fs.createWriteStream(__dirname.slice(0, __dirname.length - 21) + '/tmp/' + newName + '.xlsx'))
+                .on('finish', function () {
+                    // JSZip generates a readable stream with a "end" event,
+                    // but is piped here in a writable stream which emits a "finish" event.
+                    console.log(newName + ".xlsx was written.");
+                    if (callback) callback(newName);
+                }
+            );
+        } else {
+            //return zip.generateNodeStream({type:'nodebuffer',streamFiles:true});
+            return zip;
+        }
     }
 }

+ 11 - 0
public/web/urlUtil.js

@@ -0,0 +1,11 @@
+/**
+ * Created by Mai on 2017/4/18.
+ */
+var scUrlUtil = {
+    GetQueryString: function (name) {
+        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
+        var r = window.location.search.substr(1).match(reg);
+        if (r != null) return unescape(r[2]);
+        return null;
+    }
+};

+ 18 - 15
server.js

@@ -36,30 +36,33 @@ app.use(session({
     //*/
 }));
 
+app.use(function (req, res, next) {
+    var url = req.originalUrl;
+    if (!/^\/login/.test(req.originalUrl) && !req.session.userAccount) {
+        if (req.headers.referer) {
+            return res.redirect('/login' + '?referer=' + req.headers.referer);
+        } else {
+            return res.redirect("/login" + '?referer=' + req.originalUrl);
+        }
+    }
+    next();
+});
+
 app.use('/', require('./modules/users/routes/users_route'));
 app.use('/fees', require('./modules/fees/routes/fees_router'));
 
 app.get('/template/bills', function (req, res) {
-    if (!req.session.userAccount) {
-        res.redirect('/login');
-    } else {
-        res.render('templates/html/bills.html',
-            {userAccount: req.session.userAccount,
-                userID: req.session.userID});
-    }
+    res.render('templates/html/bills.html',
+        {userAccount: req.session.userAccount,
+            userID: req.session.userID});
 });
 var billsTemplateRouter = require('./modules/templates/routes/bills_template_router');
 app.use('/template/bills', billsTemplateRouter);
 
 app.get('/pm', function(req, res){
-    if(!req.session.userAccount){
-        res.redirect('/login');
-    }
-    else{
-        res.render('pm/html/project-management.html',
-            {userAccount: req.session.userAccount,
-                userID: req.session.userID});
-    }
+    res.render('pm/html/project-management.html',
+        {userAccount: req.session.userAccount,
+            userID: req.session.userID});
 });
 
 var pmRouter = require('./modules/pm/routes/pm_route');

+ 16 - 2
web/report/html/RptHome.html

@@ -109,10 +109,24 @@
         }
     }
 
-    function getExcel() {
+    function getDummyExcel() {
         var id = "dummy_request_id";
         var p1 = "dummy_paramm_2";
-        var url =  "/report_api/getExcel/" + id + "/" + p1;
+        var url =  "/report_api/getDummyExcel/" + id + "/" + p1;
+        window.location = url;//这里不能使用get方法跳转,否则下载不成功
+    }
+
+    function getExcel() {
+        var rpt = document.getElementById("select_k1");
+        var size = document.getElementById("select_k2");
+        var rpt_name = rpt.options[rpt.selectedIndex].text;
+        if (rpt.selectedIndex >= 0 && size.selectedIndex >= 0) {
+            var strs = rpt.options[rpt.selectedIndex].value.split(';');
+            rpt_grp = strs[0];
+            rpt_id = strs[1];
+            rpt_size = size.options[size.selectedIndex].value;
+        }
+        var url =  "/report_api/getExcel/" + rpt_grp + "/" + rpt_id + "/" + rpt_size + "/" + rpt_name;
         window.location = url;//这里不能使用get方法跳转,否则下载不成功
     }
 

+ 8 - 1
web/users/login.html

@@ -9,6 +9,7 @@
     <link rel="stylesheet" href="/web/css/main.css">
     <link rel="stylesheet" href="/web/css/font-awesome/font-awesome.min.css">
     <script src="/lib/jquery/jquery.min.js"></script>
+    <script src="/public/web/urlUtil.js"></script>
 </head>
 <body>
     <div class="container">
@@ -36,6 +37,8 @@
 
         <script>
             $(document).ready(function () {
+                var referer = scUrlUtil.GetQueryString('referer');
+
                 $("#login").click(function () {
                     var account = $("#inputEmail").val();
                     var pw = $("#inputPassword").val();
@@ -47,7 +50,11 @@
                         success: function (result) {
                             if (result.data) {
 //                                $('#ver').modal('show');
-                                location.href = '/';
+                                if (referer) {
+                                    location.href = referer;
+                                } else {
+                                    location.href = '/';
+                                }
                             }
                             else {
                                 $('#hint').html(result.error);