config:invalid signature (iOS system is normal after refresh)

Note: It needs to be debugged on the real machine after deployment

There are several cases of signature errors like this

1. There is no problem with Android phones, and Apple phones will be fine after refreshing

2. The realAuthUrl error path following the signature is consistent with the current path or initially inconsistent, and it is consistent after refreshing

3. The realAuthUrl error reporting path followed by the signature is different from the current path for the first time and after refreshing

 Case 1: There is no problem with the Android system, and it will be fine after the Apple system is refreshed

This is because the path that the Apple mobile phone enters is fixed and will not change in essence. The path from which to enter is the path. At this time, an error will be reported when using the current path to request

Solution: save the path when entering for the first time

if (typeof window.entryUrl === 'undefined' || window.entryUrl === '') {
	window.entryUrl = location.href.split('#')[0]
}

when using it

//判断是否时安卓系统获取路径
let signLink = /(Android)/i.test(navigator.userAgent) ? location.href : window.entryUrl;
//刷新后路径变化使用当前路径
if (encodeURIComponent(signLink) == 'undefined') signLink = location.href

Case 2: The realAuthUrl error path following the signature is consistent with the current path or inconsistent at the beginning, and consistent after refreshing 

In this case, we need to consider whether the obtained signature is correct. Note: Inconsistent appids will also cause errors

We can use the verification tool provided by WeChat: https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign

wx.config({
	debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
	appId: '自己公众号的appid', // 必填,企业号的唯一标识,此处填写企业号corpid
	timestamp: timestamp, // 必填,生成签名的时间戳
	nonceStr: noncestr, // 必填,生成签名的随机串
	signature: signature, // 必填,签名
	jsApiList: [''], // 必填,需要使用的JS接口列表
	openTagList: ['']
}),
wx.ready(() => {
	console.log('ready')
});
wx.error(function(res1) {
	console.log('error', res1, res1.errMsg)
});

The third case: the realAuthUrl error reporting path followed by the signature is different from the current path for the first time and after refreshing

This situation is simple, that is, the current path is inconsistent with the path you requested, because the url at the time of execution is consistent with the url at the time of signing, and it will pass

We can also check the official document to find the problem: document https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#%E9%99%84%E5%BD%951-JS-SDK %E4%BD%BF%E7%94%A8%E6%9D%83%E9%99%90%E7%AD%BE%E5%90%8D%E7%AE%97%E6%B3%95

Guess you like

Origin blog.csdn.net/qq_53590046/article/details/128544811