comment.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 CommentService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'live_comment');
  10. this.model = this.ctx.model.Comment;
  11. }
  12. async query({ skip, limit, ...info }) {
  13. const total = await (await this.model.find(info)).length;
  14. const comments = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  15. const newdatas = [];
  16. for (const comment of comments) {
  17. const url = 'http://127.0.0.1:9999/api/auth/user/' + comment.uid;
  18. const user = await this.ctx.curl(url, {
  19. method: 'get',
  20. headers: {
  21. 'content-type': 'application/json',
  22. },
  23. dataType: 'json',
  24. });
  25. const newdata = { ...JSON.parse(JSON.stringify(comment)) };
  26. if (user.data.errcode === 0) {
  27. newdata.uname = user.data.data.name;
  28. }
  29. newdatas.push(newdata);
  30. }
  31. return { data: newdatas, total };
  32. }
  33. }
  34. module.exports = CommentService;