Files
mama-box/js/kit.js

91 lines
6.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 11-03 用户:「把此刻那个板块改成测一测、科普、还有计算器,你看一下怎么设计替代此刻的模块」(先在预览改)
→ 位置不变(「我的孕期工具」卡底部,原 #pulse 处),版式:左边一张大的「测一测」+ 右边上下两条「科普」「计算器」,内容按孕周换:
· 测一测:广场测一测 30 个里挑孕期能测的,按孕周段分池,每天轮换一个(北京日期);
· 科普:问题库 hot.json 按孕周段(同「其他孕妈也在搜」qaPick),跳过下面列表已经露出的前 6 条和测试 / 计算器题;
· 计算器:生娃计算器(topic/huafei)、产假天数计算器(topic/zhichang),孕 29 周起产假排前面。
右上「换一换」三格一起换下一组。人数同首页口径:测一测 = 测一测页的 gcTested;科普 / 计算器 = 「此刻」总人数 × heat(模拟数,AGENTS.md 12b)。
原「此刻」#pulse 只是藏起来(kit.css),JS 照跑(别的地方要用它的总人数);地址带 ?kit=0 可看回旧版对比。 */
(function () {
if (/[?&]kit=0(?:&|$)/.test(location.search)) return;
var box = document.getElementById('kit');
if (!box) return;
var V = '26110258';
var l = document.createElement('link'); l.rel = 'stylesheet'; l.href = 'js/kit.css?v=' + V; document.head.appendChild(l);
document.documentElement.classList.add('has-kit');
function esc(t) { return String(t == null ? '' : t).replace(/[&<>"]/g, function (c) { return { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]; }); }
function href(h) { return h + location.search + location.hash; }
function testURL(id) { var q = new URLSearchParams(location.search); q.set('id', id); return 'guangchang/ceshi/?' + q.toString() + location.hash; }
function wan(n) { return n >= 1e4 ? (Math.round(n / 1e3) / 10) + '万' : String(Math.round(n)); }
function total() { var p = window.__mbPulse; return (p && p.total && +p.total.value) || 1510000; }
var day = Math.floor((Date.now() + 288e5) / 864e5), shift = 0;
/* 测一测:按孕周段分池(段同 qaBand:1 ≤8 / 2 ≤13 / 3 ≤20 / 4 ≤28 / 5 ≤36 / 6 37+;0 = 没孕周 / 产后) */
var QZ_POOL = {
0: ['t11', 't14', 't13', 't16', 't18', 't23', 't03', 't22'],
1: ['t17', 't01', 't25', 't24', 't02', 't11'],
2: ['t17', 't01', 't25', 't24', 't02', 't11'],
3: ['t24', 't25', 't01', 't17', 't21', 't02'],
4: ['t24', 't21', 't25', 't01', 't02', 't17'],
5: ['t05', 't21', 't02', 't24', 't03', 't25'],
6: ['t05', 't03', 't02', 't06', 't21', 't25']
};
var CALC = [
{ t: '生娃要花多少钱?一键算清', s: '产检 · 分娩 · 月子 · 第一年', h: 'topic/huafei/', heat: .118 },
{ t: '产假能休多少天?算一算', s: '按地区和生产情况算', h: 'topic/zhichang/', heat: .062 }
];
var QA = null;
fetch('assets/data/qa/hot.json?v=' + V).then(function (r) { return r.ok ? r.json() : []; }).catch(function () { return []; })
.then(function (a) { QA = Array.isArray(a) ? a : []; render(); });
if (!window.GC_TESTS_META) {
var s = document.createElement('script'); s.src = 'guangchang/ceshi/meta.js?v=' + V; s.onload = s.onerror = render; document.body.appendChild(s);
}
function band() {
var p = window.__mbPreg || {};
if (p.postpartum || !p.week) return 0;
return window.__mbQaBand ? window.__mbQaBand(p.week) : 0;
}
function pickQz(b) {
var meta = window.GC_TESTS_META || [], pool = (QZ_POOL[b] || QZ_POOL[0]).map(function (id) { return meta.filter(function (x) { return x.id === id; })[0]; }).filter(Boolean);
return pool.length ? pool[(day + shift) % pool.length] : null;
}
function pickKp(b) {
if (!QA || !QA.length) return null;
var w = (window.__mbPreg || {}).week, skip = ['topic/huafei/', 'topic/ceshi/', 'topic/duiyou/', 'topic/zhichang/'];
var shown = window.__mbQaPick ? window.__mbQaPick(QA, b ? w : 0, 6) : [];
var all = window.__mbQaPick ? window.__mbQaPick(QA, b ? w : 0, 30) : QA;
var l = all.filter(function (x) { return x.href && skip.indexOf(x.href) < 0 && shown.indexOf(x) < 0; });
return l.length ? l[shift % l.length] : null;
}
function render() {
var b = band(), qz = pickQz(b), kp = pickKp(b);
var cl = b >= 5 ? [CALC[1], CALC[0]] : CALC, ca = cl[shift % cl.length];
var p = window.__mbPreg || {}, wk = p.week && !p.postpartum ? '孕 ' + p.week + ' 周' : '';
var h = '<div class="kit-hd"><b>' + (wk ? '<em>' + wk + '</em> · ' : '') + '今天看点什么</b>' +
'<button type="button" class="kit-sw" aria-label="换一组"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 12a8 8 0 0113.7-5.6L20 8.5M20 4v4.5h-4.5M20 12a8 8 0 01-13.7 5.6L4 15.5M4 20v-4.5h4.5"/></svg>换一换</button></div>';
h += '<div class="kit-g">';
if (qz) {
var n = window.gcTestedText ? window.gcTestedText(qz.id) : '';
h += '<a class="kit-qz" href="' + esc(testURL(qz.id)) + '"><span class="kit-k">测一测</span><b class="kit-qt">' + esc(qz.title) + '</b>' +
'<small>' + esc(n || (qz.n + ' 道题')) + '</small><span class="kit-go">去测一测</span>' +
'<img src="guangchang/ceshi/img/' + qz.id + '_s.webp" alt="" loading="lazy" decoding="async"></a>';
}
h += '<div class="kit-col">';
if (kp) h += '<a class="kit-r kp" href="' + esc(href(kp.href)) + '"><img src="assets/img/kit/kepu.webp" alt="" loading="lazy"><span class="kit-rt"><span class="kit-k">科普</span><b>' + esc(kp.q) + '</b>' +
'<small>' + wan(total() * (+kp.heat || .03)) + '人看过</small></span></a>';
h += '<a class="kit-r js" href="' + esc(href(ca.h)) + '"><img src="assets/img/kit/jisuan.webp" alt="" loading="lazy"><span class="kit-rt"><span class="kit-k">计算器</span><b>' + esc(ca.t) + '</b>' +
'<small>' + wan(total() * ca.heat) + '人算过</small></span></a>';
h += '</div></div>';
box.innerHTML = h;
box.classList.remove('hide');
}
box.addEventListener('click', function (e) {
if (!e.target.closest('.kit-sw')) return;
shift++; render();
var g = box.querySelector('.kit-g'); if (g) { g.classList.remove('flip'); void g.offsetWidth; g.classList.add('flip'); }
});
window.addEventListener('mb-preg', render);
render();
})();