room.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 updateanchor(id, otheid) {
  76. const res = await this.model.findById(id);
  77. if (res) {
  78. res.otheid = otheid;
  79. const result = await res.save();
  80. if (result) {
  81. const { mq } = this.ctx;
  82. if (mq) {
  83. const exchange = 'othe_publish_' + result.id;
  84. const parm = {
  85. durable: true,
  86. headers: {
  87. userid: result.id,
  88. } };
  89. await mq.fanout(exchange, result.id, JSON.stringify(result), parm);
  90. }
  91. }
  92. }
  93. return res;
  94. }
  95. async roomquest(data) {
  96. const res = await this.model.findById(data.roomid);
  97. if (!res) {
  98. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  99. }
  100. res.queid = data.queid;
  101. res.isque = '1';
  102. const result = await res.save();
  103. if (result) {
  104. const { mq } = this.ctx;
  105. if (mq) {
  106. const exchange = 'quest_publish_' + res.id;
  107. const parm = {
  108. durable: true,
  109. headers: {
  110. userid: res.id,
  111. } };
  112. await mq.fanout(exchange, res.id, JSON.stringify(res), parm);
  113. }
  114. }
  115. return result;
  116. }
  117. async roomquestclose(data) {
  118. const res = await this.model.findById(data.roomid);
  119. if (!res) {
  120. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  121. }
  122. res.isque = '0';
  123. const result = await res.save();
  124. return result;
  125. }
  126. async updateshmai({ id, shmaiid }) {
  127. const res = await this.model.findById(id);
  128. if (res) {
  129. res.shmaiid = shmaiid;
  130. const result = await res.save();
  131. if (result) {
  132. const { mq } = this.ctx;
  133. if (mq) {
  134. const exchange = 'switch_shmai_' + id;
  135. const parm = {
  136. durable: true,
  137. headers: {
  138. userid: res.id,
  139. } };
  140. await mq.fanout(exchange, res.id, JSON.stringify(res), parm);
  141. }
  142. }
  143. }
  144. return res;
  145. }
  146. async switchzjr({ id, switchzjr }) {
  147. const resmodel = await this.model.findById(id);
  148. if (resmodel) {
  149. resmodel.switchzjr = switchzjr;
  150. const result = await resmodel.save();
  151. if (result) {
  152. const { mq } = this.ctx;
  153. if (mq) {
  154. const exchange = 'switch_zjr_' + id;
  155. const parm = {
  156. durable: true,
  157. headers: {
  158. userid: result.id,
  159. } };
  160. await mq.fanout(exchange, result.id, JSON.stringify(result), parm);
  161. }
  162. }
  163. }
  164. return resmodel;
  165. }
  166. }
  167. module.exports = RoomService;