123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const sd = require('silly-datetime');
- class NoticeService extends CrudService {
- constructor(ctx) {
- super(ctx, 'notice');
- this.model = this.ctx.model.Notice;
- this.umodel = this.ctx.model.User;
- }
- async create(data) {
- const { noticeid, content, notified } = data;
- assert(noticeid, '通知人id为必填项');
- assert(content, '通知内容为必填项');
- const res = await this.model.create(data);
- if (res) {
- for (const elm of res.notified) {
- const user = await this.umodel.findOne({ uid: elm.notifiedid });
- if (!user) {
- continue;
- }
- if (user.openid) {
- const openid = user.openid;
- const remark = '感谢您的使用';
- const date = await this.ctx.service.util.updatedate();
- const detail = '尊敬的' + user.name + ',您有一个新的通知,请及时查收';
- const tourl = this.ctx.app.config.baseUrl + '/mobiledirtea/messageInfo/index?uid=' + user.uid + '¬iceid=' + res.id;
- this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark, tourl);
- }
- }
- }
- }
- async look(data) {
- const { noticeid, uid } = data;
- const notice = await this.model.findById(noticeid);
- if (notice) {
- for (const elm of notice.notified) {
- if (elm.notifiedid === uid) {
- elm.status = '1';
- elm.readtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
- }
- }
- await notice.save();
- }
- }
- }
- module.exports = NoticeService;
|