box_app: add wx-h5-demo standalone project (two mini-program auth scenarios); mount /wx-h5-demo; update auth doc; drop old examples/wx-h5-auth

This commit is contained in:
2026-09-07 16:22:51 +08:00
commit eaaf2b6773
6 changed files with 280 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
// 场景2(也是场景1落地页):web-view 承载 H5 + postMessage 授权 + 回传
// 路径:/pages/demo/webview.js
// H5 地址(独立二级目录项目)
const H5_BASE = 'https://mp.yizhezhe.com/wx-h5-demo/';
const app = getApp();
Page({
data: { url: '', showAuth: false, nickname: '', avatar: '' },
_phone: '', _nick: '', _avatar: '', _openid: '',
onLoad() {
this._openid = app.globalData.openid || 'YOUR_OPENID';
// 场景1:auth-bridge 授权后 redirectTo 本页,结果在 globalData
const r = app.globalData.wxH5Result;
if (r) { this._phone = r.phone || ''; this._nick = r.nickname || ''; this._avatar = r.avatar || ''; }
this.applyUrl();
},
// 拼 H5 url:openid 走 query;授权结果走 hash(webview 丢 query、不丢 hash)
applyUrl() {
const u = H5_BASE + '?openid=' + encodeURIComponent(this._openid) +
'#phone=' + encodeURIComponent(this._phone || '') +
'&nickname=' + encodeURIComponent(this._nick || '') +
'&avatar=' + encodeURIComponent(this._avatar || '');
this.setData({ url: u });
},
// 场景2:H5 通过 postMessage 请求授权
onMessage(e) {
const data = (e.detail && e.detail.data) || [];
(Array.isArray(data) ? data : [data]).forEach((m) => {
if (m && m.action === 'requestProfile') this.setData({ showAuth: true });
});
},
onPhone(e) {
if (e.detail.errMsg === 'getPhoneNumber:ok' && e.detail.code) {
wx.request({ url: 'https://YOUR_API/phone', method: 'POST', data: { code: e.detail.code },
success: r => { this._phone = r.data.phoneNumber; this.finishAuth(); } });
} else { wx.showToast({ title: '未授权手机号', icon: 'none' }); }
},
onNick(e) { this._nick = e.detail.value; this.setData({ nickname: this._nick }); },
onAvatar(e) {
const p = e.detail.avatarUrl;
wx.uploadFile({ url: 'https://YOUR_API/avatar', filePath: p, name: 'file',
success: (res) => { const r = JSON.parse(res.data || '{}'); this._avatar = r.url || p; this.setData({ avatar: this._avatar }); } });
},
finishAuth() { this.setData({ showAuth: false }); this.applyUrl(); }, // 更新 web-view src → H5 重载显示结果
closeAuth() { this.setData({ showAuth: false }); },
noop() {},
});