chore: 入库生产基线(2026-10-26 工作树快照)

- 含首页壳/容器页/授权页/分享图/首页配置等此前未入库的在途改动
- 新增未跟踪的 pages/index/mall.vue(第 5 个 tabBar 页,pages.json 已引用)
- 排除本地备份文件(*.bak* / *.origin-bak)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-26 17:07:49 +08:00
parent 91f1ee849c
commit b45a190a59
14 changed files with 4116 additions and 2446 deletions

View File

@@ -5,11 +5,12 @@
本页替代了 CRMEB 原装修首页(原文件完整备份在同目录
index.vue.origin-bak,恢复方法见本文件底部注释)。
链路(2026-09 起:mama-box 不再是 box_app 的「应用」,URL 硬编码):
onLoad → loadMamaBox()
→ webUrl = https://mp.yizhezhe.com/mama-box/ + #openid=xxx(buildWebUrl)
链路(2026-10-08 起:首页地址后台可定制,不再写死):
onLoad → _homeUrlReady()(拉 box_app GET /api/mp/public/home/config;
本地缓存兜底,都没有才用内置默认 MAMA_HOME)
→ loadMamaBox() → webUrl = 生效首页地址(默认 /mama-box/)+ #openid=xxx(buildWebUrl)
→ <web-view> 加载该 H5
(更新首页 = 直接改服务端 box_app/mama-box/ 目录,无需发版即可刷新内容)
(更新首页内容 = 改服务端对应 H5 目录;换首页地址 = 管理后台「首页配置」,均无需发版)
两个坑(都来自 app_03 前一次接入的实测结论):
1. 微信 web-view 首次加载会丢弃 URL 的 query string(?a=b),
@@ -68,8 +69,18 @@
const app = getApp();
// mama-box H5 托管地址(硬编码;更新首页 = 改服务端 box_app/mama-box/ 目录)
// 内置兜底首页地址(2026-10-08 起不再是唯一来源):
// 生效地址 = box_app 后台配置(GET /api/mp/public/home/config)→ 本地缓存 → 这里。
// 管理后台清空配置 = 回到本值;后端 DEFAULT_HOME_URL 与此保持一致。
const MAMA_HOME = 'https://mp.yizhezhe.com/mama-box/';
// 首页地址配置接口(box_app 免登录面;保留名见 docs/api-naming-convention.md 第四节)
const HOME_CONFIG_API = 'https://mp.yizhezhe.com/api/mp/public/home/config';
// 生效首页地址的本地缓存(离线/弱网时用上次拿到的地址,不回退默认)
const HOME_URL_CACHE_KEY = 'MP_HOME_URL_CACHE';
// 与后端校验同口径:只要 https 且无空白;域名合法性由微信「web-view 业务域名」白名单兜底
function isValidHomeUrl(u) {
return typeof u === 'string' && /^https:\/\//i.test(u) && !/\s/.test(u);
}
/* 剥离分享链接里的身份/缓存参数。分享出去的链接必须干净:
① 不能把分享者的 openid/unionid 泄露给接收者;
@@ -119,6 +130,7 @@
if (this.loadError) return '请检查网络后重试';
if (this.identityFail) return '请检查网络后点下方重试,或退出小程序重新进入';
if (this.identityPending) return '马上就好,请稍候(不会加载空数据的页面)';
if (this.restoring) return '正在回到你上次使用的应用';
return '正在为你打开「兜爸心选」';
},
fallbackEmoji() {
@@ -127,11 +139,15 @@
},
data() {
return {
webUrl: '', // web-view 地址(硬编码 + #openid/#unionid)
webUrl: '', // web-view 地址(首页地址/当前 H5 + #openid/#unionid)
_homeUrl: '', // 生效首页地址(后台配置;非响应式内部字段,见 _homeUrlReady)
_homeUrlTask: null, // 「拉首页地址配置」的单飞 Promise(一次冷启动只请求一次)
requesting: false, // 防重入
_shownOnce: false, // 首次展示标记(避免首次 onShow 重复加载)
_idAttempts: 0, // 「取到 openid 才加载」的自动重试计数(见 fetchIdentityThenLoad)
loadError: false, // web-view 加载失败(显示「重新加载」)
restoring: false, // 冷启动正在判定/打开「上次进入的 H5 应用」(只影响兜底屏文案)
_noRestore: false, // 本次加载 mama-box 是否要带 #norestore=1(禁止 H5 兜底再把用户送回应用)
identityPending: false, // 正在获取 openid(此期间**不加载 H5**,见 fetchIdentityThenLoad)
identityFail: false, // 多次仍未拿到 openid → 停在兜底屏,给「重新获取身份」入口
shareInfo: {}, // CRMEB 后台「分享设置」(标题/图,无 App 匹配时兜底)
@@ -142,6 +158,20 @@
};
},
onLoad(options) {
/* 诊断日志(过滤关键字 enter-app):首页壳每次「冷启动」都会打这一行;
如果重进时**看不到它**,说明是热启动(onShow),恢复走的是「保持不动」那条路。 */
try {
console.log('[enter-app] ===== 首页壳 onLoad(冷启动)===== options=' + JSON.stringify(options || {}));
} catch (e) {}
/* 消费「本次冷启动是否允许恢复」标记(App.onLaunch 置位,见其注释)。
放在最前面就消费:后面任何提前 return(商户/管理员跳工作台、显式落地)都不该把标记留给下次。 */
let restoreArmed = false;
try {
restoreArmed = !!(app && app.globalData && app.globalData.restoreArmed);
if (app && app.globalData) app.globalData.restoreArmed = false;
} catch (e) {}
console.log('[enter-app] restoreArmed=' + restoreArmed +
'(true=冷启动落在首页,可恢复;false=显式落地或从应用内回首页,不恢复)');
// #ifdef MP
// 开启「转发给好友 / 分享到朋友圈」两项菜单(onShareTimeline 需要它)
uni.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline'] });
@@ -152,6 +182,9 @@
this.getTemlIds();
getShare().then(res => { this.shareInfo = (res && res.data) || {}; }).catch(() => {});
// #endif
// 最早发出「首页地址配置」请求(与身份解析并行,通常先于它完成 → 零额外等待)。
// loadMamaBox / retryLoad / onShow 都通过 _homeUrlReady() / _homeBase() 消费它。
this._homeUrlReady();
// 预热 box_app 全量应用表(url → shareImageUrl)。
// 本页不会随 H5 应用切换重新 onLoad(应用宫格用 location.href 换页),
// 所以分享图只能在「点转发那一刻」按 options.webViewUrl 同步匹配,
@@ -174,7 +207,9 @@
if (options && typeof options.url === 'string' && options.url) {
let target = options.url;
try { target = decodeURIComponent(target); } catch (e) {}
this.loadMamaBox(); // 保留首页作为返回落点
console.log('[enter-app] 显式落地(options.url,分享/朋友圈)→ 直接进容器页: ' + target +
'(优先级高于「恢复上次应用」)');
this.loadMamaBox(true); // 保留首页作为返回落点;带 #norestore,避免 H5 兜底把落点换成别的应用
this.$nextTick(() => {
uni.navigateTo({ url: '/pages/annex/web_view/index?url=' + encodeURIComponent(target) });
});
@@ -189,9 +224,44 @@
// ⚠️ 但严禁在商城页主动 showTabBar 来「配对」——上一轮翻车根因
// (showTabBar + DIY 底栏同显 = 商品列表双 tabBar,见 onShow 旧警告)。
uni.hideTabBar();
this.loadMamaBox();
/* 冷启动恢复(2026-09-22,档 3):先问 box_app「上次进入的 H5 应用」,
命中就直接把容器页压上去 —— **不加载 mama-box,因此没有首页闪屏**。
未命中 / 取数失败 / 无身份 → 回落到原路径(加载 mama-box)。
⚠️ 只有 options.url(分享/朋友圈落地)算显式落地并已在上面 return;
options.scene **每次冷启动都有值**(1001 最近使用 / 1007 分享 / 1011 扫码…),
不能拿它当「显式落地」判据,否则永远不恢复。 */
/* 只有「冷启动落在首页/引导页且无显式落地」才尝试恢复(restoreArmed 已在 onLoad 开头消费)。
扫码/分享进容器页、或从应用内点左上角「小房子」回首页时 armed=false → 不恢复,并且加载
mama-box 时带 #norestore=1(否则它的兜底恢复又会把用户送回刚才那个应用,出现
「点小房子回不了首页、要多点一次返回」的循环;2026-09-22 用户实测)。 */
if (!restoreArmed) {
console.log('[enter-app] 本次不是「冷启动落在首页」(显式落地 / 从应用内回首页)→ 不恢复,' +
'直接进首页(mama-box 带 #norestore=1)');
this.loadMamaBox(true);
return;
}
console.log('[enter-app] 非显式落地 → 尝试恢复「上次进入的 H5 应用」…');
this.restoreOrLoadHome();
},
onShow() {
/* 是否「小程序热启动恢复」(点胶囊关闭后再进 / 切后台再回),而不是从其它 tab 切回首页:
App.onShow 刚触发过 → 页面栈与 web-view 都还在,用户仍停在离开时的那个 H5 应用里。
⚠️ 这种情况**必须跳过下面的「重载 webview」**,否则会把用户从 H5 应用踢回兜爸心选首页
—— 2026-09-22 线上问题根因:热启动不触发 onLoad(恢复逻辑没机会跑),
却触发了这里的重载;而 shareHref 仍是 mama-box(H5 postMessage 被微信攒着没派发),
于是重载到首页,mama-box 的兜底又被 sessionStorage 闸门挡住 → 停在首页。
⚠️ 切 tab 不触发 App.onShow,所以那个场景仍按原行为重载回首页。 */
let isAppResume = false;
try {
const at = (app && app.globalData && app.globalData.appShowAt) || 0;
isAppResume = at > 0 && (Date.now() - at) < 2000;
} catch (e) {}
/* 诊断日志(过滤关键字 enter-app):这一行是「热启动还是切 tab」的唯一判据来源。
isAppResume=true → 热启动恢复(点胶囊关闭后再进):保持当前 H5 不动,不重载;
isAppResume=false → 从其它 tab 切回首页:按原行为重载 web-view 回首页。 */
console.log('[enter-app] 首页壳 onShow: isAppResume=' + isAppResume +
'(_shownOnce=' + this._shownOnce + ', webUrl=' + (this.webUrl ? '有' : '空') +
', shareHref=' + (this.shareHref || '空') + ')');
// 每次回到首页都再压一次 tabBar:hideTabBar 是全局状态,可能被别处
// showTabBar 抬回来(如后台停用 DIY 底部导航时商城页会露出原生条兜底)。
// (原这行的 uni.$emit('tabbar-update') 随 custom-tab-bar 死代码一并移除)
@@ -205,29 +275,43 @@
// 目标 H5 永远收不到 phone(2026-09-18 孕检日历授权弹层复现根因之一)。
// shareHref 由 H5 换页/发起授权前的 location 上报实时刷新(askProfile
// 会先 reportLocation 再 navigateTo),是「当前 H5」最可靠的来源。
const authBase = (this.shareHref || this.webUrl || MAMA_HOME).split('#')[0];
const authBase = (this.shareHref || this.webUrl || this._homeBase()).split('#')[0];
const authUrl = this.consumeAuthResult(authBase);
if (authUrl) {
this.loadError = false;
this._shownOnce = true;
this.webUrl = authUrl;
} else if (this._shownOnce && this.webUrl) {
} else if (this._shownOnce && this.webUrl && !isAppResume) {
// ⚠️ 从商城/其它 tab 切回首页时:webview 是同一个实例,SPA 会停留在切走前的屏。
// 改为「重载 webview」让 H5 落回首页(H5 默认即是首页)。首次展示不重复刷。
// 同理不能写死 MAMA_HOME —— 用户停在宫格切换的应用里时会被踢回 mama-box;
// 以 shareHref(当前 H5)为基址重载,停留在原应用首页。
// ⚠️ 热启动恢复(isAppResume)走不到这里:见 onShow 开头的说明。
this.loadError = false;
const reloadBase = (this.shareHref || MAMA_HOME).split('#')[0];
const reloadBase = (this.shareHref || this._homeBase()).split('#')[0];
console.log('[enter-app] onShow: 非热启动(切 tab 回首页)→ 重载 web-view,基址=' + reloadBase);
// 宿主自己把首页重载回首页 H5 时,同步清掉 last_app(人已经回到首页了)。
// 覆盖「容器页返回箭头回首页」这类不经过 H5 后退的路径 —— H5 侧 clearLastAppOnBackHome()
// 只认浏览器后退(back_forward/pageshow.persisted)或 referrer,宿主 src 重载不一定带得上 referrer。
// ⚠️ 2026-10-08 起首页地址可定制:不能再写死判断 '/mama-box',要按生效首页地址比较
// (否则定制首页后回首页不清 last_app,下次冷启动会被送回旧应用)。
if (this._isHomeBase(reloadBase)) this._clearLastApp();
if (this._hostOpenid()) {
this.webUrl = this.buildWebUrl(reloadBase + '?t=' + Date.now());
// ⚠️ 回首页 tab 的重载一律带 #norestore=1:这是「用户要回首页」,
// 不能让 mama-box 的兜底恢复又把他送回上次那个应用(见 onLoad 的入口判定)。
this.webUrl = this.buildWebUrl(reloadBase + '?t=' + Date.now(), ['norestore=1']);
} else {
/* ⚠️ 仍无 openid:走同一条「取到才加载」的路(自动重试 3 次 → 兜底屏),
不再匿名重载(原因见 fetchIdentityThenLoad 的注释)。 */
console.log('[auth-flow] 🔄 onShow: 仍无 openid,走「取到才加载」');
this._idAttempts = 0;
this._noRestore = true; // 同上:回首页 tab 不许 mama-box 兜底恢复
this.fetchIdentityThenLoad(reloadBase);
}
} else {
if (isAppResume) {
console.log('[enter-app] onShow: 热启动恢复 → 保持当前 web-view 不重载(用户仍停在上次的 H5 应用里)');
}
this._shownOnce = true;
}
// 历史(勿照旧回退):曾试过「home hide + 商城页 show」双向 programmatic
@@ -265,7 +349,7 @@
现在本页 onLoad 预热全量应用表(mixins/shareCard.js),转发时用
options.webViewUrl 同步匹配出图;H5 postMessage 的 imageUrl 有值才覆盖。 */
onShareAppMessage(options) {
const target = (options && options.webViewUrl) || this.shareHref || this.webUrl || MAMA_HOME;
const target = (options && options.webViewUrl) || this.shareHref || this.webUrl || this._homeBase();
// 按「当前 H5 地址」解析分享图:先做应用切换检测(首页壳不随 H5 换页重新 onLoad),
// 再每次重新反查 box_app —— 不能因为 shareImageUrl 已有值就跳过,
// 那是上一个应用的图(A 应用分享过、切到 B 后仍带 A 的图,就是这里出的错)。
@@ -273,6 +357,8 @@
const info = this.shareInfo || {};
const uid = this.uid || 0;
return {
// 标题来源优先级:H5 postMessage 上报 → box_app 反查(后台「分享标题」,留空 = 应用名称,
// 由 resolveShareCard 写进 shareTitle)→ CRMEB 后台分享设置 → 内置兜底。
title: this.shareTitle || info.title || '兜爸心选 · 孕育陪伴',
// spid:恢复原装修首页的分享推广归因(接收者容器页 / App.onShow 读入 globalData.spid)
path: '/pages/annex/web_view/index?url=' + encodeURIComponent(stripAuthParams(target)) + '&spid=' + uid,
@@ -280,8 +366,10 @@
};
},
onShareTimeline() {
// onShareTimeline 无回调参数,拿不到 webViewUrl,只能用 H5 传来的 href 兜底
const target = this.shareHref || this.webUrl || MAMA_HOME;
// onShareTimeline 无回调参数,拿不到 webViewUrl,只能用 H5 传来的 href 兜底。
// 同一地址也解析一次:首次分享就走朋友圈时,标题/图不能还是空的。
const target = this.shareHref || this.webUrl || this._homeBase();
this.resolveShareCard(target);
const info = this.shareInfo || {};
const uid = this.uid || 0;
return {
@@ -302,17 +390,22 @@
if (options.spid) app.globalData.spid = options.spid;
},
/**
* 查询 box_app 拿兜爸心选的托管地址。
* box_app 的 App 表是「产品盒子」的应用注册中心 —— 改后台这条记录的
* url 字段即可给 H5 换地址 / 切版本,小程序不用发版。
* 加载首页 H5。
* 2026-10-08 起:首页地址不再写死 —— 先等 _homeUrlReady()(box_app 后台配置,
* 本地缓存兜底,最多 2s),再按「有 openid 直接拼 hash 加载 / 无 openid 走取到才加载」。
*/
loadMamaBox() {
// 2026-09 起:mama-box 不再是 box_app 的「应用」(已从 App 表删除)。
// 它是小程序首页 + H5 入口,URL 在此【硬编码】;
// 更新首页 = 直接改服务端 box_app/mama-box/ 目录,无需发版即可刷新内容。
loadMamaBox(noRestore) {
/* @param {boolean} noRestore - 加载首页时给 URL 加 #norestore=1,禁止首页 H5 自己恢复
(它的兜底恢复会把用户从「用户明确要去的页面」再送回上次那个应用)。两种情况要带:
· 恢复成功后留下的「返回落点」;
· 本次明确判定「不恢复」(显式落地 / 从应用内回首页)。
只有「尝试恢复但取数失败」才不带 —— 那时让首页 H5 兜底重试一次。 */
this._noRestore = !!noRestore;
console.log('[enter-app] 加载首页(返回落点)noRestore=' + this._noRestore);
if (this.requesting) return;
this.requesting = true;
this.loadError = false;
this.restoring = false;
/* ⚠️ 身份兜底(2026-09-20 新增):store.openid / 缓存 OPENID / JWT 三处都读不到时,
**先主动取一次**(wx.login → silence_auth,见 mixin 的 _silenceAuthOpenid),
拿到再拼 hash 加载 —— 别匿名加载。
@@ -321,26 +414,198 @@
容器页那样的 openid 兜底(两条都在 2026-09-20 修,现统一叫 _ensureIdentityThenLoad)。
取不到 openid 仍继续加载(保持可用),H5 侧会显示「未获取到微信身份」提示条。 */
this.identityFail = false;
if (this._hostOpenid()) {
this.identityPending = false;
this._homeUrlReady().then((homeUrl) => {
if (this._hostOpenid()) {
this.identityPending = false;
this._idAttempts = 0;
this.webUrl = this.buildWebUrl(homeUrl, this._noRestore ? ['norestore=1'] : []);
this.requesting = false;
return;
}
/* ⚠️⚠️ 拿不到 openid **就不加载**(2026-09-20 用户定的行为):
宁可让用户在这一屏多等几秒(自动重试 3 次),也不匿名加载 H5 ——
匿名加载会让 H5 进访客态(孕周 `--`、档案查不到),
用户看到「页面能用但全是空」比等一会儿更糟,且事后很难判断是身份问题。 */
this._idAttempts = 0;
this.webUrl = this.buildWebUrl(MAMA_HOME);
this.fetchIdentityThenLoad(homeUrl);
this.requesting = false;
});
},
/* ---------- 首页地址:box_app 后台可定制(2026-10-08) ----------
过去 MAMA_HOME 写死在本文件,换首页要发小程序版。现在冷启动拉
GET /api/mp/public/home/config 拿生效地址(管理后台「首页配置」页可改,
homeUrl 清空 = 回内置默认);homeUrlReady 是单飞 Promise,onLoad 最先发出,
loadMamaBox / retryLoad 消费 —— 与身份解析并行,正常零额外等待。
可靠性分层(每一层都不能阻塞首页加载,AGENTS.md §5.6):
① 配置请求失败/超时(2s)→ 用「本地缓存的上次地址」(离线也能进上次的定制首页);
② 本地也没有 → 内置默认 MAMA_HOME;
③ 响应 code!==200(⚠️ 这个后端不存在路由时 HTTP 仍是 200,错误码在 body,见 §5.7)
或 url 非法 → 同②,但**不覆盖**本地缓存(分不清是接口坏了还是后台清空,
宁可沿用旧值,下次冷启动再试);
④ 只有拿到合法地址(含后台清空时下发的默认地址)才写缓存。
⚠️ 生效时机是「下一次冷启动」:已加载的 web-view 不因配置变化主动重载
(热启动不打扰用户,与 onShow 的 isAppResume 原则一致)。 */
_homeUrlReady() {
if (this._homeUrlTask) return this._homeUrlTask;
// 先用本地缓存占位(非响应式内部字段,只给 JS 逻辑用,与 _shownOnce 同款)
try {
const cached = uni.getStorageSync(HOME_URL_CACHE_KEY);
if (cached && isValidHomeUrl(cached.url)) this._homeUrl = cached.url;
} catch (e) {}
this._homeUrlTask = new Promise((resolve) => {
let done = false;
const finish = (url) => {
if (done) return;
done = true;
if (url) {
this._homeUrl = url;
try { uni.setStorageSync(HOME_URL_CACHE_KEY, { url: url, at: Date.now() }); } catch (e) {}
}
resolve(this._homeUrl || MAMA_HOME);
};
const timer = setTimeout(() => {
console.log('[enter-app] ⏱ 首页地址配置超时(2s)→ 按「本地缓存/默认」处理');
finish(null);
}, 2000);
uni.request({
url: HOME_CONFIG_API,
method: 'GET',
timeout: 2000,
success: (res) => {
clearTimeout(timer);
const body = res && res.data;
const url = body && body.code === 200 && body.result ? body.result.url : null;
if (isValidHomeUrl(url)) {
console.log('[enter-app] 首页地址生效: ' + url +
(url === MAMA_HOME ? '(内置默认)' : '(后台定制)'));
finish(url);
} else {
console.log('[enter-app] 首页地址配置无合法值(body=' + JSON.stringify(body) +
') → 沿用「本地缓存/默认」');
finish(null);
}
},
fail: (err) => {
clearTimeout(timer);
console.log('[enter-app] ✗ 首页地址配置请求失败 → 按「本地缓存/默认」处理: ' + JSON.stringify(err));
finish(null);
}
});
});
return this._homeUrlTask;
},
/* 当前生效的首页基址(同步取:后台配置 → 本地缓存 → 内置默认)。
用于分享兜底 / 授权回传落点 / 兜底屏重试这些「拿不到实时值时」的场景。 */
_homeBase() {
return this._homeUrl || MAMA_HOME;
},
/* base 是否就是首页 H5(去 hash/query、忽略末尾斜杠后比较)。
过去写死判断 indexOf('/mama-box');首页可定制后必须按生效地址比较,
否则定制首页时「宿主重载回首页」不会清 last_app(下次冷启动被送回旧应用)。 */
_isHomeBase(base) {
const norm = (u) => String(u || '').split('#')[0].split('?')[0].replace(/\/+$/, '');
return !!base && norm(base) === norm(this._homeBase());
},
/* ---------- 冷启动恢复:直接把容器页指向「上次进入的 H5 应用」(2026-09-22 档 3) ----------
需求:进入某 H5 应用后退出小程序,再进要恢复它。
做法:本页自己问 box_app `GET /api/mp/public/last-app?openid=`(Redis,per-openid,
7 天 TTL);命中就直接 navigateTo 容器页加载目标 —— **不加载 mama-box,无首页闪屏**。
优先级:显式落地(options.url 分享/朋友圈)> 恢复 > 首页。
回落:无记录 / 取数失败 / 超时 / 拿不到 openid → 照旧加载 mama-box
(mama-box 自己的 resumeLastApp 仍是「本页取数失败」时的兜底)。
⚠️ 身份注入交给容器页(它用 query 注入)。本页的 buildWebUrl 是 **hash** 注入,
对自带 hash 路由的应用(如 baobao-yingji)会破坏路由,所以恢复路径不自己拼身份。 */
restoreOrLoadHome() {
const decide = () => {
const openid = this._hostOpenid();
if (!openid) {
console.log('[enter-app] ✗ 身份仍未就绪(无 openid)→ 不查 last-app,交给原路径加载首页');
this.loadMamaBox();
return;
}
console.log('[enter-app] 查 last-app: openid=' + openid);
this._fetchLastApp(openid).then(rec => {
if (rec && rec.url) {
this._openRestoredApp(rec.url);
} else {
console.log('[enter-app] ○ last-app 无记录/未拿到 → 进兜爸心选首页');
this.loadMamaBox();
}
});
};
this.restoring = true;
if (this._hostOpenid()) {
console.log('[enter-app] 恢复判定开始:openid 命中缓存,直接查 last-app');
decide();
return;
}
/* ⚠️⚠️ 拿不到 openid **就不加载**(2026-09-20 用户定的行为):
宁可让用户在这一屏多等几秒(自动重试 3 次),也不匿名加载 H5 ——
匿名加载会让 H5 进访客态(孕周 `--`、档案查不到),
用户看到「页面能用但全是空」比等一会儿更糟,且事后很难判断是身份问题。 */
this._idAttempts = 0;
this.fetchIdentityThenLoad(MAMA_HOME);
this.requesting = false;
/* 无缓存身份:先等 App.vue onLaunch 那次静默登录(与 fetchIdentityThenLoad 同款——
同一次 code exchange,不重复换 code),最多 1.5s;仍拿不到就交回原路径。 */
console.log('[enter-app] 恢复判定开始:无缓存 openid,先等 App.vue 静默登录(最多 1.5s)…');
this._awaitHostLogin(1500).then(decide);
},
/* 读一次 last_app;任何失败/超时都 resolve(null),绝不阻塞首页 */
_fetchLastApp(openid) {
return new Promise(resolve => {
let done = false;
const finish = (v) => { if (!done) { done = true; resolve(v); } };
const api = 'https://mp.yizhezhe.com/api/mp/public/last-app?openid=' + encodeURIComponent(openid);
const timer = setTimeout(() => {
console.log('[enter-app] ⏱ last-app 请求超时(1.5s)→ 按无记录处理');
finish(null);
}, 1500);
uni.request({
url: api,
method: 'GET',
timeout: 1500,
success: (res) => {
clearTimeout(timer);
const body = res && res.data;
const rec = body && body.result;
console.log('[enter-app] last-app 响应: HTTP ' + ((res && res.statusCode) || '?') +
' body=' + JSON.stringify(body));
finish(rec && rec.url ? rec : null);
},
fail: (err) => {
clearTimeout(timer);
console.log('[enter-app] ✗ last-app 请求失败 → 按无记录处理: ' + JSON.stringify(err));
finish(null);
}
});
});
},
/* 宿主侧兜底清除:小程序自己把首页重载回 mama-box 时同步清 last_app(见 onShow 调用处)。
与 H5 侧 clearLastAppOnBackHome() 互补:那条认浏览器后退/referrer,这条是宿主确定要回首页。 */
_clearLastApp() {
try {
const openid = this._hostOpenid();
if (!openid) return;
console.log('[enter-app] 宿主重载回首页 → 清除 last_app(下次冷启动不再回上次那个应用)');
uni.request({
url: 'https://mp.yizhezhe.com/api/mp/public/last-app?openid=' + encodeURIComponent(openid),
method: 'DELETE',
timeout: 5000,
fail: (err) => console.log('[enter-app] ✗ 清除 last_app 失败(忽略): ' + JSON.stringify(err))
});
} catch (e) {}
},
/* 命中恢复目标:保留首页作为返回落点 + 交容器页加载(与 options.url 分享落地同款)
⚠️ 落点必须带 #norestore=1:否则 mama-box 会在容器页底下**自己再恢复一次**,
把返回落点也换成目标应用,back 就回不到首页了。 */
_openRestoredApp(url) {
console.log('[enter-app] ✅ 命中 last-app → 直接进容器页加载: ' + url +
'(不经过 mama-box,无首页闪屏)');
this.restoring = false;
this.loadMamaBox(true);
this.$nextTick(() => {
uni.navigateTo({ url: '/pages/annex/web_view/index?url=' + encodeURIComponent(url) });
});
},
/**
* 取到 openid 才加载 H5;自动重试 3 次仍失败 → 停在兜底屏给「重新获取身份」入口。
* ⚠️ 每次尝试前都再读一次 _hostOpenid():App.vue 的 onLaunch 静默授权是并行的,
* 它先落地的话这里直接命中缓存,不必自己再请求一次。
* @param {string} base - 要加载的 H5 基址(首页壳是 MAMA_HOME;onShow 重载时是当前 H5)
* @param {string} base - 要加载的 H5 基址(首页壳是生效首页地址;onShow 重载时是当前 H5)
*/
fetchIdentityThenLoad(base) {
/* 1) 缓存命中(含 App.vue 并行登录刚落地的)→ 0 等待加载 */
@@ -376,7 +641,9 @@
this.identityFail = false;
this._idAttempts = 0;
this._shownOnce = true;
this.webUrl = this.buildWebUrl(base + (base.indexOf('?') >= 0 ? '&' : '?') + 't=' + Date.now());
// 沿用本次的 _noRestore(见 loadMamaBox 注释):本轮加载是否需要禁止 mama-box 兜底恢复
this.webUrl = this.buildWebUrl(base + (base.indexOf('?') >= 0 ? '&' : '?') + 't=' + Date.now(),
this._noRestore ? ['norestore=1'] : []);
console.log('[auth-flow] ✅ 首页壳拿到 openid,开始加载 H5');
return true;
},
@@ -384,26 +651,29 @@
* 给 H5 拼 hash 参数(openid 单一身份键 + unionid 可选)。
* ⚠️ 只能拼 # 不能拼 ?:微信 web-view 首次加载丢 query(app_03 实测)。
* 身份解析统一由 mixins/webviewHost.js 的 _hostOpenid / _hostUnionid 提供。
* @param {string} url
* @param {string[]} [extraHash] 额外 hash 参数(如 ['norestore=1'];仅本页自用)
*/
buildWebUrl(url) {
buildWebUrl(url, extraHash) {
const parts = [];
const openid = this._hostOpenid();
const unionid = this._hostUnionid();
if (openid) parts.push('openid=' + openid);
if (unionid) parts.push('unionid=' + unionid);
if (!parts.length) return url;
return url + (url.indexOf('#') >= 0 ? '&' : '#') + parts.join('&');
const all = parts.concat(extraHash || []);
if (!all.length) return url;
return url + (url.indexOf('#') >= 0 ? '&' : '#') + all.join('&');
},
/* webviewHost hook:本页 web-view 地址字段是 webUrl */
_hostSetWebviewUrl(url) { this.webUrl = url; },
/* webviewHost hook:copyLink 未带 href 时的兜底地址 */
_hostCurrentUrl() { return this.webUrl || MAMA_HOME; },
_hostCurrentUrl() { return this.webUrl || this._homeBase(); },
/* 授权面板提交(base = 当前 H5 地址,同 onShow 的回传落点逻辑 ——
宫格换应用后 MAMA_HOME 不再是用户所在的 H5)→ mixin 的共享授权接口 + hash 回传 */
宫格换应用后首页地址不再是用户所在的 H5)→ mixin 的共享授权接口 + hash 回传 */
submitAuth() {
this.submitHostAuth((this.shareHref || this.webUrl || MAMA_HOME).split('#')[0]);
this.submitHostAuth((this.shareHref || this.webUrl || this._homeBase()).split('#')[0]);
},
/* MP:订阅消息模板预取(原装修首页行为;utils/SubscribeMessage.js 请求时用缓存) */
@@ -427,19 +697,22 @@
this.requesting = false;
this._shownOnce = true;
this.identityFail = false;
/* 有身份直接重载;没有就再走一遍「取到才加载」(重置尝试次数,自动重试 3 次) */
/* 有身份直接重载;没有就再走一遍「取到才加载」(重置尝试次数,自动重试 3 次)
⚠️ 沿用 this._noRestore:重试首页时同样不许首页 H5 兜底恢复(见 loadMamaBox)。 */
const homeUrl = this._homeBase();
if (this._hostOpenid()) {
this.webUrl = this.buildWebUrl(MAMA_HOME + '?t=' + Date.now());
this.webUrl = this.buildWebUrl(homeUrl + '?t=' + Date.now(),
this._noRestore ? ['norestore=1'] : []);
return;
}
this._idAttempts = 0;
this.fetchIdentityThenLoad(MAMA_HOME);
this.fetchIdentityThenLoad(homeUrl);
},
/* 授权完善资料(postMessage → 原生面板 → 共享授权接口 → hash 回传)与
pages/annex/web_view 共用 mixins/webviewHost.js:
handleHostMessage / onChooseHostAvatar / onHostGetPhone /
submitHostAuth(MAMA_HOME) / consumeAuthResult(见 onShow)
submitHostAuth(生效首页地址) / consumeAuthResult(见 onShow)
本页不再各写一份;分享图设置由 mixins/shareCard.js 提供。 */
}
}