Browse Source

整合websocket的通知推送

caiaolin 8 years ago
parent
commit
a8310cf5be

+ 14 - 2
modules/glj/controllers/glj_controller.js

@@ -37,16 +37,27 @@ class GLJController extends BaseController {
             if (isNaN(projectId) || projectId <= 0) {
                 throw '标段id有误';
             }
+
+            // 获取标段对应的单价文件id
+            let unitPriceFileModel = new UnitPriceFileModel();
+            let unitPriceFile = await unitPriceFileModel.getDataByProject(projectId);
+            if (!unitPriceFile) {
+                throw '没有对应的单价文件';
+            }
+            // 单价文件id作为同步的roomId
+            let unitPriceFileId = unitPriceFile.id;
+
             // 先获取对应标段的项目工料机数据
             let gljListModel = new GLJListModel();
-            let gljList = await gljListModel.getListByProjectId(projectId);
+            let gljList = await gljListModel.getListByProjectId(projectId, unitPriceFileId);
 
             let renderData = {
                 gljList: JSON.stringify(gljList),
                 materialIdList: gljListModel.materialIdList,
+                hostname: request.hostname,
+                roomId: unitPriceFileId
             };
             response.render('glj/html/glj_index', renderData);
-
         } catch (error) {
             response.status(404).send('404 Error');
         }
@@ -122,6 +133,7 @@ class GLJController extends BaseController {
      * @return {void}
      */
     async test(request, response) {
+        console.log(global.test);
         // 从定额库获取的数据
         let data = {
             glj_repository_id: 1,

+ 2 - 9
modules/glj/models/glj_list_model.js

@@ -53,9 +53,10 @@ class GLJListModel extends BaseModel {
      * 根据标段对应工料机数据
      *
      * @param {Number} projectId
+     * @param {Number} unitPriceFileId
      * @return {Promise}
      */
-    async getListByProjectId(projectId) {
+    async getListByProjectId(projectId, unitPriceFileId) {
         let gljData = null;
         try {
             // 首先获取对应标段下所有的项目工料机数据
@@ -68,14 +69,6 @@ class GLJListModel extends BaseModel {
                 throw '无数据';
             }
 
-            // 获取标段对应的单价文件id
-            let unitPriceFileModel = new UnitPriceFileModel();
-            let unitPriceFile = await unitPriceFileModel.getDataByProject(projectId);
-            if (!unitPriceFile) {
-                throw '没有对应的单价文件';
-            }
-            let unitPriceFileId = unitPriceFile.id;
-
             // 获取标段设置的单价文件数据
             let unitPriceModel = new UnitPriceModel();
             let unitPriceList = await unitPriceModel.getDataByFileId(unitPriceFileId);

+ 1 - 0
package.json

@@ -24,6 +24,7 @@
   "dependencies": {
     "bluebird": "^3.5.0",
     "jszip": "^3.1.3",
+    "socket.io": "^2.0.3",
     "uuid": "^3.1.0"
   },
   "scripts": {

+ 27 - 0
socket.js

@@ -0,0 +1,27 @@
+/**
+ * socket入口脚本文件
+ *
+ * @author caiaolin
+ * @date 2017-05-12
+ * @version
+ */
+import socket from "socket.io";
+
+const socketIO = socket(3300);
+
+// socket.io相关操作
+socketIO.on('connection', function(socket) {
+    let roomId = '';
+
+    // 加入房间
+    socket.on('join', function(data) {
+        roomId = data;
+        socket.join(data);
+    });
+
+    // 数据更改通知
+    socket.on('dataNotify', function(data) {
+        socket.broadcast.to(roomId).emit('dataChange', data);
+    });
+
+});

+ 5 - 0
web/glj/html/glj_index.html

@@ -35,6 +35,11 @@
     // 不能修改市场价格的类型id
     let canNotChangeTypeId = '64';
     canNotChangeTypeId = canNotChangeTypeId !== '' ? canNotChangeTypeId.split(",") : '';
+    let host = '<%= hostname %>';
+    let socket = null;
+    let roomId = <%= roomId %>;
+    let changeInfo = [];
 </script>
 <%include footer.html %>
+<script type="text/javascript" src="/web/glj/js/socket.js"></script>
 <script type="text/javascript" src="/web/glj/js/glj_index.js"></script>

+ 3 - 52
web/glj/js/glj_index.js

@@ -1,5 +1,5 @@
 /**
- * 聊天室相关
+ * 工料机汇总相关
  *
  * @author CaiAoLin
  * @date 2017/6/15
@@ -105,28 +105,8 @@ $(document).ready(function () {
         rowCounter++;
     }
 
-/*
-    let socket = io('http://notify.smartcost.com.cn:3300');
-    socket.on('connect', function () {
-        socket.emit('join', project);
-        console.log('连接成功');
-    });
-
-    // 接受到改变
-    let changeInfo = [];
-    socket.on('dataChange', function(data) {
-        data = JSON.parse(data);
-        if (data.newValue === undefined) {
-            return false;
-        }
-        changeInfo.push(data);
-        $("#message").html('基价单位已被修改,<a href="javascript:void(0);" id="load-data">点击加载</a>');
-        $("#notify").slideDown('fast');
-        initiativeChange = false;
-    });
 
     // 是否主动更改数据
-    let initiativeChange = false;
     $("#message").on('click', '#load-data', function() {
         $("#notify").slideUp('fast');
         if (changeInfo.length > 0) {
@@ -138,11 +118,6 @@ $(document).ready(function () {
         changeInfo = [];
     });
 
-    // 进入单元格后设置为主动设置数据
-    sheet.bind(GC.Spread.Sheets.Events.EditEnding, function (element, info) {
-        initiativeChange = true;
-    });
- */
     // 绑定事件
     let isChanging = false;
     sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function (element, info) {
@@ -205,32 +180,6 @@ $(document).ready(function () {
                 }
             }
         });
-        /*
-        // 如果修改了数据且为主动修改,先存库再广播给其他页面
-        if (info.newValue !== undefined && info.oldValue !== undefined && initiativeChange) {
-            let id = sheet.getCell(info.row, 0).value();
-            $.ajax({
-                url: '/save',
-                type: 'post',
-                data: {priceValue: info.newValue, id: id},
-                error: function() {
-                    // @todo 替换为更好地错误提示
-                    alert('error');
-                },
-                beforeSend: function() {
-
-                },
-                success: function(response) {
-                    if (response.err === 0) {
-                        socket.emit('dataNotify', JSON.stringify(info));
-                    } else {
-                        // @todo 替换为更好地错误提示
-                        alert(response.msg);
-                    }
-                }
-            });
-        }
-        */
     });
 
     // 绑定双击事件
@@ -332,6 +281,8 @@ function successTrigger(field, info) {
             // 市场单价的计算
             marketPriceCalculate(type, info);
 
+            // 触发websocket通知
+            socket.emit('dataNotify', JSON.stringify(info));
             break;
     }
 }

+ 28 - 0
web/glj/js/socket.js

@@ -0,0 +1,28 @@
+/**
+ * websocket相关
+ *
+ * @author CaiAoLin
+ * @date 2017/7/6
+ * @version
+ */
+$(document).ready(function() {
+
+    // 连接socket服务器
+    socket = io('http://'+ host +':3300');
+    socket.on('connect', function () {
+        socket.emit('join', roomId);
+        console.log('连接成功');
+    });
+
+    // 接收到改变
+    socket.on('dataChange', function(data) {
+        data = JSON.parse(data);
+        if (data.newValue === undefined) {
+            return false;
+        }
+        changeInfo.push(data);
+        $("#message").html('市场单位已被修改,<a href="javascript:void(0);" id="load-data">点击加载</a>');
+        $("#notify").slideDown('fast');
+    });
+
+});