小程序刷新回到首页的 bug:子页面(广场/笔记/专题/测一测/计算器/星空页)记本机最后位置,首页被宿主重新加载时回到那一页;与 last_app 比时间 (v26110639)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
deploy
2026-11-07 05:58:27 +00:00
parent 67111af060
commit 76190c5a67
128 changed files with 215 additions and 7 deletions

View File

@@ -3,7 +3,7 @@
(function () {
'use strict';
/* 页面版本号(YYMMDD+序号):显示在「关于兜爸心选」页底部;bump 时同步 head 注释与 ?v= */
var VER = '26110638';
var VER = '26110639';
const $ = (s, c) => (c || document).querySelector(s);
const $$ = (s, c) => Array.from((c || document).querySelectorAll(s));
@@ -195,6 +195,46 @@
} catch (e) { try { console.warn('[resume] reportLastApp fail:', e); } catch (e2) {} }
}
/* ================= 子页面恢复(2026-11-07 用户)=================
「在孕妈广场、笔记页或任何一个子页面,在小程序上刷新,刷新结束之后它会回到首页」——
小程序刷新 / 重新进入只加载首页;首页仓自己的子页面不在 App 表里,last_app 记不住。
子页面由 js/last-sub.js 把当前地址记在本机 mmbox.lastsub.v1;星空页开着时 whQs 也记一条 ?wh=1。
首页被**宿主**加载(没有同源 referrer、不是后退)且只在小程序里时,回到那一页;
同时有 last_app 时比时间,谁新回谁。用户自己点回首页(同源 referrer / 后退)= 想待在首页 → 清掉记录。
返回 true = 已经在跳了,后面的 resumeLastApp 不用再跑。 */
const SUB_KEY = 'mmbox.lastsub.v1';
function homeDir() { return location.pathname.replace(/[^/]*$/, ''); }
function resumeSub() {
try {
if (/[?&](wh|wc|go|demo)=/.test(location.search) || hashParam('norestore')) return false;
const ref = document.referrer || '';
if (ref.indexOf(location.origin + '/') === 0 || isBackToThisPage()) { localStorage.removeItem(SUB_KEY); return false; }
const inMp = window.__wxjs_environment === 'miniprogram' || /miniProgram/i.test(navigator.userAgent);
if (!inMp) return false;
const sub = JSON.parse(localStorage.getItem(SUB_KEY) || 'null');
if (!sub || !sub.u || sub.h !== homeDir() || sub.u.indexOf(homeDir()) !== 0 || Date.now() - sub.at > 7 * 864e5) return false;
const go = function () {
console.log('[resume] 回到上次停的子页面:', sub.u);
location.replace(withIdentity(sub.u) + location.hash); /* 身份 query + 原 hash 都带上(子页面 hash/query 双读) */
};
const oid = hashParam('openid');
try { sessionStorage.setItem(RESUME_DONE_KEY, '1'); } catch (e) {}
if (!oid) { go(); return true; }
fetch(LAST_APP_API + '?openid=' + encodeURIComponent(oid))
.then(r => (r.ok ? r.json() : null))
.then(d => {
const rec = d && d.result, t = rec && Date.parse(rec.at);
if (rec && rec.url && t > sub.at && urlBase(rec.url) !== urlBase(location.href)) { /* 后来又进了别的应用:回那个应用 */
const target = withIdentity(rec.url);
reportHostLocation(target);
location.replace(target);
} else go();
})
.catch(go);
return true;
} catch (e) { return false; }
}
/* 首屏恢复:读一次 last_app,命中就带着当前身份 replace 过去。 */
function resumeLastApp() {
try {
@@ -3749,7 +3789,7 @@
clearLastAppOnBackHome(false);
/* 冷启动恢复:读一次 last_app,命中就跳到上次进入的 H5 应用(会话内只尝试一次,
见 resumeLastApp)。放在最前面,尽量少闪一下首页。 */
resumeLastApp();
if (!resumeSub()) resumeLastApp();
/* 首屏向小程序宿主上报当前位置:宿主据此解析分享卡片(见 reportHostLocation 说明)。 */
if (window.wxAuth && window.wxAuth.reportLocation) window.wxAuth.reportLocation();
/* 默认预产期是「今天 + 3 个月」现算出来的,标记里那些
@@ -4848,6 +4888,11 @@
if (on) q.set('wh', '1'); else { q.delete('wh'); q.delete('wc'); }
const u = location.pathname + (q.toString() ? '?' + q : '') + location.hash;
if (u !== location.pathname + location.search + location.hash) history.replaceState(history.state, '', u);
try { /* 星空页开着也算一个子页面:刷新回来还在星空页(见 resumeSub) */
const cur = JSON.parse(localStorage.getItem('mmbox.lastsub.v1') || 'null');
if (on) localStorage.setItem('mmbox.lastsub.v1', JSON.stringify({ u: location.pathname + '?wh=1', h: location.pathname.replace(/[^/]*$/, ''), at: Date.now() }));
else if (cur && /[?&]wh=1/.test(cur.u)) localStorage.removeItem('mmbox.lastsub.v1');
} catch (e) {}
const th = window.MB_WH_THEME ? window.MB_WH_THEME() : null;
if (window.wxAuth && wxAuth.reportLocation) wxAuth.reportLocation(on ? { title: th ? '本周话题:' + th.name : '孕妈悄悄话', imageUrl: new URL('assets/share/wh-star.jpg', document.baseURI).href } : {}); /* 转发卡 = 星空图 */
} catch (e) {}