1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- class LiveroomService extends CrudService {
- constructor(ctx) {
- super(ctx, 'liveroom');
- this.model = this.ctx.model.Liveroom;
- this.stumodel = this.ctx.model.Student;
- this.umodel = this.ctx.model.User;
- }
- async create(data) {
- const { subid, teacherid } = data;
- assert(subid, '缺少科目信息');
- assert(teacherid, '缺少教师信息');
- const list = await this.model.find();
- const last = _.last(list);
- let number = '000001';
- if (last) {
- number = last.number * 1 + 1;
- }
- data.number = number;
- const res = await this.model.create(data);
- return res;
- }
- async personcount(data) {
- const { number, name, userid, type } = data;
- // 取出已经进入直播的用户列表
- // await this.app.redis.del(`liveroom${number}`);
- let list = await this.app.redis.get(`liveroom${number}`);
- if (!userid) return list;
- if (list) {
- // 已经开始直播,并且有人观看
- list = JSON.parse(list);
- } else {
- // 还没有人进直播
- list = [];
- }
- if (!type) {
- list.push({ name, userid, number });
- } else {
- list = list.filter(f => f.userid !== userid);
- }
- list = _.uniqBy(list, 'userid');
- list = JSON.stringify(list);
- 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);
- }
- return list;
- }
- async sendNotice(data) {
- const { roomid, termid, classid } = data;
- const room = await this.model.findById(roomid);
- if (!room) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '直播房间不存在');
- }
- 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}`);
- let list = [];
- const query = {};
- if (termid) query.termid = termid;
- if (classid) query.classid = classid;
- list = await this.stumodel.find(query);
- list = list.map(i => ObjectId(i._id));
- list = await this.umodel.find({ uid: { $in: list } });
- for (const stu of list) {
- const { openid } = stu;
- if (openid) {
- await this.ctx.service.weixin.sendTemplateDesign(
- this.ctx.app.config.REVIEW_TEMPLATE_ID,
- openid,
- '您有一个新的通知!',
- '您有新的安排',
- '您有在线课堂将要开课,请注意课表上课时间',
- '感谢您的使用',
- backUrl
- );
- }
- }
- }
- }
- module.exports = LiveroomService;
|