liveroom.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class LiveroomService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'liveroom');
  10. this.model = this.ctx.model.Liveroom;
  11. this.stumodel = this.ctx.model.Student;
  12. this.umodel = this.ctx.model.User;
  13. }
  14. async create(data) {
  15. const { subid, teacherid } = data;
  16. assert(subid, '缺少科目信息');
  17. assert(teacherid, '缺少教师信息');
  18. const list = await this.model.find();
  19. const last = _.last(list);
  20. let number = '000001';
  21. if (last) {
  22. number = last.number * 1 + 1;
  23. }
  24. data.number = number;
  25. const res = await this.model.create(data);
  26. return res;
  27. }
  28. async personcount(data) {
  29. const { number, name, userid, type } = data;
  30. // 取出已经进入直播的用户列表
  31. // await this.app.redis.del(`liveroom${number}`);
  32. let list = await this.app.redis.get(`liveroom${number}`);
  33. if (!userid) return list;
  34. if (list) {
  35. // 已经开始直播,并且有人观看
  36. list = JSON.parse(list);
  37. } else {
  38. // 还没有人进直播
  39. list = [];
  40. }
  41. if (!type) {
  42. list.push({ name, userid, number });
  43. } else {
  44. list = list.filter(f => f.userid !== userid);
  45. }
  46. list = _.uniqBy(list, 'userid');
  47. list = JSON.stringify(list);
  48. await this.app.redis.set(`liveroom${number}`, list);
  49. const { mq } = this.ctx;
  50. if (mq) {
  51. const exchange = 'liveroom-personcount';
  52. const parm = {
  53. durable: true,
  54. headers: {
  55. userid: 1,
  56. },
  57. };
  58. await mq.fanout(exchange, number, list, parm);
  59. }
  60. return list;
  61. }
  62. async sendNotice(data) {
  63. const { roomid, termid, classid } = data;
  64. const room = await this.model.findById(roomid);
  65. if (!room) {
  66. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '直播房间不存在');
  67. }
  68. const backUrl = encodeURI(`${this.app.config.baseUrl}${this.config.authUrl}?type=0&redirect_uri=${this.app.config.baseUrl}/student/login?redirect_uri=/user/liveDetail/${roomid}`);
  69. let list = [];
  70. const query = {};
  71. if (termid) query.termid = termid;
  72. if (classid) query.classid = classid;
  73. list = await this.stumodel.find(query);
  74. list = list.map(i => ObjectId(i._id));
  75. list = await this.umodel.find({ uid: { $in: list } });
  76. for (const stu of list) {
  77. const { openid } = stu;
  78. if (openid) {
  79. await this.ctx.service.weixin.sendTemplateDesign(
  80. this.ctx.app.config.REVIEW_TEMPLATE_ID,
  81. openid,
  82. '您有一个新的通知!',
  83. '您有新的安排',
  84. '您有在线课堂将要开课,请注意课表上课时间',
  85. '感谢您的使用',
  86. backUrl
  87. );
  88. }
  89. }
  90. }
  91. }
  92. module.exports = LiveroomService;