/* DevBox · 生活与时间进位:番茄钟 / 秒表 / 倒计时、人民币大写、日期大写、十二时辰 */ (function () { 'use strict' const { el, panelTitle, copy, toast, onInput, pad2 } = DB const btn = (label, fn, cls = '') => el('button', { class: 'btn ' + cls, text: label, onclick: fn }) const pane = (title, right) => { const p = el('div', { class: 'panel' }); if (title) p.append(panelTitle(title, right)); return p } /* ---------- 番茄钟 / 秒表 / 倒计时 ---------- */ DB.tool({ id: 'timer-suite', name: '番茄钟 / 秒表 / 倒计时', group: 'time', icon: 'clock', desc: '25 分钟专注 + 5 分钟休息自动切换,秒表带分段,倒计时到点响铃', keywords: ['pomodoro', 'timer', 'stopwatch', '番茄', '秒表', '倒计时'], mount(root) { let pomo = { left: 25 * 60, run: false, focus: true, rounds: 0, timer: 0 } let sw = { base: 0, run: false, timer: 0, laps: [] } let cd = { left: 300, run: false, timer: 0 } const face = (s) => `${pad2(Math.floor(s / 60))}:${pad2(s % 60)}` // 番茄钟 const pFace = el('div', { class: 'big-time', text: face(pomo.left) }) const pTag = el('div', { class: 'hint', text: '专注 · 第 1 轮' }) const pRender = () => { pFace.textContent = face(pomo.left) pTag.textContent = (pomo.focus ? '专注' : '休息') + ' · 第 ' + (pomo.rounds + 1) + ' 轮' pFace.classList.toggle('run', pomo.run) } const pTick = () => { if (!pomo.run) return pomo.left-- if (pomo.left <= 0) { beep() if (pomo.focus) { pomo.rounds++; pomo.focus = false; pomo.left = 5 * 60 } else { pomo.focus = true; pomo.left = 25 * 60 } } pRender() } const pStart = () => { pomo.run = !pomo.run; if (pomo.run && !pomo.timer) pomo.timer = setInterval(pTick, 1000); pRender() } // 秒表 const sFace = el('div', { class: 'big-time', text: '00:00.0' }) const lapList = el('div', { class: 'result-list' }) const sRender = () => { const ms = sw.base + (sw.run ? Date.now() - sw.start : 0) sFace.textContent = `${pad2(Math.floor(ms / 60000))}:${pad2(Math.floor(ms / 1000) % 60)}.${Math.floor(ms % 1000 / 100)}` } const sTick = () => { sRender(); if (sw.run) sw.timer = requestAnimationFrame(sTick) } const sToggle = () => { if (sw.run) { sw.base += Date.now() - sw.start; sw.run = false; cancelAnimationFrame(sw.timer) } else { sw.run = true; sw.start = Date.now(); sTick() } sRender() } const sLap = () => { const ms = sw.base + (sw.run ? Date.now() - sw.start : 0) const prev = sw.laps.length ? sw.laps[sw.laps.length - 1] : 0 sw.laps.push(ms) lapList.prepend(el('div', { class: 'row between' }, el('span', { text: '第 ' + sw.laps.length + ' 段' }), el('span', { class: 'tag', text: `+${((ms - prev) / 1000).toFixed(1)}s · 总 ${(ms / 1000).toFixed(1)}s` }))) } // 倒计时 const cFace = el('div', { class: 'big-time', text: face(cd.left) }) const cMin = el('input', { type: 'number', value: 5, min: 1, max: 180, style: { width: '80px' } }) const cRender = () => { cFace.textContent = face(cd.left); cFace.classList.toggle('run', cd.run) } const cTick = () => { if (!cd.run) return cd.left-- if (cd.left <= 0) { cd.left = 0; cd.run = false; beep(); beep() } cRender() } const cToggle = () => { if (!cd.run) { cd.left = Math.max(1, Number(cMin.value) * 60); cd.run = true; if (!cd.timer) cd.timer = setInterval(cTick, 1000) } else cd.run = false cRender() } function beep() { try { const ctx = new (window.AudioContext || window.webkitAudioContext)() const o = ctx.createOscillator(), g = ctx.createGain() o.frequency.value = 880; o.type = 'sine'; g.gain.value = 0.15 o.connect(g); g.connect(ctx.destination); o.start() setTimeout(() => { o.stop(); ctx.close() }, 320) } catch (e) { toast('浏览器不让放声音', true) } } pRender(); sRender(); cRender() root.append( pane('番茄钟', el('div', { class: 'row' }, btn('开始 / 暂停', pStart), btn('重置', () => { pomo = { left: 25 * 60, run: false, focus: true, rounds: 0, timer: pomo.timer } ; pRender() }, 'ghost'))), pFace, pTag, pane('秒表', el('div', { class: 'row' }, btn('开始 / 停', sToggle), btn('分段', sLap), btn('清零', () => { sw = { base: 0, run: false, timer: 0, laps: [] }; lapList.textContent = ''; sRender() }, 'ghost'))), sFace, lapList, pane('倒计时', el('div', { class: 'row' }, cMin, el('span', { text: '分钟' }), btn('开始 / 停', cToggle))), cFace ) }, }) /* ---------- 人民币 / 日期大写 ---------- */ const RMB_D = '零壹贰叁肆伍陆柒捌玖' function rmbUpper(num) { const n = Number(num) if (!isFinite(n)) throw new Error('不是数字') if (Math.abs(n) > 1e12) throw new Error('金额太大了') const neg = n < 0 let s = Math.abs(n).toFixed(2) let [int, dec] = s.split('.') const units = ['', '拾', '佰', '仟'] const groups = ['', '万', '亿', '万亿'] let intPart = '' if (int === '0') intPart = '零' else { const chunks = [] let rest = int while (rest.length) { chunks.unshift(rest.slice(-4)); rest = rest.slice(0, -4) } chunks.forEach((chunk, gi) => { const g = chunks.length - 1 - gi if (Number(chunk) === 0) { if (intPart && !intPart.endsWith('零')) intPart += '零'; return } let part = '' for (let i = 0; i < chunk.length; i++) { const d = Number(chunk[i]) const u = units[chunk.length - 1 - i] if (d === 0) { if (!part.endsWith('零') && part) part += '零' } else part += RMB_D[d] + u } intPart += part + groups[g] }) intPart = intPart.replace(/零+$/, '') } const jiao = Number(dec[0]), fen = Number(dec[1]) let decPart = '' if (jiao === 0 && fen === 0) decPart = '整' else { decPart = (jiao ? RMB_D[jiao] + '角' : '零') + (fen ? RMB_D[fen] + '分' : '') if (!jiao) decPart = '零' + RMB_D[fen] + '分' } return (neg ? '负' : '') + intPart + '元' + decPart } const CN_NUM = '零一二三四五六七八九' function dateUpper(d) { const y = d.getFullYear(), m = d.getMonth() + 1, day = d.getDate() const yStr = String(y).split('').map((c) => CN_NUM[Number(c)]).join('') const mStr = m <= 10 ? `十${m === 10 ? '' : CN_NUM[m]}` : `十${CN_NUM[m - 10]}` const dStr = day <= 10 ? (day === 10 ? '十' : CN_NUM[day]) : day < 20 ? '十' + CN_NUM[day - 10] : (day % 10 === 0 ? CN_NUM[Math.floor(day / 10)] + '十' : CN_NUM[Math.floor(day / 10)] + '十' + CN_NUM[day % 10]) return `${yStr}年${mStr}月${dStr}日` } const SHICHEN = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'] const shichen = (d) => { const h = d.getHours() const idx = Math.floor(((h + 1) % 24) / 2) const names = ['子时 23:00-01:00', '丑时 01:00-03:00', '寅时 03:00-05:00', '卯时 05:00-07:00', '辰时 07:00-09:00', '巳时 09:00-11:00', '午时 11:00-13:00', '未时 13:00-15:00', '申时 15:00-17:00', '酉时 17:00-19:00', '戌时 19:00-21:00', '亥时 21:00-23:00'] return SHICHEN[idx] + '时 · ' + names[idx] } const ZODIAC = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'] const zodiac = (d) => ZODIAC[(d.getFullYear() - 4) % 12] const XINGZUO = [[1, 20, '水瓶'], [2, 19, '双鱼'], [3, 21, '白羊'], [4, 20, '金牛'], [5, 21, '双子'], [6, 22, '巨蟹'], [7, 23, '狮子'], [8, 23, '处女'], [9, 23, '天秤'], [10, 24, '天蝎'], [11, 23, '射手'], [12, 22, '摩羯'], [13, 32, '水瓶']] const xingzuo = (d) => { const m = d.getMonth() + 1, day = d.getDate(); const hit = XINGZUO.find(([mm, dd]) => m === mm && day < dd); return (hit ? hit[2] : '水瓶') + '座' } DB.tool({ id: 'cn-format', name: '人民币 / 日期大写', group: 'text', icon: 'type', desc: '金额转大写、日期转汉字、十二时辰、生肖与星座,一次全给出', keywords: ['rmb', '人民币', '大写', '日期', '时辰', '生肖', '星座'], mount(root) { const amt = el('input', { type: 'text', value: '12345.67', class: 'input' }) const dateIn = el('input', { type: 'date' }) dateIn.value = new Date().toISOString().slice(0, 10) const out = el('pre', { class: 'out' }) const run = () => { const lines = [] try { lines.push('金额大写:' + rmbUpper(amt.value)) } catch (e) { lines.push('金额错误:' + e.message) } const d = new Date(dateIn.value + 'T12:00:00') if (!isNaN(d)) { lines.push('日期大写:' + dateUpper(d)) lines.push('十二时辰:' + shichen(d)) lines.push('生肖:' + zodiac(d) + ' 星座:' + xingzuo(d)) lines.push('星期:' + '日一二三四五六'[d.getDay()] + ' 当年第 ' + Math.ceil((d - new Date(d.getFullYear(), 0, 0)) / 86400000) + ' 天') } out.textContent = lines.join('\n') } root.append(pane('输入'), el('div', { class: 'row' }, amt, dateIn, btn('转换', run)), out) onInput(amt, run, 300); dateIn.addEventListener('change', run) run() }, }) })()