悄悄话留言身份与首页打通:openid 读 hash+query+同源缓存,退出登录算访客;广场/专题页也加缓存兜底 (v26110633)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
deploy
2026-11-07 05:01:16 +00:00
parent 53652c481c
commit 91f29e05c9
5 changed files with 22 additions and 14 deletions

View File

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

View File

@@ -18,7 +18,15 @@
if (!page || !bar || !window.MB_WH_THEME) return;
var TH = MB_WH_THEME(), PID = 'wh-' + TH.from.replace(/-/g, '');
var esc = function (s) { return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) { return { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]; }); };
var m = ((location.hash || '') + '&').match(/[#&]openid=([^&]*)/), OID = m ? decodeURIComponent(m[1]) : '';
/* 11-07 修:原来只读 hash 里的 openid —— 小程序里身份常在 query 或同源缓存里(应用间跳回、转发卡片落地),
首页显示已登录、这里却提示「登录后就能留言」。改成和首页 hashParam 同一套:hash + query → wxAuth 同源缓存,退出登录算访客;每次用时现读。 */
function oid() {
if (window.mbGate && window.mbGate.out) return '';
var h = (location.hash.slice(1) || '') + '&' + (location.search.slice(1) || ''), m = h.match(/(?:^|[&#])openid=([^&#]*)/);
var v = m ? decodeURIComponent(m[1]).split('?')[0] : '';
if (!v) try { var id = window.wxAuth && wxAuth.identity ? wxAuth.identity() : null; v = id && id.openid || ''; } catch (e) {}
return v;
}
var HEART = '<svg viewBox="0 0 24 24"><path d="M12 20.5s-7.5-4.6-7.5-10.2A4.3 4.3 0 0112 7.6a4.3 4.3 0 017.5 2.7c0 5.6-7.5 10.2-7.5 10.2z"/></svg>';
var WC = (location.search.match(/[?&]wc=([A-Za-z0-9]{1,40})/) || [])[1] || ''; /* 别人转发来请我点赞的那条 */
@@ -61,7 +69,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=26110632').then(function (r) { return r.json(); }).then(function (j) {
fetch('guangchang/cm.json?v=26110633').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]];
@@ -73,7 +81,7 @@
fetchReal();
}
function fetchReal() {
return fetch('/api/mp/public/mama-box/plaza/comments?kind=post&target=' + encodeURIComponent(PID) + (OID ? '&openid=' + encodeURIComponent(OID) : ''))
return fetch('/api/mp/public/mama-box/plaza/comments?kind=post&target=' + encodeURIComponent(PID) + (oid() ? '&openid=' + encodeURIComponent(oid()) : ''))
.then(function (r) { return r.json(); }).then(function (j) {
var R = j && j.code === 200 && j.result || {};
if (R.starLikes) STAR_N = R.starLikes;
@@ -247,11 +255,11 @@
return;
}
/* 真实留言:服务端计数,赞了就不能取消(同星星的抱抱) */
if (!OID) { toast('登录后就能点赞啦'); return; }
if (!oid()) { toast('登录后就能点赞啦'); return; }
if (c.liked) { toast('你已经赞过啦'); return; }
c.liked = true; c.likes++; paintLk(b, c); b.classList.add('pop'); setTimeout(function () { b.classList.remove('pop'); }, 500);
if (navigator.vibrate) navigator.vibrate(8);
fetch('/api/mp/public/mama-box/plaza/comment/like?openid=' + encodeURIComponent(OID) + '&id=' + encodeURIComponent(c.id), { method: 'POST' })
fetch('/api/mp/public/mama-box/plaza/comment/like?openid=' + encodeURIComponent(oid()) + '&id=' + encodeURIComponent(c.id), { method: 'POST' })
.then(function (x) { return x.json(); }).then(function (j) {
if (!j || j.code !== 200) throw j;
c.likes = j.result.likes; var was = c.star; c.star = !!j.result.star; paintLk(b, c);
@@ -266,10 +274,10 @@
});
function post(text, week, reply) {
if (!OID) return Promise.resolve('登录后就能留言啦');
if (!oid()) return Promise.resolve('登录后就能留言啦');
var body = { kind: 'post', target: PID, title: '孕妈悄悄话 · ' + TH.name, text: text, stage: week ? '孕 ' + week + ' 周' : '' };
if (reply) { body.parent = reply.key; body.parentName = reply.n; }
return fetch('/api/mp/public/mama-box/plaza/comment?openid=' + encodeURIComponent(OID), {
return fetch('/api/mp/public/mama-box/plaza/comment?openid=' + encodeURIComponent(oid()), {
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body)
}).then(function (r) { return r.json(); }).then(function (j) {
if (!j || j.code !== 200 || !j.result) return (j && j.message) || '没发出去,再试一次';