fix: 冷启动竞态 — 无 token 时延迟重试等待静默授权,仍无则匿名直达

This commit is contained in:
2026-07-31 11:37:35 +08:00
parent 3455a834c4
commit a600dc05f7

View File

@@ -265,10 +265,30 @@ export default {
this._applyIdentity(targetUrl, openid, unionid, urlOpenid);
return; // _applyIdentity 内部已调用 _finalizeUrl,避免163重复覆盖
} else {
// 无 token:不拦截,匿名直达 H5(openid 为空)
// 用户需要手机号/头像/昵称时,由 H5 调 bridge.requireAuth() 再授权
console.log('[auth-flow] 🔓 无 token,匿名直达 H5(不弹授权,按需授权由 H5 requireAuth 触发)');
this._finalizeUrl(targetUrl);
// 无 token:可能是冷启动竞态(onLaunch 静默授权尚未完成)
// 延迟重试等待 token,最多 3 次;仍无则匿名直达(不弹授权)
let retryCount = 0;
const maxRetry = 3;
const tryLoad = () => {
const t2 = store.state.app.token;
console.log('[auth-flow] ⏳ 无 token,第 ' + (retryCount + 1) + ' 次等待静默授权...');
if (t2) {
let { openid, unionid } = getCurrentUser();
console.log('[auth-flow] ✅ 静默授权完成: token=有, openid=' + (openid || '(空)') + ', unionid=' + (unionid || '(空)'));
if (openid) {
this._applyIdentity(targetUrl, openid, unionid, getUrlParam(targetUrl, 'openid'));
return;
}
}
retryCount++;
if (retryCount < maxRetry) {
setTimeout(tryLoad, 800);
} else {
console.warn('[auth-flow] ⚠️ 等待 ' + maxRetry + ' 次仍无 token,匿名直达 H5(按需授权由 H5 requireAuth 触发)');
this._finalizeUrl(targetUrl);
}
};
tryLoad();
return;
}
} else {