mama-box: 我的页加「退出登录」按钮 + 授权卡改 navigateTo 授权页(替代非实时 postMessage)
This commit is contained in:
37
js/app.js
37
js/app.js
@@ -4141,13 +4141,40 @@
|
||||
b.addEventListener('click', goMall);
|
||||
});
|
||||
|
||||
/* 「授权完善资料」:H5 → 宿主小程序弹原生授权面板(手机号/昵称/头像)
|
||||
宿主授权后写 box_app /profile/auth,并更新 web-view src 带结果回传。 */
|
||||
/* 「授权完善资料」:H5 → navigateTo → 小程序端独立授权页(pages/annex/auth/index)。
|
||||
说明:微信 web-view 的 postMessage 非实时送达(只在分享/后退/onHide 等时机
|
||||
才派发 bindmessage),点按钮时端上收不到 → 改用 wx.miniProgram.navigateTo
|
||||
实时跳转到授权页,避开该限制。 */
|
||||
function goAuth() {
|
||||
haptic();
|
||||
if (window.wx && window.wx.miniProgram) {
|
||||
window.wx.miniProgram.navigateTo({ url: '/pages/annex/auth/index' });
|
||||
} else {
|
||||
toast('请在微信小程序中打开');
|
||||
}
|
||||
}
|
||||
var meAuth = $('#meAuthCard');
|
||||
if (meAuth) meAuth.addEventListener('click', function () {
|
||||
if (meAuth) meAuth.addEventListener('click', goAuth);
|
||||
|
||||
/* 「退出登录」:清除本机身份(openid/unionid/资料展示) → 重新加载页面回到未登录态。
|
||||
入口跳到授权页,便于直接测试授权流程。 */
|
||||
var meLogout = $('#meLogoutCard');
|
||||
if (meLogout) meLogout.addEventListener('click', function () {
|
||||
haptic();
|
||||
if (!(window.wx && window.wx.miniProgram)) { toast('请在微信小程序中打开'); return; }
|
||||
window.wx.miniProgram.postMessage({ data: { action: 'requestProfile' } });
|
||||
toast('请在弹出的小程序面板授权');
|
||||
try { if (location.hash) location.hash = ''; } catch (e) {}
|
||||
// 清除本地读取到的 openid/unionid,回到未登录态
|
||||
try {
|
||||
var base = location.href.split('#')[0];
|
||||
// 保留 query 里的结构与版本戳,仅去掉身份参数
|
||||
var q = (location.search || '').replace(/([?&])openid=[^&]*(&|$)/g, '$1')
|
||||
.replace(/([?&])unionid=[^&]*(&|$)/g, '$1').replace(/\?&$/, '?').replace(/&$/, '');
|
||||
var url = base + (q && q !== '?' ? q : '') ;
|
||||
// 重新加载当前 H5(去掉身份)→ applyRemoteProfile 因无 openid 跳过,页面回到未登录占位
|
||||
location.replace(url + '?t=' + Date.now());
|
||||
} catch (e) {
|
||||
toast('已清除身份');
|
||||
}
|
||||
});
|
||||
|
||||
/* 从商城/后台切回小程序首页时,webview 会再次可见 —— 把 H5 重置回「首页」屏。
|
||||
|
||||
Reference in New Issue
Block a user