微信 jssdk 签名错误

 wechat.config({
      debug: false,
      appId: appId,
      timestamp: timestamp,
      nonceStr: nonceStr,
      signature: signature,
      jsApiList: ['scanQRCode'],
    });

invalid signature 一般都是后端签名有问题 后端的域名要在公众号上配置下

但问题是在iOS下,如果我的另外一个菜单入口是B页面,我从B页面跳转到A页面,这时候我的入口链接被强制变成了A页面,依然会产生签名失败的错误。
let url = "";
// 判断是否是ios微信浏览器 ios要使用当前的url
// if (navigator.userAgent.indexOf('iPhone') !== -1)
if (window.__wxjs_is_wkwebview === true) {
url = this.$store.state.url.split("#")[0];
} else {
url = window.location.href.split("#")[0];
}


所以我们还需要在微信公众号的每一个入口菜单链接里加一个特殊的参数,例如wechat=1,变成这样:https://www.abc.com/abc.html?abc=def&wechat=1

然后我们再增加一层判断,变成这样:

if (navigator.userAgent.indexOf('iPhone') !== -1) {
  if (this.$route.query.wechat !== undefined && this.$route.query.wechat === '1') {
    window.wechaturl = window.location + '';
  }
}

这里我用了vue的写法,但原理是一样的。只有我检测到了wechat这个参数,我才认为当前页面是入口页面,如果没有检测到,则不必强行设置为入口页面。

 
// 处理jssdk签名,兼容history模式
if (!store.state.url) {
store.commit('setUrl', document.URL)
}
原文地址:https://www.cnblogs.com/guidan/p/10299740.html