lookuser.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const moment = require('moment');
  5. const { ObjectId } = require('mongoose').Types;
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. const { BusinessError, ErrorCode } = require('naf-core').Error;
  8. class LookuserService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'lookuser');
  11. this.model = this.ctx.model.Lookuser;
  12. this.rmodel = this.ctx.model.Lookrecord;
  13. this.umodel = this.ctx.model.Roomuser;
  14. }
  15. async create(data) {
  16. const { roomname, userid } = data;
  17. const lookuser = await this.model.findOne({ roomname, userid });
  18. const time = moment().format('YYYY-MM-DD HH:mm:ss');
  19. if (!lookuser) {
  20. data.createtime = time;
  21. await this.model.create(data);
  22. }
  23. const newdata = { roomid: data.roomid, roomname: data.roomname, userid, username: data.username, createtime: time, type: '0', isonline: '1' };
  24. await this.rmodel.create(newdata);
  25. }
  26. async update({ id }, data) {
  27. const { switchrole } = data;
  28. const lookuser = await this.model.findById(id);
  29. if (!lookuser) {
  30. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  31. }
  32. lookuser.switchrole = switchrole;
  33. const res = await lookuser.save();
  34. if (res) {
  35. if (switchrole === 'anchor') {
  36. const { mq } = this.ctx;
  37. if (mq) {
  38. const exchange = 'switch_role_' + res.userid;
  39. const parm = {
  40. durable: true,
  41. headers: {
  42. userid: res.userid,
  43. } };
  44. await mq.fanout(exchange, res.userid, JSON.stringify(res), parm);
  45. }
  46. } else {
  47. const url = `http://127.0.0.1:6666/api/stomp/exitroom?uid=${res.userid}&roomid=${res.roomname}`;
  48. await this.ctx.curl(url, {
  49. method: 'get',
  50. headers: {
  51. 'content-type': 'application/json',
  52. },
  53. });
  54. }
  55. }
  56. return res;
  57. }
  58. async roomcount(data) {
  59. const { roomname } = data;
  60. const total = await this.model.count({ roomname });
  61. const onlinetotal = await this.model.count({ roomname, isonline: '1' });
  62. return { total, onlinetotal };
  63. }
  64. async userswichrole(data) {
  65. const { roomid, userid } = data;
  66. const res = await this.model.findOne({ roomid, userid });
  67. return res;
  68. }
  69. async updatexf(data) {
  70. const { roomid, isxf, userid, hosname, deptname, level, major } = data;
  71. const lookuser = await this.model.findOne({ roomid, userid });
  72. if (!lookuser) {
  73. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  74. }
  75. lookuser.isxf = isxf;
  76. lookuser.hosname = hosname;
  77. lookuser.deptname = deptname;
  78. lookuser.level = level;
  79. lookuser.major = major;
  80. const res = await lookuser.save();
  81. return res;
  82. }
  83. async query({ skip, limit, ...info }) {
  84. const total = await this.model.count(info);
  85. const lusers = await this.model
  86. .find(info)
  87. .skip(Number(skip))
  88. .limit(Number(limit));
  89. const data = [];
  90. for (const _luser of lusers) {
  91. const luser = _.cloneDeep(JSON.parse(JSON.stringify(_luser)));
  92. const user = await this.umodel.findById(_luser.userid);
  93. if (user) {
  94. luser.idnumber = user.number;
  95. luser.isjc = user.isjc;
  96. luser.isxf = user.isxf;
  97. luser.address = user.address;
  98. luser.age = user.age;
  99. luser.gender = user.gender;
  100. if (user.hosname) {
  101. luser.hosname = user.hosname;
  102. }
  103. if (user.deptname) {
  104. luser.deptname = user.deptname;
  105. }
  106. if (user.level) {
  107. luser.level = user.level;
  108. }
  109. if (user.major) {
  110. luser.major = user.major;
  111. }
  112. }
  113. const lrecords = await this.rmodel.find({ roomid: _luser.roomid, userid: _luser.userid }).sort({ createtime: -1 }).limit(1);
  114. if (lrecords.length > 0) {
  115. const lrecord = lrecords[0];
  116. luser.exittime = lrecord.createtime;
  117. const start_date = moment(luser.createtime, 'YYYY-MM-DD HH:mm:ss');
  118. const end_date = moment(lrecord.createtime, 'YYYY-MM-DD HH:mm:ss');
  119. // 秒
  120. const seconds = end_date.diff(start_date, 'seconds');
  121. // 分钟
  122. const mintus = Math.ceil(seconds / 60);
  123. luser.looktime = mintus;
  124. }
  125. data.push(luser);
  126. }
  127. return { data, total };
  128. }
  129. async updateonline(data) {
  130. const { roomid, userid, isonline } = data;
  131. const lookuser = await this.model.findOne({ roomid, userid });
  132. if (!lookuser) {
  133. throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  134. }
  135. lookuser.isonline = isonline;
  136. const res = await lookuser.save();
  137. return res;
  138. }
  139. }
  140. module.exports = LookuserService;