本文主要记录项目中遇到的坑,其余可参考微信文档详情戳here

欢迎大神评论区批评指正

一、填写服务器配置(需要校验 URL 才可提交成功)

微信小程序推送消息加解密之node实现

URL:开发者实现HTTP/HTTPS接口,用于微信返回信息

Token & EncodingAESKey:代码中会用,用于校验是否是微信返回给开发者的信息

消息加密方式:选择明文模式,上面的URL必须是HTTPS

注意:当点击提交按钮时,微信会请求上面的URL进行校验,此时需要走第二步

二、验证消息的确来自微信服务器

微信小程序推送消息加解密之node实现

代码实现:

 1 function(token, timestamp, nonce, sig) {
 2         try {
 3             let sortList = [token, timestamp, nonce]
 4             sortList.sort() 
 5             let sha = crypto.createHash('sha1');  // 加密
 6             sha.update(sortList.join(""))
 7             return sha.digest('hex') == sig
 8         } catch (e) {
 9             console.log(e, 'getSHA1 error')
10         }
11         return false
12     }
13 
14 router.get('/api/wx_media_check', async(ctx) => {
15     let ret = wxVerify.checkSig(config.wxMsgCheck.token, ctx.query.timestamp, ctx.query.nonce, ctx.query.signature)
16     if (ret) {
17         ctx.body = ctx.query.echostr
18     } else {
19         console.error('wx check signature error')
20         ctx.body = ''
21     }
22 })
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2023-02-04
  • 2021-12-05
  • 2021-11-20
  • 2022-01-26
  • 2021-04-08
猜你喜欢
  • 2021-09-24
  • 2022-12-23
  • 2021-12-30
  • 2022-12-23
  • 2022-12-23
  • 2021-04-11
  • 2022-12-23
相关资源
相似解决方案