zhangweicheng 6 years ago
parent
commit
77cb8b217b
3 changed files with 106 additions and 22 deletions
  1. 56 0
      lib/rabbitmq/RabbitMQ.js
  2. 27 22
      logs/online_logs.js
  3. 23 0
      public/cipher.js

+ 56 - 0
lib/rabbitmq/RabbitMQ.js

@@ -0,0 +1,56 @@
+/**
+ * Created by zhang on 2019/5/5.
+ */
+let amqp = require('amqplib');
+class RabbitMQ{
+    constructor(connect){
+        this.connect = connect;
+    }
+
+    static async connection(URL){
+        if(!RabbitMQ.instance){
+            RabbitMQ.instance =  new RabbitMQ(await amqp.connect(URL)) //'amqp://13726297388:123456@qa.smartcost.com.cn:5672'
+        }
+        return RabbitMQ.instance
+    }
+
+    static async sendMessage(queue,msg,durable=false){
+        if(RabbitMQ.instance){
+            let ch = await RabbitMQ.instance.connect.createChannel();
+            try {
+                await ch.assertQueue(queue, {durable: durable});
+                ch.sendToQueue(queue,Buffer.from(msg),{persistent: durable});
+                console.log(" Sent %s to :"+queue, msg);
+                ch.close();
+            }catch (e){
+                console.log(e);
+                ch.close();
+            }
+
+        }
+    }
+
+    static async receiveMessage(queue,callback,durable=false){
+        if(RabbitMQ.instance){
+            let ch = await RabbitMQ.instance.connect.createChannel();
+            try {
+                await ch.assertQueue(queue, {durable: durable});
+                ch.prefetch(1);
+                console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", queue);
+                ch.consume(queue, function(msg) {
+                    console.log(" [x] Received %s", msg.content.toString());
+                    if(durable == true) ch.ack(msg);
+                    if(callback) callback(msg)
+                }, {
+                    noAck: !durable
+                });
+            }catch (e){
+                console.log(e);
+                ch.close();
+            }
+        }
+    }
+}
+
+export default RabbitMQ;
+

+ 27 - 22
logs/online_logs.js

@@ -10,28 +10,33 @@ import moment from "moment";
 let logs_model = mongoose.model("online_logs");
 
 async function saveOnlineTime(req) {
-    let interval_time = 10 * 60 *1000;
-    let start = req.session.online_start_time;
-    let end = + new Date();
-    if(start === undefined) return req.session.online_start_time ==end;
-    let online_times =  end - start;
-    //1秒内只记一次就好
-    if(online_times < 500) return;//如果间隔太短,则忽略
-    if(online_times > interval_time ){//如果间隔超过有效时长,则不累加这次时长,从头开始算
+    try {
+        let interval_time = 10 * 60 *1000;
+        let start = req.session.online_start_time;
+        let end = + new Date();
+        if(start === undefined) return req.session.online_start_time ==end;
+        let online_times =  end - start;
+        //1秒内只记一次就好
+        if(online_times < 500) return;//如果间隔太短,则忽略
+        if(online_times > interval_time ){//如果间隔超过有效时长,则不累加这次时长,从头开始算
+            req.session.online_start_time = end;
+            return
+        }
+        if(!req.session.sessionUser||!req.session.sessionCompilation) return;
+        let dataString = moment(end).format('YYYY-MM-DD');
+        let condition = {userID:req.session.sessionUser.id,compilationID:req.session.sessionCompilation._id,dateString:dataString};
+        let record = await logs_model.findOne(condition);
+        if(record){ //如果找到,则累加
+            await logs_model.update(condition,{$inc:{'online_times' : online_times }});
+        }else {//如果没找到,则新增一条记录
+            condition["online_times"] = online_times;
+            let today = moment(dataString).toDate();
+            condition["dateTime"] = +today;
+            await logs_model.create(condition);
+        }
         req.session.online_start_time = end;
-        return
+    }catch (e){
+        console.log(e)
     }
-    if(!req.session.sessionUser||!req.session.sessionCompilation) return;
-    let dataString = moment(end).format('YYYY-MM-DD');
-    let condition = {userID:req.session.sessionUser.id,compilationID:req.session.sessionCompilation._id,dateString:dataString};
-    let record = await logs_model.findOne(condition);
-    if(record){ //如果找到,则累加
-        await logs_model.update(condition,{$inc:{'online_times' : online_times }});
-    }else {//如果没找到,则新增一条记录
-        condition["online_times"] = online_times;
-        let today = moment(dataString).toDate();
-        condition["dateTime"] = +today;
-        await logs_model.create(condition);
-    }
-    req.session.online_start_time = end;
+
 }

+ 23 - 0
public/cipher.js

@@ -0,0 +1,23 @@
+/**
+ * Created by zhang on 2019/5/10.
+ */
+let crypto = require('crypto');
+module.exports ={
+    aesEncrypt:aesEncrypt,
+    aesDecrypt:aesDecrypt
+};
+
+
+function aesEncrypt(data, key = 'SmartCost') {
+    const cipher = crypto.createCipher('aes192', key);
+    var crypted = cipher.update(data, 'utf8', 'hex');
+    crypted += cipher.final('hex');
+    return crypted;
+}
+
+function aesDecrypt(encrypted, key= 'SmartCost') {
+    const decipher = crypto.createDecipher('aes192', key);
+    var decrypted = decipher.update(encrypted, 'hex', 'utf8');
+    decrypted += decipher.final('utf8');
+    return decrypted;
+}