liuyu 4 years ago
parent
commit
086702fc4d
5 changed files with 81 additions and 3 deletions
  1. 4 1
      app/controller/notice.js
  2. 0 1
      app/model/notice.js
  3. 1 0
      app/router.js
  4. 36 0
      app/service/notice.js
  5. 40 1
      app/service/weixin.js

+ 4 - 1
app/controller/notice.js

@@ -13,7 +13,10 @@ class NoticeController extends Controller {
     this.service = this.ctx.service.notice;
   }
 
-
+  async look() {
+    const res = await this.service.look(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
 }
 
 module.exports = CrudController(NoticeController, meta);

+ 0 - 1
app/model/notice.js

@@ -14,7 +14,6 @@ const NoticeSchema = {
   noticeid: { type: String, required: true, maxLength: 200 }, // 通知人id
   content: { type: String, required: true }, // 通知内容
   notified: { type: [ notifiedInfo ], select: true }, // 被通知信息
-
 };
 
 

+ 1 - 0
app/router.js

@@ -205,6 +205,7 @@ module.exports = app => {
   // 通知表设置路由
   router.resources('notice', '/api/train/notice', controller.notice); // index、create、show、destroy
   router.post('notice', '/api/train/notice/update/:id', controller.notice.update);
+  router.post('notice', '/api/train/notice/look', controller.notice.look);
 
   // 课程模板表设置路由
   router.resources('lessonmode', '/api/train/lessonmode', controller.lessonmode); // index、create、show、destroy

+ 36 - 0
app/service/notice.js

@@ -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 + '&noticeid=' + 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();
+    }
   }
 
 }

+ 40 - 1
app/service/weixin.js

@@ -221,6 +221,45 @@ class WeixinAuthService extends AxiosService {
       data: JSON.stringify(requestData),
     });
   }
-}
 
+  // 发送微信模板消息自定义消息
+  async sendTemplateDesign(templateid, openid, first, keyword1, keyword2, remark, tourl) {
+    const url = this.ctx.app.config.sendDirMq + this.ctx.app.config.appid;
+    const requestData = { // 发送模板消息的数据
+      touser: openid,
+      template_id: templateid,
+      url: tourl,
+      data: {
+        first: {
+          value: first,
+          color: '#173177',
+        },
+        keyword1: {
+          value: keyword1,
+          color: '#1d1d1d',
+        },
+        keyword2: {
+          value: keyword2,
+          color: '#1d1d1d',
+        },
+        remark: {
+          value: remark,
+          color: '#173177',
+        },
+      },
+    };
+    console.log('templateid---' + templateid);
+    console.log('openid---' + openid);
+    console.log('requestData---' + JSON.stringify(requestData));
+    await this.ctx.curl(url, {
+      method: 'post',
+      headers: {
+        'content-type': 'application/json',
+      },
+      dataType: 'json',
+      data: JSON.stringify(requestData),
+    });
+  }
+
+}
 module.exports = WeixinAuthService;