项目结构

node.js 微信开发2-消息回复、token获取、自定义菜单

>config/wechat.json 微信公众号的配置文件

>controllers/oauth.js 微信网页授权接口(下一篇再细讲讲)

>controllers/wechat.js 微信公众号接口(包括接入接口和其他调用微信api的接口)

>wechat/access_token.json 请求微信api接口之前都需要使用的access_token

>wechat/crytoGraphy.js 加密解密文件(这里使用的是明文方式,未用到)

>wechat/menus.json 微信公众号的自定义菜单

>wechat/wechat.js 调用微信api的方法

项目的架构说明和使用步骤可以参考前面一篇的‘node.js 接口调用示例’: https://www.cnblogs.com/eye-like/p/11743744.html

 

一些说明点

1、公众号的接入接口和接收消息接口

两个接口的地址是一致的,区别是:

> 接入接口是GET请求,需要的是对get的接口参数进行解析验证,然后按需返回就可以了

> 接收消息接口是POST请求,需要先解析微信发送过来的消息,然后根据情况决定是否返回消息

WeChat.prototype.handleMsg = async function (ctx) {
            return new Promise((resolve, reject) => {

                // let req = ctx.request;
                // let res = ctx.response;
                let req = ctx.req;
                var buffer = [],
                    that = this;

                //实例微信消息加解密
                // var cryptoGraphy = new CryptoGraphy(that.config,ctx.request);
                //监听 data 事件 用于接收数据
                req.on('data', function (data) {
                    // logger.info("on data", data);
                    buffer.push(data);
                });
                req.on('end', function () {
                    // logger.info("on end");
                    var msgXml = Buffer.concat(buffer).toString('utf-8');

                    parseString(msgXml, {
                        explicitArray: false
                    }, function (err, result) {
                        // logger.info("on result", result);
                        result = result.xml;
                        resolve(result)
                    })
                });
            })

        },
接收微信消息

相关文章:

  • 2021-06-29
  • 2021-05-30
  • 2022-12-23
  • 2021-12-04
  • 2022-01-01
  • 2022-12-23
  • 2021-12-29
  • 2021-04-28
猜你喜欢
  • 2021-07-13
  • 2022-12-23
  • 2021-09-14
  • 2021-09-05
  • 2022-12-23
  • 2022-02-16
  • 2021-12-04
相关资源
相似解决方案