room.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 RoomService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'room');
  10. this.model = this.ctx.model.Room;
  11. this.rumodel = this.ctx.model.Roomuser;
  12. this.lookmodel = this.ctx.model.Lookuser;
  13. }
  14. async create(data) {
  15. const { name } = data;
  16. const room_ = await this.model.findOne({ name });
  17. if (room_) {
  18. throw new BusinessError('此房间已经存在,请重新输入');
  19. }
  20. const roomuser = await this.rumodel.findById(data.anchorid);
  21. if (!roomuser) {
  22. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  23. }
  24. data.username = roomuser.name;
  25. const res = await this.model.create(data);
  26. return res;
  27. }
  28. async findroomname(data) {
  29. const { roomname } = data;
  30. const res = await this.model.findOne({ name: roomname });
  31. return res;
  32. }
  33. async query({ skip, limit, ...info }) {
  34. const total = await this.model.count(info);
  35. const rooms = await this.model
  36. .find(info)
  37. .skip(Number(skip))
  38. .limit(Number(limit));
  39. const data = [];
  40. for (const _room of rooms) {
  41. const room = _.cloneDeep(JSON.parse(JSON.stringify(_room)));
  42. const number = await this.lookmodel.count({ roomid: room._id });
  43. room.number = number;
  44. data.push(room);
  45. }
  46. return { data, total };
  47. }
  48. // 接收录播地址
  49. async startrecord(data) {
  50. // 接收录播参数
  51. const { stream_id } = data;
  52. console.log('视频回调--->' + data);
  53. if (stream_id) {
  54. const arr = stream_id.split('_');
  55. const id = arr[1];
  56. console.log('id--->' + id);
  57. const res = await this.model.findById(id);
  58. if (res) {
  59. res.stream_id = stream_id;
  60. res.url = data.video_url;
  61. res.file_id = data.file_id;
  62. res.ishis = '1';
  63. await res.save();
  64. }
  65. }
  66. }
  67. async updaterequid(id, requestid) {
  68. const res = await this.model.findById(id);
  69. if (res) {
  70. res.requestId = requestid;
  71. await res.save();
  72. }
  73. return res;
  74. }
  75. async roomquest(data) {
  76. const res = await this.model.findById(data.roomid);
  77. if (!res) {
  78. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  79. }
  80. res.queid = data.queid;
  81. res.isque = '1';
  82. const result = await res.save();
  83. if (result) {
  84. const { mq } = this.ctx;
  85. if (mq) {
  86. const exchange = 'quest_publish_' + res.id;
  87. const parm = {
  88. durable: true,
  89. headers: {
  90. userid: res.id,
  91. } };
  92. await mq.fanout(exchange, res.id, JSON.stringify(res), parm);
  93. }
  94. }
  95. return result;
  96. }
  97. async roomquestclose(data) {
  98. const res = await this.model.findById(data.roomid);
  99. if (!res) {
  100. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  101. }
  102. res.isque = '0';
  103. const result = await res.save();
  104. return result;
  105. }
  106. }
  107. module.exports = RoomService;