lrf402788946 4 years ago
parent
commit
e50243ee31
5 changed files with 51 additions and 6 deletions
  1. 1 1
      app/controller/.chatroom.js
  2. 18 5
      app/controller/.liveroom.js
  3. 6 0
      app/router.js
  4. 1 0
      app/service/chatroom.js
  5. 25 0
      app/service/liveroom.js

+ 1 - 1
app/controller/.chatroom.js

@@ -23,7 +23,7 @@ module.exports = {
         teacherid: "teacherid",
         studentid: "studentid",
         teacher: "teacher",
-        student: "student"
+        student: "student",
       },
     },
     service: "query",

+ 18 - 5
app/controller/.liveroom.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ["!subid","subname", "!teacherid","teacher", "reason"],
+    requestBody: ["!subid", "subname", "!teacherid", "teacher", "reason"],
   },
   destroy: {
     params: ["!id"],
@@ -8,7 +8,16 @@ module.exports = {
   },
   update: {
     params: ["!id"],
-    requestBody: ["number", "!subid","subname", "!teacherid","teacher", "status", "reason", 'start'],
+    requestBody: [
+      "number",
+      "!subid",
+      "subname",
+      "!teacherid",
+      "teacher",
+      "status",
+      "reason",
+      "start",
+    ],
   },
   show: {
     parameters: {
@@ -21,10 +30,10 @@ module.exports = {
       query: {
         number: "number",
         subid: "subid",
-        subname:'subname',
-        teacher:"teacher",
+        subname: "subname",
+        teacher: "teacher",
         teacherid: "teacherid",
-        start:"start",
+        start: "start",
         status: "status",
       },
     },
@@ -36,4 +45,8 @@ module.exports = {
       count: true,
     },
   },
+  personcount: {
+    requestBody: ["!teacherid", "teacher", "!studentid", "student"],
+    service: "personcount",
+  },
 };

+ 6 - 0
app/router.js

@@ -540,6 +540,12 @@ module.exports = app => {
     '/api/train/liveroom/update/:id',
     controller.liveroom.update
   );
+  // 监听人数
+  router.post(
+    'liveroom',
+    '/api/train/liveroom/personcount',
+    controller.liveroom.personcount
+  );
   // 培训视频
   router.resources(
     'trainvideo',

+ 1 - 0
app/service/chatroom.js

@@ -23,6 +23,7 @@ class ChatroomService extends CrudService {
     const cres = await this.model.create({ teacherid, teacher, studentid, student });
     return cres;
   }
+
 }
 
 module.exports = ChatroomService;

+ 25 - 0
app/service/liveroom.js

@@ -27,6 +27,31 @@ class LiveroomService extends CrudService {
     return res;
   }
 
+  async personcount(data) {
+    const { number, name, userid } = data;
+    // 取出已经进入直播的用户列表
+    let list = await this.app.redis.get(`liveroom${number}`);
+    if (list) {
+      // 已经开始直播,并且有人观看
+      list = JSON.parse(list);
+      list = JSON.stringify(list.push({ name, userid }));
+    } else {
+      // 还没有人进直播
+      list = JSON.stringify([{ name, userid }]);
+    }
+    await this.app.redis.set(`liveroom${number}`, list);
+    const { mq } = this.ctx;
+    if (mq) {
+      const exchange = 'liveroom-personcount';
+      const parm = {
+        durable: true,
+        headers: {
+          userid: 1,
+        },
+      };
+      await mq.fanout(exchange, number, list, parm);
+    }
+  }
 }
 
 module.exports = LiveroomService;