fix: 修复 H5 公众号微信 JSSDK 签名 URL 错用 location.origin 导致 wx.config invalid signature,改用 location.href.split('#')[0]

master
YunaiV 2026-05-02 23:20:49 +08:00
parent 0d0017eac7
commit b6b0ca3dbd
1 changed files with 8 additions and 3 deletions

View File

@ -30,8 +30,9 @@ export default {
} }
// 调用后端接口,获得 JSSDK 初始化所需的签名 // 调用后端接口,获得 JSSDK 初始化所需的签名
const url = location.origin; // 微信要求签名 URL 与当前页面去掉 hash 后完全一致;不能直接用 location.origin会丢子路径与 query
const { code, data } = await AuthUtil.createWeixinMpJsapiSignature(url); const signUrl = location.href.split('#')[0];
const { code, data } = await AuthUtil.createWeixinMpJsapiSignature(signUrl);
if (code === 0) { if (code === 0) {
jweixin.config({ jweixin.config({
debug: false, debug: false,
@ -57,7 +58,11 @@ export default {
configSuccess = true; configSuccess = true;
jweixin.error((err) => { jweixin.error((err) => {
configSuccess = false; configSuccess = false;
console.error('微信 JSSDK 初始化失败', err); console.error('[wx-jssdk] config error', err, {
href: location.href,
signUrl,
data,
});
$helper.toast('微信JSSDK:' + err.errMsg); $helper.toast('微信JSSDK:' + err.errMsg);
}); });
jweixin.ready(() => { jweixin.ready(() => {