wx-h5-demo: scenario1 '去小程序授权' in webview → wx.miniProgram.navigateTo(/pages/demo/auth); browser → configurable LAUNCH_SCHEME; clarify README

This commit is contained in:
2026-09-07 17:38:30 +08:00
parent eaaf2b6773
commit bc3531e83c
2 changed files with 14 additions and 6 deletions

View File

@@ -30,7 +30,10 @@ wx-h5-demo/
- `https://YOUR_API/phone|avatar`(或注释里说明;手机号 `code`→后端换明文;头像需上传换正式地址)。
3. **入口**:
- 场景2:宿主页已用 `<web-view src="{{url}}">` 加载 H5,H5 点「在 web-view 里请求授权」即可。
- 场景1:H5 点「去小程序授权」→ 需给 `jumpToMiniAuth()` 配一个**小程序 URL Scheme**(后端用 appid+path 换)或把页面放进公众号/env 用 `<wx-open-launch-weapp path="/pages/demo/auth">`。
- 场景1:H5 点「去小程序授权」→
- **小程序 web-view 内**:已内置 `wx.miniProgram.navigateTo({url:'/pages/demo/auth'})`(无需 scheme)。
- **微信浏览器(非 web-view)**:给 `LAUNCH_SCHEME` 填一个**小程序 URL Scheme**(后端用 appid+path 换),或把页面放进公众号/网页用 `<wx-open-launch-weapp path="/pages/demo/auth">`(需 jsapi 签名,并在微信内打开)。
- 场景1 能真正走通的前提:**你的小程序里要有 `/pages/demo/auth`(= `miniapp/scenario1-auth-bridge.*`)和 `/pages/demo/webview`(=`scenario2-host-page.*`),并在 `app.json` 注册这两个页面**;否则 navigateTo 会「页面不存在」。
## 注意
- 手机号:前端只拿 `code`,**明文必须后端** `wx.business.getuserphonenumber` 换(旧 `code2Session`+解密已收紧)。

View File

@@ -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 `<wx-open-launch-weapp>`(需 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;否则提示用 <wx-open-launch-weapp>(需 jsapi 签名)
if (LAUNCH_SCHEME) { location.href = LAUNCH_SCHEME; return; }
toast('请配置小程序 URL Scheme(后端生成)或用 <wx-open-launch-weapp> 跳授权页');
}
// 场景2:postMessage 请求 web-view 宿主弹授权面板