From 3455a834c4e756393a936f763f54002f9c0efe9b Mon Sep 17 00:00:00 2001 From: Karlcarlo Date: Fri, 31 Jul 2026 11:32:02 +0800 Subject: [PATCH] =?UTF-8?q?debug:=20=E5=A2=9E=E5=8A=A0=E5=85=8D=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E7=9B=B4=E8=BE=BE=20H5=20=E6=B5=81=E7=A8=8B=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E6=97=A5=E5=BF=97=20[auth-flow]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/annex/web_view/index.vue | 34 +++++++++++++++++++++++++--------- utils/util.js | 3 ++- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/pages/annex/web_view/index.vue b/pages/annex/web_view/index.vue index 904d7d5..4276719 100644 --- a/pages/annex/web_view/index.vue +++ b/pages/annex/web_view/index.vue @@ -140,14 +140,15 @@ export default { // 1) 首次登录返回(savedUrl 有值,url 未设置)→ 用保存的 URL 重新处理身份 // 2) H5 内 requireAuth 跳登录页返回 → url 已有值,但 token/身份可能刚更新 → 强制刷新注入新身份 if (this.savedUrl && store.state.app.token && !this.url) { - console.log('[web_view] onShow: 登录返回,重新处理 URL:', this.savedUrl.substring(0, 80)); + console.log('[auth-flow] 🔄 onShow: 首次登录返回,重新处理 URL:', this.savedUrl.substring(0, 80)); const targetUrl = this.savedUrl; const { openid, unionid } = getCurrentUser(); const urlOpenid = getUrlParam(targetUrl, 'openid'); + console.log('[auth-flow] onShow 身份: openid=' + (openid || '(空)') + ', unionid=' + (unionid || '(空)')); if (openid) { this._applyIdentity(targetUrl, openid, unionid, urlOpenid); } else { - console.warn('[web_view] onShow: 仍无 openid,加载原始URL'); + console.warn('[auth-flow] ⚠️ onShow: 仍无 openid,加载原始URL'); this._finalizeUrl(targetUrl); } } else if (this.url && store.state.app.token) { @@ -155,9 +156,12 @@ export default { const { openid, unionid } = getCurrentUser(); const currentUrl = this.url; const urlOpenid = getUrlParam(currentUrl, 'openid'); + console.log('[auth-flow] 🔄 onShow: H5 授权返回检测 → openid=' + (openid || '(空)') + ', urlOpenid=' + (urlOpenid || '(无)')); if (openid && urlOpenid !== openid) { - console.log('[web_view] onShow: 授权返回,刷新身份 openid=', openid); + console.log('[auth-flow] ✅ 授权返回,刷新身份 openid=', openid, ', unionid=', unionid || '(空)'); this._applyIdentity(currentUrl, openid, unionid, urlOpenid); + } else { + console.log('[auth-flow] onShow: 身份一致或仍无 openid,不刷新'); } } }, @@ -218,9 +222,15 @@ export default { console.log('[DEBUG 太阳码] 可信域:', isTrustedDomain); if (isTrustedDomain) { const token = store.state.app.token; + console.log('[auth-flow] ===== 进入 H5 跳转流程 ====='); + console.log('[auth-flow] 入口: 太阳码/点击应用'); + console.log('[auth-flow] token:', token ? '有' : '无'); + console.log('[auth-flow] store openid:', store.state.app.openid || '(空)', ', unionid:', store.state.app.unionid || '(空)'); if (token) { let { openid, unionid } = getCurrentUser(); const urlOpenid = getUrlParam(targetUrl, 'openid'); + console.log('[auth-flow] 静默身份: openid=' + (openid || '(空)') + ', unionid=' + (unionid || '(空)')); + console.log('[auth-flow] URL 已有 openid:', urlOpenid || '(无)'); // 收集调试信息 let info = '[debug] token=' + (token ? '有' : '无') + @@ -231,29 +241,33 @@ export default { if (!openid) { // 获取不到身份信息,用 setTimeout 重试 - console.warn('[web_view] 首次获取 openid 为空,延迟重试', info); + console.warn('[auth-flow] ⚠️ 有 token 但 openid 为空,500ms 后重试', info); setTimeout(() => { try { const retry = getCurrentUser(); - console.log('[web_view] 重试后 openid=' + (retry.openid || '(空)')); + console.log('[auth-flow] 重试后 openid=' + (retry.openid || '(空)') + ', unionid=' + (retry.unionid || '(空)')); if (retry.openid) { + console.log('[auth-flow] ✅ 重试成功,注入身份并跳转'); this._applyIdentity(targetUrl, retry.openid, retry.unionid, urlOpenid); } else { + console.warn('[auth-flow] ⚠️ 重试仍无 openid,匿名加载 H5(unionid 无法携带)'); this.url = targetUrl; } } catch (e) { + console.error('[auth-flow] 重试异常,匿名加载:', e); this.url = targetUrl; } }, 500); return; } + console.log('[auth-flow] ✅ 有 openid,注入身份跳转(unionid ' + (unionid ? '已携带' : '为空,可接受') + ')'); this._applyIdentity(targetUrl, openid, unionid, urlOpenid); return; // _applyIdentity 内部已调用 _finalizeUrl,避免163重复覆盖 } else { // 无 token:不拦截,匿名直达 H5(openid 为空) // 用户需要手机号/头像/昵称时,由 H5 调 bridge.requireAuth() 再授权 - console.log('[web_view] 无 token,匿名加载 H5(按需授权)'); + console.log('[auth-flow] 🔓 无 token,匿名直达 H5(不弹授权,按需授权由 H5 requireAuth 触发)'); this._finalizeUrl(targetUrl); return; } @@ -272,6 +286,7 @@ export default { }, _applyIdentity(targetUrl, openid, unionid, urlOpenid) { + console.log('[auth-flow] _applyIdentity: openid=' + (openid || '(空)') + ', unionid=' + (unionid || '(空)') + ', urlOpenid=' + (urlOpenid || '(无)')); if (urlOpenid && urlOpenid !== openid) { targetUrl = stripAuthParams(targetUrl); const joiner = targetUrl.indexOf('?') !== -1 ? '&' : '?'; @@ -279,7 +294,7 @@ export default { targetUrl = targetUrl + joiner + 'openid=' + encodeURIComponent(openid); if (unionid) targetUrl += '&unionid=' + encodeURIComponent(unionid); targetUrl += '&t=' + t; - console.log('[web_view] 替换为当前用户身份'); + console.log('[auth-flow] 🔁 替换为当前用户身份'); } else if (!urlOpenid) { targetUrl = stripAuthParams(targetUrl); const joiner = targetUrl.indexOf('?') !== -1 ? '&' : '?'; @@ -287,10 +302,11 @@ export default { targetUrl = targetUrl + joiner + 'openid=' + encodeURIComponent(openid); if (unionid) targetUrl += '&unionid=' + encodeURIComponent(unionid); targetUrl += '&t=' + t; - console.log('[web_view] 补充身份参数'); + console.log('[auth-flow] ➕ 补充身份参数'); } else { - console.log('[web_view] 身份一致,保留原 URL'); + console.log('[auth-flow] 身份一致,保留原 URL'); } + console.log('[auth-flow] 最终注入 URL:', targetUrl.substring(0, 120)); this.debugMsg += ' [最终URL] ' + targetUrl.substring(0, 120); this._finalizeUrl(targetUrl); }, diff --git a/utils/util.js b/utils/util.js index 366c6f0..221aa22 100644 --- a/utils/util.js +++ b/utils/util.js @@ -263,7 +263,7 @@ export default { console.log('[GoUrl] 最终参数: openid=' + (openid || '(空)') + ', unionid=' + (unionid || '(空)') + ', t=' + Math.floor(Date.now() / 1000)); if (!openid) { // 无 openid:不拦截,匿名进入 H5(需要身份时由 H5 调 bridge.requireAuth() 再授权) - console.log('[GoUrl] 无 openid,匿名加载 H5(按需授权)'); + console.log('[auth-flow] JumpPath: 🔓 无 openid,匿名跳转 H5(不弹授权,按需授权由 H5 requireAuth 触发)'); uni.navigateTo({ url: `/pages/annex/web_view/index?url=${encodeURIComponent(openUrl)}` }); @@ -276,6 +276,7 @@ export default { openUrl += '&unionid=' + encodeURIComponent(unionid); } openUrl += '&t=' + t; + console.log('[auth-flow] JumpPath: ✅ 有 openid,注入身份跳转 web_view (unionid ' + (unionid ? '已携带' : '为空') + ')'); } uni.navigateTo({ url: `/pages/annex/web_view/index?url=${encodeURIComponent(openUrl)}`