Ver Fonte

修改消息状态

liuyu há 5 anos atrás
pai
commit
4b23ddf25c
3 ficheiros alterados com 33 adições e 1 exclusões
  1. 5 0
      app/controller/personchat.js
  2. 1 0
      app/router.js
  3. 27 1
      app/service/personchat.js

+ 5 - 0
app/controller/personchat.js

@@ -11,6 +11,11 @@ class PersonchatController extends Controller {
     super(ctx);
     this.service = this.ctx.service.personchat;
   }
+
+  async received() {
+    await this.service.received(this.ctx.body);
+    this.ok({ });
+  }
 }
 
 module.exports = CrudController(PersonchatController, meta);

+ 1 - 0
app/router.js

@@ -66,4 +66,5 @@ module.exports = app => {
   // 私人聊天记录表设置路由
   router.resources('personchat', '/api/live/personchat', controller.personchat); // index、create、show、destroy
   router.post('personchat', '/api/live/personchat/update/:id', controller.personchat.update);
+  router.post('personchat', '/api/live/personchat/received', controller.personchat.received); // 将消息设置成已读
 };

+ 27 - 1
app/service/personchat.js

@@ -16,7 +16,7 @@ class PersonchatService extends CrudService {
     assert(content, '缺少发言内容');
     assert(receiver_id, '缺少接收人信息');
     assert(personroom_id, '缺少聊天房间信息');
-    const send_time = moment().format("YYYY-MM-DD HH:mm:ss");
+    const send_time = moment().format('YYYY-MM-DD HH:mm:ss');
     const res = await this.model.create({
       sender_id,
       sender_name,
@@ -53,6 +53,32 @@ class PersonchatService extends CrudService {
     }
     return res;
   }
+
+  async received(data) {
+    const { ids, sender_id, receiver_id, personroom_id } = data;
+    for (const id of ids) {
+      const chat = await this.model.findById(id);
+      chat.status = '1';
+      await chat.save();
+    }
+    const { mq } = this.ctx;
+    if (mq) {
+      const exchange = 'person_chat';
+      const parm = {
+        durable: true,
+        headers: {
+          sender_id,
+          receiver_id,
+        },
+      };
+      await mq.topic(
+        exchange,
+        personroom_id + '_' + receiver_id,
+        '消息已查收',
+        parm
+      );
+    }
+  }
 }
 
 module.exports = PersonchatService;