notice.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. const sd = require('silly-datetime');
  8. class NoticeService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'notice');
  11. this.model = this.ctx.model.Notice;
  12. this.umodel = this.ctx.model.User;
  13. this.stumodel = this.ctx.model.Student;
  14. this.schmodel = this.ctx.model.School;
  15. this.heamodel = this.ctx.model.Headteacher;
  16. this.teamodel = this.ctx.model.Teacher;
  17. }
  18. async create(data) {
  19. const { noticeid, content, notified } = data;
  20. assert(noticeid, '通知人id为必填项');
  21. assert(content, '通知内容为必填项');
  22. const res = await this.model.create(data);
  23. if (res) {
  24. for (const elm of res.notified) {
  25. const user = await this.umodel.findOne({ uid: elm.notifiedid });
  26. if (!user) {
  27. continue;
  28. }
  29. if (user.openid) {
  30. const openid = user.openid;
  31. const remark = '感谢您的使用';
  32. const date = await this.ctx.service.util.updatedate();
  33. const detail = '尊敬的' + user.name + ',您有一个新的通知,请及时查收';
  34. const tourl = this.ctx.app.config.baseUrl + '/mobiledirtea/messageInfo/index?uid=' + user.uid + '&noticeid=' + res.id;
  35. this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark, tourl);
  36. }
  37. }
  38. }
  39. }
  40. async look(data) {
  41. const { noticeid, uid } = data;
  42. const notice = await this.model.findById(noticeid);
  43. if (notice) {
  44. for (const elm of notice.notified) {
  45. if (elm.notifiedid === uid) {
  46. elm.status = '1';
  47. elm.readtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
  48. }
  49. }
  50. await notice.save();
  51. }
  52. }
  53. async query({ skip, limit, ...info }) {
  54. const total = await this.model.count(info);
  55. const notices = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
  56. const res = [];
  57. for (const _notice of notices) {
  58. const notice = _.cloneDeep(JSON.parse(JSON.stringify(_notice)));
  59. const { noticeid, content, notified, meta } = notice;
  60. const elm = [];
  61. for (const notified of notice.notified) {
  62. const _notified = _.cloneDeep(JSON.parse(JSON.stringify(notified)));
  63. const userinfo = await this.findUserInfo(_notified.notifiedid);
  64. elm.push({ ...JSON.parse(JSON.stringify(userinfo)), ..._notified });
  65. }
  66. res.push({ noticeid, content, meta, notified: elm });
  67. }
  68. return { data: res, total };
  69. }
  70. async findUserInfo(userid) {
  71. let userinfo;
  72. userinfo = await this.heamodel.findById(userid);
  73. if (!userinfo) {
  74. userinfo = await this.schmodel.findById(userid);
  75. } if (!userinfo) {
  76. userinfo = await this.teamodel.findById(userid);
  77. if (userinfo) {
  78. delete userinfo.status;
  79. }
  80. } if (!userinfo) {
  81. userinfo = await this.stumodel.findById(userid);
  82. }
  83. return userinfo;
  84. }
  85. }
  86. module.exports = NoticeService;