HEL-574: add segmented interest accumulation mode to interest calculator
Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -898,15 +898,17 @@ img { display: block; }
|
||||
font-size: 12.5px; font-weight: 600; color: var(--text-2); line-height: 1.4;
|
||||
}
|
||||
.v-fusion .cr > .cr-ctl { flex: 1; min-width: 0; max-width: 560px; }
|
||||
.v-fusion .cr-ctl input[type="text"], .v-fusion .cr-ctl input[type="number"], .v-fusion .cr-ctl input[type="date"], .v-fusion .cr-ctl input[data-jn-date], .v-fusion .cr-ctl select {
|
||||
.v-fusion .cr-ctl input[type="text"], .v-fusion .cr-ctl input[type="number"], .v-fusion .cr-ctl input[type="date"], .v-fusion .cr-ctl input[data-jn-date], .v-fusion .cr-ctl select,
|
||||
.v-fusion .ci-seg-f input[type="text"], .v-fusion .ci-seg-f input[type="number"], .v-fusion .ci-seg-f input[data-jn-date], .v-fusion .ci-seg-f select {
|
||||
max-width: 100%;
|
||||
height: 34px; padding: 0 11px; border-radius: 9px;
|
||||
border: 1px solid var(--hairline); background-color: var(--surface); color: var(--text);
|
||||
outline: none; font-size: 13.5px;
|
||||
transition: border-color 150ms ease, box-shadow 150ms ease;
|
||||
}
|
||||
.v-fusion .cr-ctl select { padding-right: 28px; }
|
||||
.v-fusion .cr-ctl input[type="text"]:focus, .v-fusion .cr-ctl input[type="number"]:focus, .v-fusion .cr-ctl input[type="date"]:focus, .v-fusion .cr-ctl input[data-jn-date]:focus, .v-fusion .cr-ctl select:focus {
|
||||
.v-fusion .cr-ctl select, .v-fusion .ci-seg-f select { padding-right: 28px; }
|
||||
.v-fusion .cr-ctl input[type="text"]:focus, .v-fusion .cr-ctl input[type="number"]:focus, .v-fusion .cr-ctl input[type="date"]:focus, .v-fusion .cr-ctl input[data-jn-date]:focus, .v-fusion .cr-ctl select:focus,
|
||||
.v-fusion .ci-seg-f input[type="text"]:focus, .v-fusion .ci-seg-f input[type="number"]:focus, .v-fusion .ci-seg-f input[data-jn-date]:focus, .v-fusion .ci-seg-f select:focus {
|
||||
border-color: var(--focus-ring); box-shadow: 0 0 0 3px var(--focus-glow);
|
||||
}
|
||||
.v-fusion .cr-inline { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-height: 34px; }
|
||||
@@ -1371,3 +1373,49 @@ img { display: block; }
|
||||
.v-fusion .dir-acts a { font-size: 11.5px; color: var(--text-3); padding: 2px 6px; border-radius: 5px; transition: background-color 120ms ease, color 120ms ease; }
|
||||
@media (hover: hover) and (pointer: fine) { .v-fusion .dir-acts a:hover { background: var(--hover); color: var(--accent); } }
|
||||
.v-fusion .dir-row .nav-badge { color: inherit; flex: 0 0 auto; }
|
||||
|
||||
/* ============================================================
|
||||
HEL-574 · 利息计算器分段累加(复用既有 token,无新颜色/材质)
|
||||
============================================================ */
|
||||
/* 模式切换:沿用 .seg 按钮形态,仅做表单内间距 */
|
||||
.v-fusion .ci-mode { margin: 2px 0 14px; }
|
||||
/* 分段行容器:CSS 计数器维护段号,增删行无需 JS 重排 */
|
||||
.v-fusion .ci-seg-rows { display: flex; flex-direction: column; gap: 10px; counter-reset: ciseg; }
|
||||
.v-fusion .ci-seg-row {
|
||||
display: grid; align-items: end;
|
||||
grid-template-columns: 30px minmax(116px, 1fr) minmax(168px, 1.2fr) 112px minmax(238px, 1.5fr) minmax(84px, auto) 28px;
|
||||
gap: 10px; padding: 11px 12px;
|
||||
border: 1px solid var(--hairline); border-radius: 10px; background: var(--fill);
|
||||
}
|
||||
.v-fusion .ci-seg-no {
|
||||
counter-increment: ciseg; align-self: center;
|
||||
width: 26px; height: 22px; border-radius: 999px;
|
||||
display: inline-flex; align-items: center; justify-content: center;
|
||||
background: var(--surface); border: 1px solid var(--hairline);
|
||||
font-size: 11.5px; font-weight: 700; color: var(--gold);
|
||||
}
|
||||
.v-fusion .ci-seg-no::after { content: counter(ciseg); }
|
||||
.v-fusion .ci-seg-f { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
|
||||
.v-fusion .ci-seg-f[hidden] { display: none; }
|
||||
.v-fusion .ci-seg-f > label { font-size: 11px; font-weight: 600; letter-spacing: 0.04em; color: var(--text-3); }
|
||||
.v-fusion .ci-seg-pair { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
|
||||
.v-fusion .ci-seg-sep { font-size: 11.5px; color: var(--text-3); flex: 0 0 auto; }
|
||||
.v-fusion .ci-seg-out {
|
||||
align-self: center; font-size: 11.5px; font-weight: 600; color: var(--text-2);
|
||||
line-height: 1.4; white-space: nowrap;
|
||||
}
|
||||
.v-fusion .ci-seg-out[data-bad] { color: var(--red); font-weight: 650; white-space: normal; }
|
||||
.v-fusion .ci-seg-del { flex: 0 0 auto; align-self: start; margin-top: 18px; }
|
||||
/* 结果区分段明细:与 calc-formula 同族 */
|
||||
.v-fusion .ci-seg-detail {
|
||||
margin-top: 10px; padding: 10px 12px; border-radius: 10px; background: var(--fill);
|
||||
font-size: 12px; color: var(--text-2); line-height: 1.8;
|
||||
}
|
||||
/* ≤767:分段行折成两列小卡,删除钮固定行右上 */
|
||||
@media (max-width: 767px) {
|
||||
.v-fusion .ci-seg-row { grid-template-columns: repeat(2, minmax(0, 1fr)); position: relative; padding: 12px 44px 12px 12px; row-gap: 10px; }
|
||||
.v-fusion .ci-seg-no { position: absolute; top: 11px; right: 44px; }
|
||||
.v-fusion .ci-seg-del { position: absolute; top: 10px; right: 10px; margin-top: 0; }
|
||||
.v-fusion .ci-seg-f--amt, .v-fusion .ci-seg-f--days { grid-column: span 2; }
|
||||
.v-fusion .ci-seg-out { grid-column: span 2; }
|
||||
}
|
||||
|
||||
+151
-1
@@ -318,6 +318,148 @@ function renderInterest() {
|
||||
<button type="button" class="btn btn--plain" style="width:100%; margin-top:12px;" data-calc-act="copy">复制结果</button>`;
|
||||
}
|
||||
|
||||
/* — 利息计算器 · 分段累加(HEL-574):多笔支用 / 逾期分段变利率变本金 — */
|
||||
function ciTopMode(r) {
|
||||
const b = $("[data-ci-mode][data-active]", r || root());
|
||||
return b ? b.dataset.ciMode : "single";
|
||||
}
|
||||
function setCIMode(mode) {
|
||||
const r = root(); if (!r) return;
|
||||
const next = mode === "multi" ? "multi" : "single";
|
||||
$$("[data-ci-mode]", r).forEach((b) => {
|
||||
if (b.dataset.ciMode === next) b.setAttribute("data-active", "");
|
||||
else b.removeAttribute("data-active");
|
||||
});
|
||||
const pS = $('[data-ci-panel="single"]', r), pM = $('[data-ci-panel="multi"]', r);
|
||||
if (pS) pS.hidden = next !== "single";
|
||||
if (pM) pM.hidden = next !== "multi";
|
||||
if (next === "multi") ensureCiSegRows();
|
||||
initCalc();
|
||||
}
|
||||
const CI_SEG_MAX = 20;
|
||||
function ciSegAddRow(focus) {
|
||||
const r = root(); const host = r && $("[data-ci-seg-rows]", r); const tpl = r && $("[data-ci-seg-tpl]", r);
|
||||
if (!host || !tpl) return null;
|
||||
if (host.children.length >= CI_SEG_MAX) { toast("最多支持 " + CI_SEG_MAX + " 段"); return null; }
|
||||
const row = tpl.content.firstElementChild.cloneNode(true);
|
||||
host.appendChild(row);
|
||||
if (focus !== false) { const inp = $('[data-seg-f="amt"]', row); if (inp) inp.focus(); }
|
||||
return row;
|
||||
}
|
||||
function ensureCiSegRows() {
|
||||
const r = root(); const host = r && $("[data-ci-seg-rows]", r);
|
||||
if (host && !host.children.length) { ciSegAddRow(false); ciSegAddRow(false); }
|
||||
}
|
||||
function ciSegSyncRowMode(row) {
|
||||
const m = ($('[data-seg-f="daymode"]', row) || {}).value || "manual";
|
||||
const cManual = $('[data-seg-cell="manual"]', row), cRange = $('[data-seg-cell="range"]', row);
|
||||
if (cManual) cManual.hidden = m !== "manual";
|
||||
if (cRange) cRange.hidden = m !== "range";
|
||||
if (m === "manual") {
|
||||
["d1", "d2"].forEach((k) => { const el = $(`[data-seg-f="${k}"]`, row); if (el) { el.value = ""; el.removeAttribute("aria-invalid"); } });
|
||||
} else {
|
||||
const el = $('[data-seg-f="days"]', row); if (el) el.value = "";
|
||||
}
|
||||
$$(".field-err", row).forEach((e) => e.remove());
|
||||
}
|
||||
function ciSegDelRow(btn) {
|
||||
const row = btn.closest("[data-ci-seg-row]"); if (!row) return;
|
||||
const host = row.parentElement;
|
||||
if (host && host.children.length <= 1) { toast("至少保留一段"); return; }
|
||||
row.remove();
|
||||
initCalc();
|
||||
}
|
||||
function ciSegResetRows(r) {
|
||||
const host = $("[data-ci-seg-rows]", r);
|
||||
if (!host) return;
|
||||
$$("[data-ci-seg-row]", host).forEach((row, i) => {
|
||||
if (i > 1) { row.remove(); return; }
|
||||
["amt", "rate", "days", "d1", "d2"].forEach((k) => {
|
||||
const el = $(`[data-seg-f="${k}"]`, row);
|
||||
if (el) { el.value = ""; el.removeAttribute("aria-invalid"); }
|
||||
});
|
||||
const dm = $('[data-seg-f="daymode"]', row); if (dm) dm.value = "manual";
|
||||
const kd = $('[data-seg-f="kind"]', row); if (kd) kd.value = "annual";
|
||||
const out = $('[data-seg-out]', row); if (out) { out.textContent = ""; out.removeAttribute("data-bad"); }
|
||||
ciSegSyncRowMode(row);
|
||||
});
|
||||
if (!host.children.length) { ciSegAddRow(false); ciSegAddRow(false); }
|
||||
}
|
||||
function ciSegRowDays(row, cm) {
|
||||
const m = ($('[data-seg-f="daymode"]', row) || {}).value || "manual";
|
||||
if (m === "manual") {
|
||||
const n = parseInt(String($('[data-seg-f="days"]', row).value).trim(), 10);
|
||||
return isFinite(n) && n >= 0 ? { days: n } : { err: "请输入有效天数" };
|
||||
}
|
||||
const d1 = $('[data-seg-f="d1"]', row).value, d2 = $('[data-seg-f="d2"]', row).value;
|
||||
const a = ymdParts(d1), b = ymdParts(d2);
|
||||
if (!a || !b) return { err: "起止日期不完整" };
|
||||
const days = ciDaysFromRange(a.ymd, b.ymd, cm);
|
||||
return isFinite(days) && days >= 0 ? { days } : { err: "起止日期倒挂" };
|
||||
}
|
||||
function renderInterestMulti() {
|
||||
const r = root(); const f = r && $("[data-calc-interest]", r); if (!f) return;
|
||||
const slot = $("[data-ci-result]", r);
|
||||
ensureCiSegRows();
|
||||
const cm = f.countMode.value, basis = f.basis.value;
|
||||
const rows = $$("[data-ci-seg-row]", f);
|
||||
const segs = [];
|
||||
let firstErr = "";
|
||||
rows.forEach((row, i) => {
|
||||
const no = i + 1;
|
||||
const amtEl = $('[data-seg-f="amt"]', row), rateEl = $('[data-seg-f="rate"]', row);
|
||||
const P = cNum(amtEl.value), rate = cNum(rateEl.value);
|
||||
const kind = $('[data-seg-f="kind"]', row).value;
|
||||
const res = ciSegRowDays(row, cm);
|
||||
const out = $('[data-seg-out]', row);
|
||||
const blank = !["amt", "rate", "days", "d1", "d2"].some((k) => String($(`[data-seg-f="${k}"]`, row).value).trim() !== "");
|
||||
let rowErr = "";
|
||||
if (blank) rowErr = `第 ${no} 段为空行,请填写或删除`;
|
||||
else if (!(P > 0)) rowErr = `第 ${no} 段:请输入有效本金`;
|
||||
else if (!(rate > 0)) rowErr = `第 ${no} 段:请输入有效利率`;
|
||||
else if (res.err) rowErr = `第 ${no} 段:${res.err}`;
|
||||
if (out) {
|
||||
if (blank) { out.textContent = "空行"; out.setAttribute("data-bad", ""); }
|
||||
else if (res.err) { out.textContent = res.err; out.setAttribute("data-bad", ""); }
|
||||
else { out.textContent = `→ ${res.days} 天`; out.removeAttribute("data-bad"); }
|
||||
}
|
||||
if (rowErr) { if (!firstErr) firstErr = rowErr; return; }
|
||||
const divisor = kind === "annual" ? +basis : kind === "monthly" ? 30 : 1;
|
||||
segs.push({ no, P, rate, kind, days: res.days, interest: P * (rate / 100) * (res.days / divisor) });
|
||||
});
|
||||
if (firstErr || !segs.length) {
|
||||
window._ciLast = null;
|
||||
slot.innerHTML = `
|
||||
<div class="section-title" style="margin:0 0 8px;">计算结果</div>
|
||||
<div class="calc-note" style="margin-top:2px;">${esc(firstErr || "请至少填写一段有效数据")},请修正后再计算</div>`;
|
||||
return;
|
||||
}
|
||||
const interestSum = segs.reduce((s, x) => s + x.interest, 0);
|
||||
const principalSum = segs.reduce((s, x) => s + x.P, 0);
|
||||
const total = principalSum + interestSum;
|
||||
const detail = segs.map((s) => {
|
||||
const div = s.kind === "annual" ? ` ÷ ${basis} ` : s.kind === "monthly" ? " ÷ 30 " : " ";
|
||||
return `<div class="num">第 ${s.no} 段:${cM2(s.P)} × ${s.rate}%${div}× ${s.days} 天 = ${cM2(s.interest)} 元</div>`;
|
||||
}).join("");
|
||||
window._ciLast = { text: `分段利息合计 ${cM2(interestSum)} 元 | 本金合计 ${cM2(principalSum)} 元 | 本息合计 ${cM2(total)} 元 | 共 ${segs.length} 段(${segs.map((s) => `第${s.no}段 ${cM2(s.P)}×${s.rate}%×${s.days}天=${cM2(s.interest)}`).join(";")})` };
|
||||
slot.innerHTML = `
|
||||
<div class="section-title" style="margin:0 0 8px;">计算结果</div>
|
||||
<div style="padding:6px 0 4px;">
|
||||
<div style="font-size:11px; font-weight:700; letter-spacing:0.12em; color:var(--gold);">利息合计</div>
|
||||
<div class="calc-result-hero num">${cM2(interestSum)}</div>
|
||||
<div class="calc-caps-block">
|
||||
<span class="caps-label">大写</span>
|
||||
<span class="calc-caps">${caps(interestSum)}</span>
|
||||
<button type="button" class="caps-copy" data-copy-text="${esc(caps(interestSum))}">复制</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="ci-seg-detail">${detail}</div>
|
||||
<div class="summary-row"><span>本金合计(${segs.length} 段)</span><b class="num">${cM2(principalSum)} 元</b></div>
|
||||
<div class="summary-row"><span>本息合计</span><b class="num">${cM2(total)} 元</b></div>
|
||||
<div class="calc-note">${cm === "inclusive" ? "记首记尾" : "记首不记尾"} · ${basis} 天/年 · 年利率 ÷${basis}、月利率 ÷30、日利率直乘 · 各段独立计息后汇总</div>
|
||||
<button type="button" class="btn btn--plain" style="width:100%; margin-top:12px;" data-calc-act="copy">复制结果</button>`;
|
||||
}
|
||||
|
||||
/* — 承兑贴现计算器 — */
|
||||
function dcHist() {
|
||||
try { const raw = localStorage.getItem("dc_hist_v1"); if (raw) return JSON.parse(raw); } catch (e) {}
|
||||
@@ -418,6 +560,8 @@ function syncCalcModeRows(target) {
|
||||
$('[data-ci-row="manual"]', r).hidden = m !== "manual";
|
||||
$('[data-ci-row="range"]', r).hidden = m !== "range";
|
||||
}
|
||||
const segRow = target.closest && target.closest("[data-ci-seg-row]");
|
||||
if (segRow && target.getAttribute("data-seg-f") === "daymode") ciSegSyncRowMode(segRow);
|
||||
const cd = $("[data-calc-discount]", r);
|
||||
if (cd && target.name === "cdMatMode") {
|
||||
const m = (cd.querySelector('input[name="cdMatMode"]:checked') || {}).value;
|
||||
@@ -464,6 +608,7 @@ function calcAction(act) {
|
||||
if (ci) {
|
||||
ci.amt.value = ""; ci.rate.value = ""; ci.days.value = ""; ci.d1.value = ""; ci.d2.value = "";
|
||||
[ci.d1, ci.d2].forEach((el) => { if (el) setErr(el, ""); });
|
||||
ciSegResetRows(r);
|
||||
initCalc(); toast("已重置"); return;
|
||||
}
|
||||
const cd2 = $("[data-calc-discount]", r);
|
||||
@@ -493,7 +638,7 @@ function calcAction(act) {
|
||||
}
|
||||
function initCalc() {
|
||||
const r = root(); if (!r) return;
|
||||
if ($("[data-calc-interest]", r)) renderInterest();
|
||||
if ($("[data-calc-interest]", r)) { ciTopMode(r) === "multi" ? renderInterestMulti() : renderInterest(); }
|
||||
if ($("[data-calc-discount]", r)) { renderDiscount(); renderDcHist(); }
|
||||
}
|
||||
|
||||
@@ -1316,6 +1461,11 @@ document.addEventListener("click", (e) => {
|
||||
}
|
||||
const sw = t.closest("[data-switch]");
|
||||
if (sw) { sw.setAttribute("aria-checked", String(sw.getAttribute("aria-checked") !== "true")); initCalc(); return; }
|
||||
const ciModeBtn = t.closest("[data-ci-mode]");
|
||||
if (ciModeBtn) { setCIMode(ciModeBtn.dataset.ciMode); return; }
|
||||
if (t.closest("[data-ci-seg-add]")) { ciSegAddRow(); initCalc(); return; }
|
||||
const ciSegDel = t.closest("[data-ci-seg-del]");
|
||||
if (ciSegDel) { ciSegDelRow(ciSegDel); return; }
|
||||
const calcAct = t.closest("[data-calc-act]");
|
||||
if (calcAct) { calcAction(calcAct.dataset.calcAct); return; }
|
||||
const refRate = t.closest("[data-ref-rate]");
|
||||
|
||||
Reference in New Issue
Block a user