From edb084bd7a1c519b74494738a99a800af4e8b2f3 Mon Sep 17 00:00:00 2001 From: Karlcarlo Date: Fri, 31 Jul 2026 11:25:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=8D=E6=8E=88=E6=9D=83=E7=9B=B4?= =?UTF-8?q?=E8=BE=BE=20H5=20=E2=80=94=20=E6=97=A0=20token=20=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E8=B7=B3=E7=99=BB=E5=BD=95=E9=A1=B5=EF=BC=8C=E6=8C=89?= =?UTF-8?q?=E9=9C=80=E6=8E=88=E6=9D=83=EF=BC=88requireAuth=20=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=90=8E=E5=88=B7=E6=96=B0=E8=BA=AB=E4=BB=BD=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/annex/web_view/index.vue | 20 +++++++++++++++----- utils/util.js | 9 +++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/pages/annex/web_view/index.vue b/pages/annex/web_view/index.vue index 3e0c641..904d7d5 100644 --- a/pages/annex/web_view/index.vue +++ b/pages/annex/web_view/index.vue @@ -13,7 +13,6 @@ import store from '@/store'; import { HTTP_REQUEST_URL, TRUSTED_DOMAINS } from '@/config/app'; import { LOGIN_STATUS } from '@/config/cache'; - import { toLogin } from '@/libs/login'; function base64Decode(str) { var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; @@ -138,7 +137,8 @@ export default { }, onShow() { // 登录返回后:页面仍在栈中,onLoad 不会再次触发 - // 检查是否有保存的 URL 和 token,重新处理身份 + // 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)); const targetUrl = this.savedUrl; @@ -150,6 +150,15 @@ export default { console.warn('[web_view] onShow: 仍无 openid,加载原始URL'); this._finalizeUrl(targetUrl); } + } else if (this.url && store.state.app.token) { + // H5 授权登录返回:重新注入最新身份(openid 可能刚获取到) + const { openid, unionid } = getCurrentUser(); + const currentUrl = this.url; + const urlOpenid = getUrlParam(currentUrl, 'openid'); + if (openid && urlOpenid !== openid) { + console.log('[web_view] onShow: 授权返回,刷新身份 openid=', openid); + this._applyIdentity(currentUrl, openid, unionid, urlOpenid); + } } }, methods: { @@ -242,9 +251,10 @@ export default { this._applyIdentity(targetUrl, openid, unionid, urlOpenid); return; // _applyIdentity 内部已调用 _finalizeUrl,避免163重复覆盖 } else { - console.log('[web_view] 无 token,跳转登录'); - this.savedUrl = targetUrl; - toLogin(); + // 无 token:不拦截,匿名直达 H5(openid 为空) + // 用户需要手机号/头像/昵称时,由 H5 调 bridge.requireAuth() 再授权 + console.log('[web_view] 无 token,匿名加载 H5(按需授权)'); + this._finalizeUrl(targetUrl); return; } } else { diff --git a/utils/util.js b/utils/util.js index da7bb4e..366c6f0 100644 --- a/utils/util.js +++ b/utils/util.js @@ -15,7 +15,6 @@ import { } from '../config/app.js'; import { LOGIN_STATUS } from '../config/cache.js'; import store from '../store'; -import { toLogin } from '../libs/login'; import { customerType } from '@/api/api.js' @@ -263,9 +262,11 @@ export default { } console.log('[GoUrl] 最终参数: openid=' + (openid || '(空)') + ', unionid=' + (unionid || '(空)') + ', t=' + Math.floor(Date.now() / 1000)); if (!openid) { - // 先存目标路径,登录后自动跳回 web_view - Cache.set('login_back_url', '/pages/annex/web_view/index?url=' + encodeURIComponent(openUrl)); - toLogin(); + // 无 openid:不拦截,匿名进入 H5(需要身份时由 H5 调 bridge.requireAuth() 再授权) + console.log('[GoUrl] 无 openid,匿名加载 H5(按需授权)'); + uni.navigateTo({ + url: `/pages/annex/web_view/index?url=${encodeURIComponent(openUrl)}` + }); return; } const joiner = url.indexOf('?') !== -1 ? '&' : '?';