Browse Source

提交上传excel相关代码

olym 7 years ago
parent
commit
0cb9ca9d23

+ 106 - 58
modules/ration_repository/controllers/ration_repository_controller.js

@@ -9,59 +9,65 @@ import async from "async";
 var callback = function(req, res, err, message, data){
     res.json({error: err, message: message, data: data});
 };
+// 上传控件
+const multiparty = require("multiparty");
+const fs = require("fs");
+// excel解析
+const excel = require("node-xlsx");
 
-class RationRepositoryController extends baseController{
-   async getCompilationList(req, res){
-       try{
-           let compilationModel = new CompilationModel(), rst = [];
-           let compilationList = await compilationModel.getCompilationList();
-           if(compilationList.length <= 0){
-               throw '没有数据';
-           }
-           else{
+class RationRepositoryController extends baseController {
+    async getCompilationList(req, res) {
+        try {
+            let compilationModel = new CompilationModel(), rst = [];
+            let compilationList = await compilationModel.getCompilationList();
+            if (compilationList.length <= 0) {
+                throw '没有数据';
+            }
+            else {
 
-               compilationList.forEach(function (compilation) {
-                   rst.push({_id: compilation._id, name: compilation.name});
-               });
-               //获得相关编办下的工料机库
-               let parallelFucs = [];
-               for(let i = 0; i < rst.length; i++){
-                   parallelFucs.push((function (obj) {
-                       return function (cb) {
-                           gljMapModel.find({compilationId: obj._id, deleted: false}, function (err, result) {
-                               if(err){
-                                   cb(err);
-                               }
-                               else{
-                                   cb(null, result);
-                               }
-                           })
-                       }
-                   })(rst[i]));
-               }
-               async.parallel(parallelFucs, function (err, result) {
-                   if(err){
-                       callback(req, res, err, '没有数据', null);
-                   }
-                   else{
-                       let gljLibsRst = [];
-                       for(let i = 0; i < result.length; i++){
-                           for(let j = 0; j < result[i].length; j++){
-                               gljLibsRst.push(result[i][j]);
-                           }
-                       }
-                       callback(req, res, false, '', {compilation: rst, gljLibs: gljLibsRst});
-                   }
-               })
-           }
-       }
-        catch(err) {
+                compilationList.forEach(function (compilation) {
+                    rst.push({_id: compilation._id, name: compilation.name});
+                });
+                //获得相关编办下的工料机库
+                let parallelFucs = [];
+                for (let i = 0; i < rst.length; i++) {
+                    parallelFucs.push((function (obj) {
+                        return function (cb) {
+                            gljMapModel.find({compilationId: obj._id, deleted: false}, function (err, result) {
+                                if (err) {
+                                    cb(err);
+                                }
+                                else {
+                                    cb(null, result);
+                                }
+                            })
+                        }
+                    })(rst[i]));
+                }
+                async.parallel(parallelFucs, function (err, result) {
+                    if (err) {
+                        callback(req, res, err, '没有数据', null);
+                    }
+                    else {
+                        let gljLibsRst = [];
+                        for (let i = 0; i < result.length; i++) {
+                            for (let j = 0; j < result[i].length; j++) {
+                                gljLibsRst.push(result[i][j]);
+                            }
+                        }
+                        callback(req, res, false, '', {compilation: rst, gljLibs: gljLibsRst});
+                    }
+                })
+            }
+        }
+        catch (err) {
             callback(req, res, err, '没有数据', null);
         }
     }
-    addRationRepository(req,res){
+
+    addRationRepository(req, res) {
         var rationObj = JSON.parse(req.body.rationRepObj);
-        rationRepository.addRationRepository(rationObj,function(err,data){
+        rationRepository.addRationRepository(rationObj, function (err, data) {
             if (data) {
                 callback(req, res, err, "has data", data);
             } else {
@@ -69,28 +75,31 @@ class RationRepositoryController extends baseController{
             }
         })
     }
-    getRationLib(req, res){
+
+    getRationLib(req, res) {
         let libId = req.body.libId;
-        rationRepository.getRationLib(libId, function(err, data){
+        rationRepository.getRationLib(libId, function (err, data) {
             if (data) {
-                callback(req, res, err, "has data",data);
+                callback(req, res, err, "has data", data);
             } else {
                 callback(req, res, err, "no data", null);
             }
         });
     }
-    getDisPlayRationLibs(req, res){
-        rationRepository.getDisplayRationLibs(function(err, data){
+
+    getDisPlayRationLibs(req, res) {
+        rationRepository.getDisplayRationLibs(function (err, data) {
             if (data) {
-                callback(req, res, err, "has data",data);
+                callback(req, res, err, "has data", data);
             } else {
                 callback(req, res, err, "no data", null);
             }
         });
     }
-    getRealLibName(req,res){
+
+    getRealLibName(req, res) {
         var libName = req.body.rationName;
-        rationRepository.getRealLibName(libName,function(err,data){
+        rationRepository.getRealLibName(libName, function (err, data) {
             if (data) {
                 callback(req, res, err, "has data", data);
             } else {
@@ -98,8 +107,9 @@ class RationRepositoryController extends baseController{
             }
         })
     }
-    getLibIDByName(req,res){
-        rationRepository.getLibIDByName(req.body.libName, function(err,data){
+
+    getLibIDByName(req, res) {
+        rationRepository.getLibIDByName(req.body.libName, function (err, data) {
             if (data) {
                 callback(req, res, err, "has ID", data);
             } else {
@@ -108,7 +118,7 @@ class RationRepositoryController extends baseController{
         })
     }
 
-    deleteRationLib(req,res){
+    deleteRationLib(req, res) {
         let oprtor = req.body.oprtor,
             libId = req.body.libId;
         rationRepository.deleteRationLib(oprtor, libId, function (err, message) {
@@ -123,6 +133,44 @@ class RationRepositoryController extends baseController{
             callback(req, res, err, message, null);
         })
     }
+
+    /**
+     * 上传定额库原始数据
+     *
+     * @param {Object} request
+     * @param {Object} response
+     * @return {void}
+     */
+    async uploadSourceData(request, response) {
+        const allowHeader = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
+        try {
+            const uploadOption = {
+                uploadDir: './public/tmp'
+            };
+            const form = new multiparty.Form(uploadOption);
+            form.parse(request, function(err, fields, files) {
+                const file = files.source_file !== undefined ? files.source_file[0] : null;
+                if (err || file === null) {
+                    throw '上传失败';
+                }
+                // 判断类型
+                if (file.headers['content-type'] === undefined || allowHeader.indexOf(file.headers['content-type']) < 0) {
+                    throw '不支持该类型';
+                }
+                // 重命名文件名
+                const uploadFullName = uploadOption.uploadDir + '/' + file.originalFilename;
+                fs.renameSync(file.path, uploadFullName);
+
+                const test = excel.parse(uploadFullName);
+                console.log(test[0].data);
+            });
+        } catch (error) {
+            console.log(error);
+        }
+
+        return;
+    }
+
 }
 
 export default RationRepositoryController;

+ 3 - 0
modules/ration_repository/routes/ration_rep_routes.js

@@ -72,5 +72,8 @@ module.exports =  function (app) {
     apiRouter.post('/getRationItem',searchController.auth, searchController.init, searchController.getRationItem);
     apiRouter.post('/findRation', searchController.auth, searchController.init, searchController.findRation);
 
+    // 导入导出定额库相关
+    apiRouter.post('/upload', rationRepositoryController.auth, rationRepositoryController.init, rationRepositoryController.uploadSourceData);
+
     app.use("/rationRepository/api",apiRouter);
 }

+ 2 - 0
package.json

@@ -25,6 +25,8 @@
     "bluebird": "^3.5.0",
     "jszip": "^3.1.3",
     "log4js": "~2.3.3",
+    "multiparty": "^4.1.3",
+    "node-xlsx": "^0.11.2",
     "pdfkit": "^0.8.2",
     "ueditor": "^1.2.3"
   },

+ 4 - 1
web/maintain/ration_repository/js/main.js

@@ -103,7 +103,10 @@ function getAllRationLib(callback){
                         "<td>"+createDate+" </td>" +
                         "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
                         "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
-                        "<i class='fa fa-remove'></i></a></td></tr>");
+                        "<i class='fa fa-remove'></i></a></td>" +
+                        "<td><a class='btn btn-secondary btn-sm' href='javacript:void(0);' data-toggle='modal' data-target='#import' title='导入原始数据'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
+                        "<td><a class='btn btn-success btn-sm' href='javacript:void(0);' data-toggle='modal' data-target='#emport' title='导出内部数据'><i class='fa fa-sign-out fa-rotate-270'></i>导出</a> <a class='btn btn-secondary btn-sm' href='javacript:void(0);' data-toggle='modal' data-target='#import2' title='导入内部数据'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
+                        "</tr>");
                     var newHref = "/rationRepository/ration?repository="+id;
                     $("#tempId td:first a").attr("href", newHref);
                     $("#tempId").attr("id", id);

+ 60 - 1
web/maintain/ration_repository/main.html

@@ -35,7 +35,7 @@
                   <div class="col-md-8">
                     <div class="warp-p2 mt-3">
                       <table class="table table-hover table-bordered">
-                        <thead><tr><th>定额库名称</th><th width="160">编办</th><th width="160">添加时间</th><th width="90">操作</th></tr></thead>
+                        <thead><tr><th>定额库名称</th><th width="160">编办</th><th width="160">添加时间</th><th width="90">操作</th><th width="90">原始数据</th><th width="150">内部数据</th></tr></thead>
                         <tbody id="showArea">
                         </tbody>
                       </table>
@@ -153,6 +153,65 @@
             </div>
         </div>
     </div>
+    <!--弹出导入原始数据-->
+    <div class="modal fade" id="import" data-backdrop="static" style="display: none;" aria-hidden="true">
+        <form action="/rationRepository/api/upload" enctype="multipart/form-data" method="post">
+            <div class="modal-dialog" role="document">
+                <div class="modal-content">
+                    <div class="modal-header">
+                        <h5 class="modal-title">导入原始数据</h5>
+                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+                            <span aria-hidden="true">×</span>
+                        </button>
+                    </div>
+                    <div class="modal-body">
+                        <div class="alert alert-warning" role="alert">
+                            导入操作会覆盖数据,请谨慎操作!!
+                        </div>
+                        <form>
+                            <div class="form-group">
+                                <label>请选择Excel格式文件</label>
+                                <input class="form-control-file" type="file" name="source_file">
+                            </div>
+                        </form>
+                    </div>
+                    <div class="modal-footer">
+                        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
+                        <button type="submit" class="btn btn-primary">确定导入</button>
+                    </div>
+                </div>
+            </div>
+        </form>
+    </div>
+
+    <!--弹出导入内部数据-->
+    <div class="modal fade" id="import2" data-backdrop="static" style="display: none;" aria-hidden="true">
+        <div class="modal-dialog" role="document">
+            <div class="modal-content">
+                <div class="modal-header">
+                    <h5 class="modal-title">导入内部数据</h5>
+                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+                        <span aria-hidden="true">×</span>
+                    </button>
+                </div>
+                <div class="modal-body">
+                    <div class="alert alert-warning" role="alert">
+                        导入操作会覆盖数据,请谨慎操作!!
+                    </div>
+                    <form>
+                        <div class="form-group">
+                            <label>请选择Excel格式文件</label>
+                            <input class="form-control-file" type="file">
+                        </div>
+                    </form>
+                </div>
+                <div class="modal-footer">
+                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
+                    <a href="" class="btn btn-primary">确定导入</a>
+                </div>
+            </div>
+        </div>
+    </div>
     <!-- JS. -->
     <script src="/lib/jquery/jquery.min.js"></script>
     <script src="/lib/tether/tether.min.js"></script>