自学内容网 自学内容网

Node.js 微信公众号-基本配置-服务器URL

1、验证签名

在提交配置时,服务器对应的URL需要实现签名的验证,成功时,需要直接返回 echostr 的值,注意此时URL对应的接口为get请求

                        const { signature, timestamp, nonce, echostr  } = _req.query;

                        const token = "my_token"; // 公众号配置时的Token

                        const tmpArr = [token, timestamp, nonce];

                        tmpArr.sort();

                        // 引入加密模块

                        const crypto = require('crypto');

                        // 创建哈希对象

                        const sha1Hash = crypto.createHash('sha1');

                        // console.log("排序后连接:", tmpArr.join(""))

                        sha1Hash.update(tmpArr.join(""));

                        const hashedData = sha1Hash.digest('hex');

                        // console.log(hashedData, signature);

                        if (hashedData == signature) {

                                res.end(echostr);

                        }

2、消息推送

在用户操作时(关注、取消关注、扫描公众号二维码码等操作时),微信公众号会发送消息到基本配置中的URL对应的接口,注意此时URL对应的接口为post请求,也就是说URL对应的接口需要同时支持get和post请求。而且需要在IP白名单中添加服务器的IP地址

3、完整示例

// router.js

var api = require('./api'


原文地址:https://blog.csdn.net/wuxiaoquan_520/article/details/136005177

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!