服务器配置
URL:你服务器地址,不能是IP地址,而且域名必须指向80端口
Token:随机字符串
const sha1 = require("sha1")
router.get("/auth", (req, res, next) => {
// 接受微信返回的相应参数
const { signature, echostr, timestamp, nonce } = req.query
// token是上图配置的字符串
const { token } = config
// 将三个参数字符串拼接成一个字符串进行sha1加密
const sha1Str = sha1([timestamp, nonce, token].sort().join(""))
// 开发者获得加密后的字符串可与微信返回的signature对比,标识该请求来源于微信
if (sha1Str === signature) {
console.log("验证成功")
res.send(echostr)
} else {
res.end("验证失败")
}
})