feat: 免授权直达 H5 — 无 token 不再跳登录页,按需授权(requireAuth 返回后刷新身份)

This commit is contained in:
2026-07-31 11:25:39 +08:00
parent 96571fee73
commit edb084bd7a
2 changed files with 20 additions and 9 deletions

View File

@@ -13,7 +13,6 @@
import store from '@/store'; import store from '@/store';
import { HTTP_REQUEST_URL, TRUSTED_DOMAINS } from '@/config/app'; import { HTTP_REQUEST_URL, TRUSTED_DOMAINS } from '@/config/app';
import { LOGIN_STATUS } from '@/config/cache'; import { LOGIN_STATUS } from '@/config/cache';
import { toLogin } from '@/libs/login';
function base64Decode(str) { function base64Decode(str) {
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
@@ -138,7 +137,8 @@ export default {
}, },
onShow() { onShow() {
// 登录返回后:页面仍在栈中,onLoad 不会再次触发 // 登录返回后:页面仍在栈中,onLoad 不会再次触发
// 检查是否有保存的 URL 和 token,重新处理身份 // 1) 首次登录返回(savedUrl 有值,url 未设置)→ 用保存的 URL 重新处理身份
// 2) H5 内 requireAuth 跳登录页返回 → url 已有值,但 token/身份可能刚更新 → 强制刷新注入新身份
if (this.savedUrl && store.state.app.token && !this.url) { if (this.savedUrl && store.state.app.token && !this.url) {
console.log('[web_view] onShow: 登录返回,重新处理 URL:', this.savedUrl.substring(0, 80)); console.log('[web_view] onShow: 登录返回,重新处理 URL:', this.savedUrl.substring(0, 80));
const targetUrl = this.savedUrl; const targetUrl = this.savedUrl;
@@ -150,6 +150,15 @@ export default {
console.warn('[web_view] onShow: 仍无 openid,加载原始URL'); console.warn('[web_view] onShow: 仍无 openid,加载原始URL');
this._finalizeUrl(targetUrl); 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: { methods: {
@@ -242,9 +251,10 @@ export default {
this._applyIdentity(targetUrl, openid, unionid, urlOpenid); this._applyIdentity(targetUrl, openid, unionid, urlOpenid);
return; // _applyIdentity 内部已调用 _finalizeUrl,避免163重复覆盖 return; // _applyIdentity 内部已调用 _finalizeUrl,避免163重复覆盖
} else { } else {
console.log('[web_view] 无 token,跳转登录'); // 无 token:不拦截,匿名直达 H5(openid 为空)
this.savedUrl = targetUrl; // 用户需要手机号/头像/昵称时,由 H5 调 bridge.requireAuth() 再授权
toLogin(); console.log('[web_view] 无 token,匿名加载 H5(按需授权)');
this._finalizeUrl(targetUrl);
return; return;
} }
} else { } else {

View File

@@ -15,7 +15,6 @@ import {
} from '../config/app.js'; } from '../config/app.js';
import { LOGIN_STATUS } from '../config/cache.js'; import { LOGIN_STATUS } from '../config/cache.js';
import store from '../store'; import store from '../store';
import { toLogin } from '../libs/login';
import { import {
customerType customerType
} from '@/api/api.js' } from '@/api/api.js'
@@ -263,9 +262,11 @@ export default {
} }
console.log('[GoUrl] 最终参数: openid=' + (openid || '(空)') + ', unionid=' + (unionid || '(空)') + ', t=' + Math.floor(Date.now() / 1000)); console.log('[GoUrl] 最终参数: openid=' + (openid || '(空)') + ', unionid=' + (unionid || '(空)') + ', t=' + Math.floor(Date.now() / 1000));
if (!openid) { if (!openid) {
// 先存目标路径,登录后自动跳回 web_view // 无 openid:不拦截,匿名进入 H5(需要身份时由 H5 调 bridge.requireAuth() 再授权)
Cache.set('login_back_url', '/pages/annex/web_view/index?url=' + encodeURIComponent(openUrl)); console.log('[GoUrl] 无 openid,匿名加载 H5(按需授权)');
toLogin(); uni.navigateTo({
url: `/pages/annex/web_view/index?url=${encodeURIComponent(openUrl)}`
});
return; return;
} }
const joiner = url.indexOf('?') !== -1 ? '&' : '?'; const joiner = url.indexOf('?') !== -1 ? '&' : '?';