浏览代码

项目信息通知微信通知

ellisran 1 年之前
父节点
当前提交
104b62df37
共有 4 个文件被更改,包括 44 次插入1 次删除
  1. 30 0
      app/controller/dashboard_controller.js
  2. 7 0
      app/extend/helper.js
  3. 1 1
      app/service/message.js
  4. 6 0
      app/service/project_account.js

+ 30 - 0
app/controller/dashboard_controller.js

@@ -12,6 +12,7 @@ const auditConst = require('../const/audit');
 const officeList = require('../const/cld_office').list;
 const maintainConst = require('../const/maintain');
 const typeColMap = require('../const/advance').typeColMap;
+const moment = require('moment');
 
 module.exports = app => {
 
@@ -207,6 +208,35 @@ module.exports = app => {
                 ctx.helper.validate(rule);
                 const result = await ctx.service.message.save(id, ctx.request.body, ctx.session.sessionUser, ctx.session.sessionProject.id);
                 if (result) {
+                    // 新增的项目通知会发送微信模版消息通知客户
+                    if (id === 0) {
+                        // 获取该项目所有的openid,发送信息
+                        const wechats = await ctx.service.projectAccount.getOpenIdListByPid(ctx.session.sessionProject.id);
+                        if (wechats.length > 0) {
+                            const msgInfo = await ctx.service.message.getDataById(result);
+                            const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
+                            // 绑定成功通知
+                            const templateId = 'VKUo4us4lt2dQY0EaaJxcui2jkjmriN3A0K7i4kpZwY';
+                            const url = ctx.protocol + '://' + ctx.host + '/wap/dashboard/msg/' + msgInfo.id;
+                            const msgData = {
+                                thing21: {
+                                    value: projectData.code,
+                                },
+                                thing2: {
+                                    value: ctx.helper.contentChange(msgInfo.title),
+                                },
+                                thing8: {
+                                    value: msgInfo.creator,
+                                },
+                                time3: {
+                                    value: moment(msgInfo.release_time * 1000).format('YYYY-MM-DD'),
+                                },
+                            };
+                            for (const wx of wechats) {
+                                await app.wechat.api.sendTemplate(wx.wx_openid, templateId, url, '', msgData);
+                            }
+                        }
+                    }
                     ctx.redirect('/dashboard/msg/list');
                 }
             } catch (error) {

+ 7 - 0
app/extend/helper.js

@@ -1676,4 +1676,11 @@ module.exports = {
         qtys.qc_minus_qty = qtys.qc_minus_qty ? qtys.qc_minus_qty : null;
         return qtys;
     },
+    contentChange(content) {
+        let str = content.replace(/【/g, '(');
+        str = str.replace(/】/g, ')');
+        str = str.replace(/工程款/g, '***');
+        str = str.length > 20 ? str.substring(0, 17) + '...' : str;
+        return str;
+    },
 };

+ 1 - 1
app/service/message.js

@@ -115,7 +115,7 @@ module.exports = app => {
             const operate = id === 0 ? await this.db.insert(this.tableName, data) :
                 await this.db.update(this.tableName, data);
 
-            const result = operate.affectedRows > 0;
+            const result = operate.affectedRows > 0 ? operate.insertId : false;
 
             return result;
         }

+ 6 - 0
app/service/project_account.js

@@ -936,6 +936,12 @@ module.exports = app => {
             const result = await this.getDataById(id);
             return result ? result.self_category_level : '';
         }
+
+        async getOpenIdListByPid(pid) {
+            const sql = 'SELECT `wx_openid` FROM ?? WHERE `project_id` = ? AND `wx_openid` is not null';
+            const param = [this.tableName, pid];
+            return await this.db.query(sql, param);
+        }
     }
 
     return ProjectAccount;