From bf43084afe3fd8799a52931874d6ec3cef362d2b Mon Sep 17 00:00:00 2001 From: Karlcarlo Date: Fri, 9 Oct 2026 22:12:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(goHome):=20=E5=9B=9E=E3=80=8C=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E9=85=8D=E7=BD=AE=E7=9A=84=E9=A6=96=E9=A1=B5=E3=80=8D?= =?UTF-8?q?=E2=80=94=E2=80=94=20=E5=85=88=E6=8B=89=20/api/mp/public/home/c?= =?UTF-8?q?onfig=20=E5=86=8D=E8=B7=B3=EF=BC=881.5s=20=E8=B6=85=E6=97=B6/?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=9D=8F=E5=9B=9E=E9=80=80=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=EF=BC=8C=E8=BA=AB=E4=BB=BD=20query=20=E7=BB=AD=E6=8E=A5?= =?UTF-8?q?=EF=BC=8C=E5=85=BC=E5=AE=B9=E8=87=AA=E5=B8=A6=20query=20?= =?UTF-8?q?=E7=9A=84=E5=AE=9A=E5=88=B6=E9=A6=96=E9=A1=B5=EF=BC=89(v2610090?= =?UTF-8?q?1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 2 +- js/app.js | 2 +- js/wx-auth.js | 43 ++++++++++++++++++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 8fc0f40..ac66fab 100644 --- a/index.html +++ b/index.html @@ -7,7 +7,7 @@ 兜爸心选 · 孕育陪伴 - + diff --git a/js/app.js b/js/app.js index 5c7e26f..7392bee 100644 --- a/js/app.js +++ b/js/app.js @@ -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)); diff --git a/js/wx-auth.js b/js/wx-auth.js index 37cc82f..3b17357 100644 --- a/js/wx-auth.js +++ b/js/wx-auth.js @@ -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) {