feat(goHome): 回「后台配置的首页」—— 先拉 /api/mp/public/home/config 再跳(1.5s 超时/接口坏回退默认,身份 query 续接,兼容自带 query 的定制首页)(v26100901)

This commit is contained in:
2026-10-09 22:12:31 +08:00
parent d0fcc1e988
commit bf43084afe
3 changed files with 40 additions and 7 deletions

View File

@@ -3,7 +3,7 @@
(function () {
'use strict';
/* 页面版本号(YYMMDD+序号):显示在「关于兜爸心选」页底部;bump 时同步 head 注释与 ?v= */
var VER = '26092219';
var VER = '26100901';
const $ = (s, c) => (c || document).querySelector(s);
const $$ = (s, c) => Array.from((c || document).querySelectorAll(s));

View File

@@ -9,7 +9,7 @@
wxAuth.isAuthorized(); // 是否已授权(手机号/昵称/头像任一非空)
wxAuth.requestProfile(); // 点「授权登录」→ postMessage 通知宿主页
wxAuth.goMall(); // 点「进商城」→ 跳小程序商城 tabBar 页
wxAuth.goHome(); // 点「返回首页」→ 回 mama-box(带身份;别裸写 location.href)
wxAuth.goHome(); // 点「返回首页」→ 回「后台配置的首页」(带身份;别裸写 location.href)
wxAuth.identity(); // 读 openid/unionid(URL 优先 → 同源缓存兜底)
wxAuth.watchLocation(); // 换页/路由切换自动上报当前位置 → 宿主刷新分享卡片(推荐)
wxAuth.reportLocation({title}); // 手动上报一次当前页(标题/图可选)
@@ -219,17 +219,50 @@
场景:授权回传后身份只在 hash 里出现一次,若随后要清 URL(防分享泄露),
可先调它存一份。 */
rememberIdentity: rememberIdentity,
/* ⑦ 回 mama-box 首页(H5 方式)。带身份:URL 优先,其次缓存。
/* ⑦ 回「后台配置的首页」(H5 方式)。带身份:URL 优先,其次缓存。
⚠️ 「返回首页」这类跳转**不要**裸写 location.href='/mama-box/' ——
宿主不会因此重新注入身份(web-view src 没变),目标页会显示「身份不完整」,
而重试也救不回来。要么调本方法,要么保证链接带上 openid/unionid。
(第三方应用改不了的话,靠 identity() 的缓存兜底也能让首页认出人来。) */
(第三方应用改不了的话,靠 identity() 的缓存兜底也能让首页认出人来。)
2026-10-09 起首页地址在 box_app 后台可定制:本方法先拉一次生效地址
(GET /api/mp/public/home/config,同源无 CORS)再跳;超时/接口坏/值非法
→ 回退内置默认(旧行为),「回首页」绝不被网络卡死(§5.6 部署顺序与降级)。
自定义首页 H5 落地后的「清 last_app」义务见接入规范第七节(mama-box 自带)。 */
goHome: function () {
var id = identity();
var q = id.openid
? '?openid=' + encodeURIComponent(id.openid) + (id.unionid ? '&unionid=' + encodeURIComponent(id.unionid) : '')
? 'openid=' + encodeURIComponent(id.openid) + (id.unionid ? '&unionid=' + encodeURIComponent(id.unionid) : '')
: '';
location.href = 'https://mp.yizhezhe.com/mama-box/' + q;
var DEFAULT_HOME = 'https://mp.yizhezhe.com/mama-box/';
/* 首页地址可能自带 query(后台配置的),身份用 & 续在后面,别拼出两个 ? */
var jump = function (home) {
location.href = home + (q ? (home.indexOf('?') >= 0 ? '&' : '?') + q : '');
};
var okUrl = function (u) {
return typeof u === 'string' && /^https:\/\//i.test(u) && !/\s/.test(u);
};
try {
var done = false;
var finish = function (u) {
if (done) return;
done = true;
jump(okUrl(u) ? u : DEFAULT_HOME);
};
var timer = setTimeout(function () { finish(''); }, 1500);
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://mp.yizhezhe.com/api/mp/public/home/config', true);
try { xhr.timeout = 1500; } catch (e) {}
xhr.onload = function () {
clearTimeout(timer);
try {
var b = JSON.parse(xhr.responseText || '{}');
/* ⚠️ 这个后端不存在路由时 HTTP 仍是 200,错误码在 body.code —— 必须查 body */
finish(b && b.code === 200 && b.result ? b.result.url : '');
} catch (e) { finish(''); }
};
xhr.onerror = xhr.onabort = xhr.ontimeout = function () { clearTimeout(timer); finish(''); };
xhr.send();
} catch (e) { jump(DEFAULT_HOME); }
},
/* ⑧ 展示授权结果到指定元素(可选便捷方法) */
fillAuth: function (nameEl, avatarEl) {