From bc3531e83c05e694dd4226d64a97654645d6ec9b Mon Sep 17 00:00:00 2001 From: Karlcarlo Date: Mon, 7 Sep 2026 17:38:30 +0800 Subject: [PATCH] =?UTF-8?q?wx-h5-demo:=20scenario1=20'=E5=8E=BB=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E6=8E=88=E6=9D=83'=20in=20webview=20?= =?UTF-8?q?=E2=86=92=20wx.miniProgram.navigateTo(/pages/demo/auth);=20brow?= =?UTF-8?q?ser=20=E2=86=92=20configurable=20LAUNCH=5FSCHEME;=20clarify=20R?= =?UTF-8?q?EADME?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++++- index.html | 15 ++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d1dffea..18a9599 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,10 @@ wx-h5-demo/ - `https://YOUR_API/phone|avatar`(或注释里说明;手机号 `code`→后端换明文;头像需上传换正式地址)。 3. **入口**: - 场景2:宿主页已用 `` 加载 H5,H5 点「在 web-view 里请求授权」即可。 - - 场景1:H5 点「去小程序授权」→ 需给 `jumpToMiniAuth()` 配一个**小程序 URL Scheme**(后端用 appid+path 换)或把页面放进公众号/env 用 ``。 + - 场景1:H5 点「去小程序授权」→ + - **小程序 web-view 内**:已内置 `wx.miniProgram.navigateTo({url:'/pages/demo/auth'})`(无需 scheme)。 + - **微信浏览器(非 web-view)**:给 `LAUNCH_SCHEME` 填一个**小程序 URL Scheme**(后端用 appid+path 换),或把页面放进公众号/网页用 ``(需 jsapi 签名,并在微信内打开)。 + - 场景1 能真正走通的前提:**你的小程序里要有 `/pages/demo/auth`(= `miniapp/scenario1-auth-bridge.*`)和 `/pages/demo/webview`(=`scenario2-host-page.*`),并在 `app.json` 注册这两个页面**;否则 navigateTo 会「页面不存在」。 ## 注意 - 手机号:前端只拿 `code`,**明文必须后端** `wx.business.getuserphonenumber` 换(旧 `code2Session`+解密已收紧)。 diff --git a/index.html b/index.html index 582845c..e03d797 100644 --- a/index.html +++ b/index.html @@ -86,13 +86,18 @@ } // 场景1:跳小程序授权(wx-open-launch-weapp 或 URL Scheme) → 小程序授权页 + var LAUNCH_PATH = '/pages/demo/auth'; // 小程序授权页路径(场景1 的授权页,见 miniapp/scenario1-auth-bridge.*) + var LAUNCH_SCHEME = ''; // 微信浏览器用的 URL Scheme(后端用 appid+path 生成,见 README);留空则走 webview navigateTo function jumpToMiniAuth() { if (MODE === 'browser') { toast('请在微信内打开'); return; } - // 方式A:公众号/网页 JSSDK ``(需 jsapi 签名 + 微信内打开) - // 方式B:小程序 URL Scheme(后端用 appid+path 换 scheme,形如 weixin://dl/business/?t=xxx) - var scheme = $('#launchPath') && $('#launchPath').value; // 你可在下面填 scheme 或 path - if (scheme) { location.href = scheme; return; } - toast('请在此填入小程序授权页的 scheme/launch 入口(见 README)'); + // 小程序 web-view 内:用 wx.miniProgram.navigateTo 跳到授权页(场景1 在 webview 里的做法) + if (window.wx && window.wx.miniProgram && inWebview) { + window.wx.miniProgram.navigateTo({ url: LAUNCH_PATH }); + return; + } + // 微信浏览器(非 web-view):优先 URL Scheme;否则提示用 (需 jsapi 签名) + if (LAUNCH_SCHEME) { location.href = LAUNCH_SCHEME; return; } + toast('请配置小程序 URL Scheme(后端生成)或用 跳授权页'); } // 场景2:postMessage 请求 web-view 宿主弹授权面板