wx-h5-demo: 授权改 navigateTo 独立授权页(pages/annex/auth/index) 替代非实时 postMessage

This commit is contained in:
2026-09-07 20:59:38 +08:00
parent 3f47899643
commit 935fee400e
2 changed files with 11 additions and 9 deletions

View File

@@ -63,9 +63,9 @@
<p class="hint">
本页演示<b>共享授权规范</b>:<code>wx-auth.js</code>(<code>wxAuth.init/requestProfile/goMall/readAuth/isAuthorized</code>)。
授权流程:点「授权登录」→ <code>wx.miniProgram.postMessage({action:'requestProfile'})</code> →
通用宿主页 <code>pages/annex/web_view</code> 弹原生授权面板(头像/昵称/手机号)→ <code>POST /api/mp/public/auth/profile</code> 落库共享 UserAuth →
宿主页更新 <code>web-view src</code>(hash 带结果)→ 本页重载 → <code>wxAuth.readAuth()</code> 显示。
授权流程:点「授权登录」→ <code>wx.miniProgram.navigateTo('/pages/annex/auth/index')</code> →
小程序端独立授权页(头像/昵称/手机号,非 web-view 原生组件页)→ <code>POST /api/mp/public/auth/profile</code> 落库共享 UserAuth →
授权返回后宿主页更新 <code>web-view src</code>(hash 带结果)→ 本页重载 → <code>wxAuth.readAuth()</code> 显示。
「进商城」→ <code>wx.miniProgram.switchTab('/pages/goods_cate/goods_cate')</code>。
</p>
@@ -97,7 +97,7 @@
/* ============================================================
① 页面版本号(YYMMDD + 两位序号)—— 用于排查 webview 是否缓存了旧页面
============================================================ */
var VER = '26090702';
var VER = '26090703';
var verNoEl = el('verNo');
if (verNoEl) verNoEl.textContent = VER;
@@ -217,7 +217,7 @@
if (!(window.wx && window.wx.miniProgram)) {
dbgErr('requestProfile 被拒绝:window.wx.miniProgram 不存在(不在微信 web-view 或 jweixin 未加载)');
} else {
dbg('requestProfile 前置检查通过(wx.miniProgram 存在),开始发送 postMessage', 'ok');
dbg('requestProfile 前置检查通过(wx.miniProgram 存在),开始 navigateTo 授权页', 'ok');
}
wxAuth.requestProfile();
});

View File

@@ -55,11 +55,13 @@
var a = this.readAuth();
return !!(a.nickname || a.avatar || a.phone);
},
/* ④ 请求授权:postMessage → 通用宿主页(annex/web_view)弹原生授权面板 */
requestProfile: function () {
/* ④ 请求授权:navigateTo → 小程序端独立授权页(默认 pages/annex/auth/index)。
改动说明:微信 web-view 的 postMessage 非实时送达,点击时端上收不到;
改用 wx.miniProgram.navigateTo 实时跳转到授权页,避开该限制。 */
requestProfile: function (authPage) {
if (!(window.wx && window.wx.miniProgram)) { this.toast('请在微信小程序中打开'); return; }
window.wx.miniProgram.postMessage({ data: { action: 'requestProfile' } });
this.toast('请在弹出的小程序面板授权');
window.wx.miniProgram.navigateTo({ url: authPage || '/pages/annex/auth/index' });
this.toast('正在打开授权页…');
},
/* ⑤ 跳小程序商城(tabBar 页用 switchTab;非 tabBar 用 navigateTo) */
goMall: function (url) {