node生成签名使用

请先看第一篇:node获取存储token和ticket / 生成签名

一:后端

使用:

1. app.js里引入

const setSdk = require('./config/setSdk')
 
2. 前台会post一个url给后台,只需要给setSdk传入前台post的url,并返回给前台就行
ctx.body = await setSdk(ctx.request.body.url)

二:前端

前端需要post给后端一个当前页面的url

 let url = location.href.split('#')[0]
  $.ajax({
    type: 'post',
    url: '接口地址',
    dataType: 'json',
    data: {
      url
    },
    success: (res) => {
      // res是后台返回给前端的签名信息,
      // 用来匹配 wx.config 
       //包括:
        // appId: res.appId,
        // timestamp: res.timestamp,
        // nonceStr: res.nonceStr,
        // signature: res.signature,
    }
  })        
原文地址:https://www.cnblogs.com/chefweb/p/10757509.html