lrf402788946 4 tahun lalu
induk
melakukan
c993a4dbdd
3 mengubah file dengan 53 tambahan dan 21 penghapusan
  1. 5 1
      app/controller/.notice.js
  2. 1 0
      app/router.js
  3. 47 20
      app/service/notice.js

+ 5 - 1
app/controller/.notice.js

@@ -44,7 +44,7 @@ module.exports = {
         noticeid: "noticeid",
         content: "content",
         notified: "notified",
-        type:"type"
+        type: "type",
       },
     },
     service: "query",
@@ -55,4 +55,8 @@ module.exports = {
       count: true,
     },
   },
+  resend: {
+    requestBody: ["!id"],
+    service: "resend",
+  },
 };

+ 1 - 0
app/router.js

@@ -477,6 +477,7 @@ module.exports = app => {
     controller.notice.update
   );
   router.post('notice', '/api/train/notice/look', controller.notice.look);
+  router.post('notice', '/api/train/notice/resend', controller.notice.resend);
 
   // 课程模板表设置路由
   router.resources(

+ 47 - 20
app/service/notice.js

@@ -61,11 +61,9 @@ class NoticeService extends CrudService {
       // 1,判断有没有openid;2有openid的发送消息,添加记录
       if (!_.get(user, 'openid')) continue;
       const openid = _.get(user, 'openid');
-      const remark = '感谢您的使用';
-      const date = await this.ctx.service.util.updatedate();
       const detail = content;
       const tourl = this.ctx.app.config.baseUrl + '/trainnotice/?userid=' + user.uid + '&noticeid=' + res.id;
-      this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知,请点击确认您已收到信息!', detail, date, remark, tourl);
+      await this.toSendWithUrl(openid, detail, tourl);
       const notified = _.get(res, 'notified', []);
       notified.push({ notifiedid: user.uid, username: user.name });
       res.notified = notified;
@@ -134,23 +132,23 @@ class NoticeService extends CrudService {
     }
   }
 
-  async query({ skip, limit, ...info }) {
-    const total = await this.model.count(info);
-    const notices = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
-    const res = [];
-    for (const _notice of notices) {
-      const notice = _.cloneDeep(JSON.parse(JSON.stringify(_notice)));
-      const { noticeid, content, notified, meta } = notice;
-      const elm = [];
-      for (const notified of notice.notified) {
-        const _notified = _.cloneDeep(JSON.parse(JSON.stringify(notified)));
-        const userinfo = await this.findUserInfo(_notified.notifiedid);
-        elm.push({ ...JSON.parse(JSON.stringify(userinfo)), ..._notified });
-      }
-      res.push({ noticeid, content, meta, notified: elm });
-    }
-    return { data: res, total };
-  }
+  // async query({ skip, limit, ...info }) {
+  //   const total = await this.model.count(info);
+  //   const notices = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
+  //   const res = [];
+  //   for (const _notice of notices) {
+  //     const notice = _.cloneDeep(JSON.parse(JSON.stringify(_notice)));
+  //     const { noticeid, content, notified, meta, _id } = notice;
+  //     const elm = [];
+  //     for (const notified of notice.notified) {
+  //       const _notified = _.cloneDeep(JSON.parse(JSON.stringify(notified)));
+  //       const userinfo = await this.findUserInfo(_notified.notifiedid);
+  //       elm.push({ ...JSON.parse(JSON.stringify(userinfo)), ..._notified });
+  //     }
+  //     res.push({ noticeid, content, meta, notified: elm });
+  //   }
+  //   return { data: res, total };
+  // }
 
   async findUserInfo(userid) {
     let userinfo;
@@ -168,6 +166,35 @@ class NoticeService extends CrudService {
     return userinfo;
   }
 
+  async resend({ id }) {
+    const res = await this.model.findById(id);
+    if (res) {
+      const { content, notified } = res;
+      const useridList = notified.filter(f => f.status === '0').map(i => i.notifiedid);
+      const userList = await this.umodel.find({ _id: { $in: useridList } });
+      console.log('in function:');
+      for (const user of userList) {
+        if (!_.get(user, 'openid')) continue;
+        const openid = _.get(user, 'openid');
+        const detail = content;
+        const tourl = this.ctx.app.config.baseUrl + '/trainnotice/?userid=' + user.uid + '&noticeid=' + res.id;
+        await this.toSendWithUrl(openid, detail, tourl);
+      }
+    } else {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
+    }
+  }
+  /**
+   *
+   * @param {String} openid user表的openid
+   * @param {String} detail 通知内容
+   * @param {String} tourl 确认通知地址
+   */
+  async toSendWithUrl(openid, detail, tourl) {
+    const remark = '感谢您的使用';
+    const date = await this.ctx.service.util.updatedate();
+    this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有未读信息需要确认,请点击确认您已收到信息!', detail, date, remark, tourl);
+  }
 }
 
 module.exports = NoticeService;