|
@@ -6,11 +6,47 @@ 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 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) {
|
|
|
+ const res = await notice.notified.id(uid);
|
|
|
+ res.status = '1';
|
|
|
+ res.readtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
|
|
|
+ await notice.save();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|