小程序刷新回到首页的 bug:子页面(广场/笔记/专题/测一测/计算器/星空页)记本机最后位置,首页被宿主重新加载时回到那一页;与 last_app 比时间 (v26110639)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
49
js/app.js
49
js/app.js
@@ -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) {}
|
||||
|
||||
39
js/last-sub.js
Normal file
39
js/last-sub.js
Normal file
@@ -0,0 +1,39 @@
|
||||
/* 11-07 用户:「在孕妈广场、笔记页或任何一个子页面,在小程序上刷新,刷新结束之后它会回到首页」。
|
||||
原因:小程序刷新 / 重新进入只会加载首页地址;首页只恢复 App 表里的 H5 应用(last_app),首页自己的子页面不在表里。
|
||||
做法:首页仓里的子页面(孕妈广场、笔记、我的投稿、测一测、专题页、计算器)一打开就把当前地址记在本机
|
||||
localStorage「mmbox.lastsub.v1」= {u: 不带身份的路径+查询, h: 首页目录, at};页面里换笔记 / 换话题(pushState / replaceState)也跟着记。
|
||||
首页被宿主重新加载时(没有 referrer、不是后退)按它回到这一页,逻辑在 app.js resumeSub()。
|
||||
从别的应用后退回到子页面时,顺手清掉 last_app(同首页 clearLastAppOnBackHome),免得刷新被送回那个应用。 */
|
||||
(function () {
|
||||
var K = 'mmbox.lastsub.v1';
|
||||
var me = document.currentScript && document.currentScript.src;
|
||||
var home = me ? new URL('../', me).pathname : '';
|
||||
if (!home) return;
|
||||
var DROP = /^(openid|unionid|t|_v|phone|nickname|avatar|__no_identity)$/;
|
||||
function save() {
|
||||
try {
|
||||
var q = new URLSearchParams(location.search), k = [];
|
||||
q.forEach(function (v, n) { if (DROP.test(n)) k.push(n); });
|
||||
k.forEach(function (n) { q.delete(n); });
|
||||
var s = q.toString();
|
||||
localStorage.setItem(K, JSON.stringify({ u: location.pathname + (s ? '?' + s : ''), h: home, at: Date.now() }));
|
||||
} catch (e) {}
|
||||
}
|
||||
save();
|
||||
['pushState', 'replaceState'].forEach(function (fn) {
|
||||
var o = history[fn];
|
||||
if (typeof o !== 'function') return;
|
||||
history[fn] = function () { var r = o.apply(this, arguments); setTimeout(save, 0); return r; };
|
||||
});
|
||||
addEventListener('popstate', save);
|
||||
addEventListener('pageshow', function (e) { if (e.persisted) save(); });
|
||||
|
||||
/* 从别的应用后退回来 → 清 last_app */
|
||||
try {
|
||||
var nav = performance.getEntriesByType && performance.getEntriesByType('navigation')[0];
|
||||
var ref = document.referrer || '';
|
||||
var back = (nav && nav.type === 'back_forward') || (ref.indexOf(location.origin + '/') === 0 && ref.indexOf('/mama-box') < 0);
|
||||
var m = ((location.hash || '') + '&' + (location.search || '')).match(/[#?&]openid=([^&#?]*)/);
|
||||
if (back && m && window.fetch) fetch('/api/mp/public/last-app?openid=' + m[1], { method: 'DELETE', keepalive: true }).catch(function () {});
|
||||
} catch (e) {}
|
||||
})();
|
||||
@@ -70,7 +70,7 @@
|
||||
function repOf(c, me) { return { p: c.parent, n: me ? '我' : (c.nickname || '孕妈'), me: me, to: c.parentName, t: c.text, ts: c.at, st: me ? c.status : '' }; }
|
||||
function load() {
|
||||
if (loading || SIM) return; loading = true;
|
||||
fetch('guangchang/cm.json?v=26110638').then(function (r) { return r.json(); }).then(function (j) {
|
||||
fetch('guangchang/cm.json?v=26110639').then(function (r) { return r.json(); }).then(function (j) {
|
||||
var PP = j.p;
|
||||
SIM = (j.c[PID] || []).map(function (c, k) {
|
||||
var p = PP[c[0]];
|
||||
|
||||
Reference in New Issue
Block a user