debug: 增加免授权直达 H5 流程调试日志 [auth-flow]
This commit is contained in:
@@ -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);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user