Browse Source

微信自动回复功能

ellisran 1 year ago
parent
commit
f86f5f3bbd
6 changed files with 77 additions and 15896 deletions
  1. 32 0
      app/controller/wechat_controller.js
  2. 34 0
      app/lib/common.js
  3. 1 0
      app/router.js
  4. 9 0
      config/config.default.js
  5. 0 15896
      package-lock.json
  6. 1 0
      package.json

+ 32 - 0
app/controller/wechat_controller.js

@@ -17,6 +17,7 @@ const maintainConst = require('../const/maintain');
 const wxConst = require('../const/wechat_template.js');
 const smsTypeConst = require('../const/sms_type');
 const wxWork = require('../lib/wx_work');
+const { parseStringXml } = require('../lib/common');
 
 module.exports = app => {
     class WechatController extends app.BaseController {
@@ -48,6 +49,37 @@ module.exports = app => {
             }
         }
         /**
+         * 微信自动回复
+         *
+         * @param {Object} ctx - egg全局页面
+         * @return {void}
+         */
+        async replyMessage(ctx) {
+            const xml = ctx.request.body;
+            try {
+                const {
+                    createTime, msgType, toUserName, toFromName, event, msgContent,
+                } = await parseStringXml(xml);
+                let body = '';
+                if (msgType === 'text') {
+                    const reply_msg = '如有问题需要咨询,请电话联系0756-3850888;或添加企业QQ:800003850。';
+                    body = `
+            <xml>
+                <ToUserName><![CDATA[${toFromName}]]></ToUserName>
+                <FromUserName><![CDATA[${toUserName}]]></FromUserName>
+                <CreateTime>${createTime}</CreateTime>
+                <MsgType><![CDATA[text]]></MsgType>
+                <Content><![CDATA[${reply_msg}]]></Content>
+                <MediaId><![CDATA[media_id]]></MediaId>
+            </xml>
+        `;
+                }
+                ctx.body = body;
+            } catch (e) {
+                console.log(e);
+            }
+        }
+        /**
          * 微信登录验证
          *
          * @param {Object} ctx - egg全局页面

+ 34 - 0
app/lib/common.js

@@ -0,0 +1,34 @@
+'use strict';
+
+const { parseString } = require('xml2js');
+class Common {
+    parseStringXml(xml) {
+        return new Promise((resolve, reject) => {
+            parseString(xml, async (err, result) => {
+                if (!err) {
+                    const xmlData = result.xml;
+                    const { MsgType, ToUserName, FromUserName, Event, Content } = xmlData;
+                    const createTime = Date.parse(new Date());
+                    const msgType = MsgType[0]; // 消息类型,event
+                    const toUserName = ToUserName[0]; // 开发人员微信号
+                    const toFromName = FromUserName[0]; // 发送方帐号(一个OpenID)
+                    const event = Event ? Event[0] : ''; // 事件类型,subscribe(订阅)、unsubscribe(取消订阅)
+                    const msgContent = Content ? Content[0] : ''; // 消息内容
+                    resolve({
+                        createTime,
+                        msgType,
+                        toUserName,
+                        toFromName,
+                        event,
+                        msgContent,
+                    });
+                } else {
+                    console.log('err******', err);
+                    reject(new Error('解析失败'));
+                }
+            });
+        });
+    }
+}
+
+module.exports = new Common();

+ 1 - 0
app/router.js

@@ -695,6 +695,7 @@ module.exports = app => {
 
     // 微信
     app.get('/wx', 'wechatController.index');
+    app.post('/wx', 'wechatController.replyMessage');
     app.get('/wx/oauth', 'wechatController.oauth');
     app.get('/wx/bind', wechatAuth, 'wechatController.bind');
     app.post('/wx/bindwx', wechatAuth, 'wechatController.bindwx');

+ 9 - 0
config/config.default.js

@@ -179,6 +179,15 @@ module.exports = appInfo => {
     config.bodyParser = {
         jsonLimit: '10mb',
         formLimit: '10mb',
+        queryString: {
+            arrayLimit: 100,
+            depth: 5,
+            parameterLimit: 1000,
+        },
+        enableTypes: ['json', 'form', 'text'],
+        extendTypes: {
+            text: ['text/xml', 'application/xml'],
+        },
     };
 
     config.etag = {

File diff suppressed because it is too large
+ 0 - 15896
package-lock.json


+ 1 - 0
package.json

@@ -48,6 +48,7 @@
         "uglify-es": "^3.3.9",
         "uglify-js": "^3.3.27",
         "uuid": "^3.2.1",
+        "xml2js": "^0.6.2",
         "xmlreader": "^0.2.3",
         "zlib": "^1.0.5"
     },