浏览代码

微信审批通知

laiguoran 5 年之前
父节点
当前提交
282849402a

+ 4 - 0
app/const/sms_type.js

@@ -21,6 +21,7 @@ const smsType = {
     TZ: {
     TZ: {
         name: '台账审批',
         name: '台账审批',
         path: '',
         path: '',
+        wechat: false,
         children: [
         children: [
             { title: '需要我审批', value: 1 },
             { title: '需要我审批', value: 1 },
             { title: '审批结果', value: 2 },
             { title: '审批结果', value: 2 },
@@ -29,6 +30,7 @@ const smsType = {
     XD: {
     XD: {
         name: '修订审批',
         name: '修订审批',
         path: '',
         path: '',
+        wechat: false,
         children: [
         children: [
             { title: '需要我审批', value: 1 },
             { title: '需要我审批', value: 1 },
             { title: '审批结果', value: 2 },
             { title: '审批结果', value: 2 },
@@ -37,6 +39,7 @@ const smsType = {
     JL: {
     JL: {
         name: '计量审批',
         name: '计量审批',
         path: '',
         path: '',
+        wechat: true,
         children: [
         children: [
             { title: '需要我审批', value: 1 },
             { title: '需要我审批', value: 1 },
             { title: '审批结果', value: 2 },
             { title: '审批结果', value: 2 },
@@ -45,6 +48,7 @@ const smsType = {
     BG: {
     BG: {
         name: '变更管理',
         name: '变更管理',
         path: '',
         path: '',
+        wechat: true,
         children: [
         children: [
             { title: '需要我审批', value: 1 },
             { title: '需要我审批', value: 1 },
             { title: '审批结果', value: 2 },
             { title: '审批结果', value: 2 },

+ 29 - 0
app/const/wechat_template.js

@@ -0,0 +1,29 @@
+'use strict';
+
+/**
+ * 微信通知模板
+ *
+ * @author Ellisran
+ * @date 2020/8/10
+ * @version
+ */
+const template = {
+    stage: '5vU3WmR90yDajbs4LWIWH4OQhunYlS1HXTiesIGxrsk',
+    change: '',
+};
+const status = {
+    check: '待审批',
+    back: '退回',
+    success: '通过',
+};
+const tips = {
+    check: '需要您审批',
+    back: '审批退回',
+    success: '审批通过',
+};
+
+module.exports = {
+    template,
+    status,
+    tips,
+};

+ 1 - 1
app/controller/wechat_controller.js

@@ -146,7 +146,7 @@ module.exports = app => {
                 if (projectData === null) {
                 if (projectData === null) {
                     throw '不存在项目数据';
                     throw '不存在项目数据';
                 }
                 }
-                const pa = await ctx.service.projectAccount.getDataByCondition({ code, wx_openid: ctx.session.wechatToken.openid });
+                const pa = await ctx.service.projectAccount.getDataByCondition({ project_id: projectData.id, wx_openid: ctx.session.wechatToken.openid });
                 if (!pa) {
                 if (!pa) {
                     throw '该微信号未绑定此项目';
                     throw '该微信号未绑定此项目';
                 }
                 }

+ 29 - 0
app/extend/helper.js

@@ -17,6 +17,7 @@ const bc = require('../lib/base_calc.js');
 const Decimal = require('decimal.js');
 const Decimal = require('decimal.js');
 Decimal.set({ precision: 50, defaults: true });
 Decimal.set({ precision: 50, defaults: true });
 const SMS = require('../lib/sms');
 const SMS = require('../lib/sms');
+const WX = require('../lib/wechat');
 
 
 module.exports = {
 module.exports = {
     _,
     _,
@@ -977,6 +978,34 @@ module.exports = {
         }
         }
     },
     },
 
 
+
+    async sendWechat(userId, type, judge, template, data = {}) {
+        const wechats = [];
+        if (!userId || (userId instanceof Array && userId.length === 0)) return;
+        const wxUser = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: userId } });
+        for (const user of wxUser) {
+            if (!user.wx_openid || user.wx_openid === '') continue;
+            if (!user.wx_type || user.wx_type === '') continue;
+
+            const wxType = JSON.parse(user.wx_type);
+            if (wxType[type] && wxType[type].indexOf(judge) !== -1) {
+                wechats.push(user.wx_openid);
+            }
+        }
+
+        if (wechats.length > 0) {
+            const wx = new WX(this.ctx);
+            const tenderName = await wx.contentChange(this.ctx.tender.data.name);
+            const projectName = await wx.contentChange(this.ctx.tender.info.deal_info.buildName);
+            const param = {
+                projectName,
+                tenderName,
+            };
+            const postParam = Object.assign(param, data);
+            wx.Send(wechats, template, postParam);
+        }
+    },
+
     /**
     /**
      *
      *
      * @param setting
      * @param setting

+ 90 - 0
app/lib/wechat.js

@@ -0,0 +1,90 @@
+'use strict';
+
+/**
+ * 微信模板信息发送相关接口
+ *
+ * @author ellisran
+ * @date 2018/1/25
+ * @version
+ */
+
+// const xmlReader = require('xmlreader');
+const wxConst = require('../const/wechat_template.js');
+class WX {
+
+    /**
+     * 构造函数
+     *
+     * @param {Object} ctx - egg全局变量
+     * @return {void}
+     */
+    constructor(ctx) {
+        this.ctx = ctx;
+    }
+
+    /**
+     * 发送微信模板
+     *
+     * @param {Array} wx_openid - 发送的微信账号
+     * @param {String} template - 模板id
+     * @param {String} wap_url - wap页面地址
+     * @param {String} tips - 状态文字说明
+     * @param {String} status - 状态
+     * @param {Object} data - 一些模板展示数据
+     * @return {Boolean} - 发送结果
+     */
+    async Send(wx_openid, template, data) {
+        let flag = false;
+        try {
+            // 解绑成功通知
+            const templateId = template;
+            const url = this.ctx.request.header.host + '/wx/url2wap?project=' + data.code + '&url=' + data.wap_url;
+            for (const openid of wx_openid) {
+                if (template === wxConst.template.stage) {
+                    const msgData = {
+                        first: {
+                            value: '您好,本期计量' + data.tips,
+                        },
+                        keyword1: {
+                            value: data.projectName,
+                        },
+                        keyword2: {
+                            value: data.tenderName,
+                        },
+                        keyword3: {
+                            value: '第' + data.qi + '期',
+                        },
+                        keyword4: {
+                            value: data.status,
+                        },
+                        remark: {
+                            value: '查看审批详情,请登录PC端系统。',
+                        },
+                    };
+                    await this.ctx.app.wechat.api.sendTemplate(openid, templateId, url, '', msgData);
+                }
+            }
+            flag = true;
+        } catch (e) {
+            console.log(e);
+            flag = false;
+        }
+        return flag;
+    }
+
+    /**
+     * 关键字转换,并限制20个字以内
+     *
+     * @param {String} content - 内容
+     * @return {Object} - 解析结果
+     */
+    async 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;
+    }
+}
+
+module.exports = WX;

+ 2 - 7
app/middleware/wechat_auth.js

@@ -11,18 +11,13 @@ module.exports = (options, app) => {
         try {
         try {
             // 判断session
             // 判断session
             if (!ctx.session.wechatToken && ctx.query.code) {
             if (!ctx.session.wechatToken && ctx.query.code) {
-                const token = await app.wechat.oauth.getAccessToken(ctx.query.code);
+                const token = await ctx.app.wechat.oauth.getAccessToken(ctx.query.code);
                 ctx.session.wechatToken = token.data;
                 ctx.session.wechatToken = token.data;
             }
             }
             const wechatToken = ctx.session.wechatToken;
             const wechatToken = ctx.session.wechatToken;
             if (!wechatToken) {
             if (!wechatToken) {
-                // ctx.body = encodeURIComponent(ctx.request.url);
-                ctx.body = {
-                    ctx,
-                    host: ctx.request.header.host,
-                };
+                ctx.redirect('/wx/oauth?redirect_uri=' + encodeURIComponent(ctx.request.header.host + ctx.request.url));
                 return;
                 return;
-                // ctx.redirect('/wx/oauth?redirect_uri=' + encodeURIComponent(ctx.request.headers.referer));
             }
             }
             // 同步系统维护信息
             // 同步系统维护信息
             await ctx.service.maintain.syncMaintainData();
             await ctx.service.maintain.syncMaintainData();

+ 67 - 6
app/service/stage_audit.js

@@ -12,6 +12,7 @@ const auditConst = require('../const/audit').stage
 const smsTypeConst = require('../const/sms_type')
 const smsTypeConst = require('../const/sms_type')
 const SMS = require('../lib/sms')
 const SMS = require('../lib/sms')
 const SmsAliConst = require('../const/sms_alitemplate')
 const SmsAliConst = require('../const/sms_alitemplate')
+const wxConst = require('../const/wechat_template')
 const payConst = require('../const/deal_pay')
 const payConst = require('../const/deal_pay')
 const pushType = require('../const/audit').pushType
 const pushType = require('../const/audit').pushType
 
 
@@ -284,7 +285,17 @@ module.exports = app => {
                 await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
                 await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
                     qi: stageInfo.order,
                     qi: stageInfo.order,
                     code: shenpiUrl
                     code: shenpiUrl
-                })
+                });
+
+                // 微信模板通知
+                const wechatData = {
+                    wap_url: shenpiUrl,
+                    qi: stageInfo.order,
+                    status: wxConst.status.check,
+                    tips: wxConst.tips.check,
+                    code: this.session.sessionProject.code,
+                };
+                this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
 
 
                 // todo 更新标段tender状态 ?
                 // todo 更新标段tender状态 ?
                 await transaction.commit()
                 await transaction.commit()
@@ -386,7 +397,16 @@ module.exports = app => {
                     await this.ctx.helper.sendAliSms(nextAudit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
                     await this.ctx.helper.sendAliSms(nextAudit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
                         qi: stageInfo.order,
                         qi: stageInfo.order,
                         code: shenpiUrl
                         code: shenpiUrl
-                    })
+                    });
+                    // 微信模板通知
+                    const wechatData = {
+                        wap_url: shenpiUrl,
+                        qi: stageInfo.order,
+                        status: wxConst.status.check,
+                        tips: wxConst.tips.check,
+                        code: this.session.sessionProject.code,
+                    };
+                    this.ctx.helper.sendWechat(nextAudit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
                 } else {
                 } else {
                     // 本期结束
                     // 本期结束
                     // 生成截止本期数据 final数据
                     // 生成截止本期数据 final数据
@@ -437,7 +457,18 @@ module.exports = app => {
                     await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
                     await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
                         qi: stageInfo.order,
                         qi: stageInfo.order,
                         status: SmsAliConst.status.success
                         status: SmsAliConst.status.success
-                    })
+                    });
+
+                    // 微信模板通知
+                    const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order)
+                    const wechatData = {
+                        wap_url: shenpiUrl,
+                        qi: stageInfo.order,
+                        status: wxConst.status.success,
+                        tips: wxConst.tips.success,
+                        code: this.session.sessionProject.code,
+                    };
+                    this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
                 }
                 }
                 await transaction.commit()
                 await transaction.commit()
             } catch (err) {
             } catch (err) {
@@ -559,7 +590,18 @@ module.exports = app => {
                 await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
                 await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
                     qi: stageInfo.order,
                     qi: stageInfo.order,
                     status: SmsAliConst.status.back
                     status: SmsAliConst.status.back
-                })
+                });
+                // 微信模板通知
+                const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order)
+                const wechatData = {
+                    wap_url: shenpiUrl,
+                    qi: stageInfo.order,
+                    status: wxConst.status.back,
+                    tips: wxConst.tips.back,
+                    code: this.session.sessionProject.code,
+                };
+                this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
+
                 await transaction.commit()
                 await transaction.commit()
             } catch (err) {
             } catch (err) {
                 await transaction.rollback()
                 await transaction.rollback()
@@ -698,7 +740,16 @@ module.exports = app => {
                 await this.ctx.helper.sendAliSms(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
                 await this.ctx.helper.sendAliSms(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
                     qi: stageInfo.order,
                     qi: stageInfo.order,
                     code: shenpiUrl
                     code: shenpiUrl
-                })
+                });
+                // 微信模板通知
+                const wechatData = {
+                    wap_url: shenpiUrl,
+                    qi: stageInfo.order,
+                    status: wxConst.status.check,
+                    tips: wxConst.tips.check,
+                    code: this.session.sessionProject.code,
+                };
+                this.ctx.helper.sendWechat(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
 
 
                 await transaction.commit()
                 await transaction.commit()
             } catch (err) {
             } catch (err) {
@@ -825,7 +876,17 @@ module.exports = app => {
                 await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
                 await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
                     qi: stageInfo.order,
                     qi: stageInfo.order,
                     code: shenpiUrl
                     code: shenpiUrl
-                })
+                });
+                // 微信模板通知
+                const wechatData = {
+                    wap_url: shenpiUrl,
+                    qi: stageInfo.order,
+                    status: wxConst.status.check,
+                    tips: wxConst.tips.check,
+                    code: this.session.sessionProject.code,
+                };
+                this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
+
                 await transaction.commit()
                 await transaction.commit()
             } catch (err) {
             } catch (err) {
                 await transaction.rollback()
                 await transaction.rollback()

+ 2 - 0
app/view/profile/wechat.ejs

@@ -37,6 +37,7 @@
                             <input type="hidden" name="_csrf" value="<%= ctx.csrf %>">
                             <input type="hidden" name="_csrf" value="<%= ctx.csrf %>">
                             <% const user_wxType = accountData.wx_type !== '' ? JSON.parse(accountData.wx_type) : null; %>
                             <% const user_wxType = accountData.wx_type !== '' ? JSON.parse(accountData.wx_type) : null; %>
                             <% for (const s in smsType) { %>
                             <% for (const s in smsType) { %>
+                            <% if (smsType[s].wechat) { %>
                             <div class="form-group row">
                             <div class="form-group row">
                                 <label class="col-auto col-form-label"><%= smsType[s].name %>
                                 <label class="col-auto col-form-label"><%= smsType[s].name %>
                                     <!--<a href="#sms-view" data-toggle="modal" data-target="#sms-view" class="ml-2"><i class="fa fa-info-circle"></i></a>-->
                                     <!--<a href="#sms-view" data-toggle="modal" data-target="#sms-view" class="ml-2"><i class="fa fa-info-circle"></i></a>-->
@@ -51,6 +52,7 @@
                                 </div>
                                 </div>
                             </div>
                             </div>
                             <% } %>
                             <% } %>
+                            <% } %>
                             <input name="type" value="0" type="hidden">
                             <input name="type" value="0" type="hidden">
                             <button type="submit" class="btn btn-primary btn-sm">确认修改</button>
                             <button type="submit" class="btn btn-primary btn-sm">确认修改</button>
                         </form>
                         </form>

+ 1 - 1
config/menu.js

@@ -288,7 +288,7 @@ const profileMenu = {
     },
     },
     wechat: {
     wechat: {
         name: '微信通知',
         name: '微信通知',
-        display: true,
+        display: false,
         url: '/profile/wechat',
         url: '/profile/wechat',
     },
     },
     sign: {
     sign: {