notice.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. }
  14. async create(data) {
  15. const { noticeid, content, notified } = data;
  16. assert(noticeid, '通知人id为必填项');
  17. assert(content, '通知内容为必填项');
  18. const res = await this.model.create(data);
  19. if (res) {
  20. for (const elm of res.notified) {
  21. const user = await this.umodel.findOne({ uid: elm.notifiedid });
  22. if (!user) {
  23. continue;
  24. }
  25. if (user.openid) {
  26. const openid = user.openid;
  27. const remark = '感谢您的使用';
  28. const date = await this.ctx.service.util.updatedate();
  29. const detail = '尊敬的' + user.name + ',您有一个新的通知,请及时查收';
  30. const tourl = this.ctx.app.config.baseUrl + '/mobiledirtea/messageInfo/index?uid=' + user.uid + '&noticeid=' + res.id;
  31. this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark, tourl);
  32. }
  33. }
  34. }
  35. }
  36. async look(data) {
  37. const { noticeid, uid } = data;
  38. const notice = await this.model.findById(noticeid);
  39. if (notice) {
  40. for (const elm of notice.notified) {
  41. if (elm.notifiedid === uid) {
  42. elm.status = '1';
  43. elm.readtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
  44. }
  45. }
  46. await notice.save();
  47. }
  48. }
  49. }
  50. module.exports = NoticeService;