Rename titles to BMI指数, soften audit-sensitive tips, and subscribe remote config so homepage variant and entrance toggles apply within a few seconds. Co-authored-by: Cursor <cursoragent@cursor.com>
377 lines
11 KiB
TypeScript
377 lines
11 KiB
TypeScript
/**
|
||
* 首页 B — BMI 指数(过审默认)
|
||
* 干净工具风:原生顶栏、无品牌壳、少卡片。
|
||
*/
|
||
import { useEffect, useMemo, useState } from 'react'
|
||
import { Input } from '@tarojs/components'
|
||
import Taro from '@tarojs/taro'
|
||
import type { NomadroRemoteConfig } from '@/data/remoteConfig.defaults'
|
||
import { applyH5Theme, type H5Theme } from '@/utils/h5Theme'
|
||
import { H5Box as Box, H5Txt as Txt } from '@/components/H5Box'
|
||
import { trackClick, trackPage } from '@/utils/track'
|
||
import './bmi.scss'
|
||
|
||
const isH5 = process.env.TARO_ENV === 'h5'
|
||
const HISTORY_KEY = 'nomadro_bmi_history_v1'
|
||
const MAX_HISTORY = 8
|
||
|
||
type Gender = 'male' | 'female'
|
||
type BmiLevel = {
|
||
key: string
|
||
label: string
|
||
tip: string
|
||
color: string
|
||
min: number
|
||
max: number
|
||
}
|
||
|
||
const LEVELS: BmiLevel[] = [
|
||
{
|
||
key: 'under',
|
||
label: '偏瘦',
|
||
tip: '指数偏低,可对照下方参考区间。',
|
||
color: '#38bdf8',
|
||
min: 0,
|
||
max: 18.5,
|
||
},
|
||
{
|
||
key: 'normal',
|
||
label: '正常',
|
||
tip: '指数落在常见参考区间内。',
|
||
color: '#34d399',
|
||
min: 18.5,
|
||
max: 24,
|
||
},
|
||
{
|
||
key: 'over',
|
||
label: '超重',
|
||
tip: '指数偏高,可对照下方参考区间。',
|
||
color: '#fbbf24',
|
||
min: 24,
|
||
max: 28,
|
||
},
|
||
{
|
||
key: 'obese',
|
||
label: '肥胖',
|
||
tip: '指数较高,可对照下方参考区间。',
|
||
color: '#f87171',
|
||
min: 28,
|
||
max: 99,
|
||
},
|
||
]
|
||
|
||
type HistoryItem = {
|
||
id: string
|
||
bmi: number
|
||
heightCm: number
|
||
weightKg: number
|
||
label: string
|
||
at: string
|
||
}
|
||
|
||
function clamp(n: number, min: number, max: number) {
|
||
return Math.min(max, Math.max(min, n))
|
||
}
|
||
|
||
function calcBmi(heightCm: number, weightKg: number): number | null {
|
||
if (!heightCm || !weightKg || heightCm < 50 || heightCm > 250 || weightKg < 20 || weightKg > 300) {
|
||
return null
|
||
}
|
||
const m = heightCm / 100
|
||
return Math.round((weightKg / (m * m)) * 10) / 10
|
||
}
|
||
|
||
function levelOf(bmi: number): BmiLevel {
|
||
return LEVELS.find((l) => bmi >= l.min && bmi < l.max) || LEVELS[LEVELS.length - 1]!
|
||
}
|
||
|
||
function idealRange(heightCm: number): { low: number; high: number } | null {
|
||
if (heightCm < 50) return null
|
||
const m = heightCm / 100
|
||
return {
|
||
low: Math.round(18.5 * m * m * 10) / 10,
|
||
high: Math.round(23.9 * m * m * 10) / 10,
|
||
}
|
||
}
|
||
|
||
function estimateBmr(gender: Gender, heightCm: number, weightKg: number): number | null {
|
||
if (!heightCm || !weightKg) return null
|
||
const base = 10 * weightKg + 6.25 * heightCm - 5 * 28
|
||
return Math.round(gender === 'male' ? base + 5 : base - 161)
|
||
}
|
||
|
||
function gaugePercent(bmi: number): number {
|
||
return clamp(((bmi - 12) / 24) * 100, 3, 97)
|
||
}
|
||
|
||
function readHistory(): HistoryItem[] {
|
||
try {
|
||
const raw = Taro.getStorageSync(HISTORY_KEY)
|
||
if (!raw) return []
|
||
const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw
|
||
return Array.isArray(parsed) ? (parsed as HistoryItem[]) : []
|
||
} catch {
|
||
return []
|
||
}
|
||
}
|
||
|
||
function writeHistory(items: HistoryItem[]) {
|
||
try {
|
||
Taro.setStorageSync(HISTORY_KEY, JSON.stringify(items.slice(0, MAX_HISTORY)))
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
}
|
||
|
||
function Stepper({
|
||
label,
|
||
unit,
|
||
value,
|
||
min,
|
||
max,
|
||
step,
|
||
onChange,
|
||
}: {
|
||
label: string
|
||
unit: string
|
||
value: number
|
||
min: number
|
||
max: number
|
||
step: number
|
||
onChange: (n: number) => void
|
||
}) {
|
||
const bump = (d: number) => onChange(clamp(Math.round((value + d) * 10) / 10, min, max))
|
||
return (
|
||
<Box className="bmi-row">
|
||
<Box className="bmi-row-left">
|
||
<Txt className="bmi-row-label">{label}</Txt>
|
||
<Txt className="bmi-row-unit">{unit}</Txt>
|
||
</Box>
|
||
<Box className="bmi-stepper">
|
||
<Box className="bmi-step" onClick={() => bump(-step)}>
|
||
<Txt>−</Txt>
|
||
</Box>
|
||
<Input
|
||
className="bmi-input"
|
||
type="digit"
|
||
value={String(value)}
|
||
onInput={(e) => {
|
||
const n = Number(e.detail.value)
|
||
if (!Number.isFinite(n)) return
|
||
onChange(clamp(n, min, max))
|
||
}}
|
||
/>
|
||
<Box className="bmi-step" onClick={() => bump(step)}>
|
||
<Txt>+</Txt>
|
||
</Box>
|
||
</Box>
|
||
</Box>
|
||
)
|
||
}
|
||
|
||
export default function BmiHome({
|
||
theme,
|
||
}: {
|
||
config: NomadroRemoteConfig
|
||
theme: H5Theme
|
||
}) {
|
||
const [height, setHeight] = useState(170)
|
||
const [weight, setWeight] = useState(60)
|
||
const [gender, setGender] = useState<Gender>('male')
|
||
const [history, setHistory] = useState<HistoryItem[]>([])
|
||
const [justSaved, setJustSaved] = useState(false)
|
||
|
||
useEffect(() => {
|
||
applyH5Theme(theme)
|
||
setHistory(readHistory())
|
||
trackPage('pages/index/index', 'BMI首页', { homeVariant: 'bmi' })
|
||
}, [theme])
|
||
|
||
useEffect(() => {
|
||
if (!isH5) return
|
||
const html = document.documentElement
|
||
const prev = html.style.fontSize
|
||
const lock = () => {
|
||
html.style.fontSize = '16px'
|
||
}
|
||
lock()
|
||
const id = window.setInterval(lock, 500)
|
||
window.addEventListener('resize', lock)
|
||
return () => {
|
||
clearInterval(id)
|
||
window.removeEventListener('resize', lock)
|
||
html.style.fontSize = prev
|
||
}
|
||
}, [])
|
||
|
||
const bmi = useMemo(() => calcBmi(height, weight), [height, weight])
|
||
const level = bmi != null ? levelOf(bmi) : null
|
||
const ideal = useMemo(() => idealRange(height), [height])
|
||
const bmr = useMemo(() => estimateBmr(gender, height, weight), [gender, height, weight])
|
||
|
||
function saveRecord() {
|
||
if (bmi == null || !level) {
|
||
Taro.showToast({ title: '请先输入有效身高体重', icon: 'none' })
|
||
return
|
||
}
|
||
trackClick('保存BMI记录', {
|
||
bmi,
|
||
height,
|
||
weight,
|
||
gender,
|
||
level: level.label,
|
||
})
|
||
const item: HistoryItem = {
|
||
id: `${Date.now()}`,
|
||
bmi,
|
||
heightCm: height,
|
||
weightKg: weight,
|
||
label: level.label,
|
||
at: new Date().toISOString(),
|
||
}
|
||
const next = [item, ...history.filter((h) => h.id !== item.id)].slice(0, MAX_HISTORY)
|
||
setHistory(next)
|
||
writeHistory(next)
|
||
setJustSaved(true)
|
||
Taro.showToast({ title: '已保存到本机', icon: 'success' })
|
||
setTimeout(() => setJustSaved(false), 1400)
|
||
}
|
||
|
||
function clearHistory() {
|
||
trackClick('清空BMI记录', { count: history.length })
|
||
setHistory([])
|
||
writeHistory([])
|
||
Taro.showToast({ title: '已清空', icon: 'none' })
|
||
}
|
||
|
||
return (
|
||
<Box className={`bmi-page${theme === 'light' ? ' bmi-page--light' : ''}`} data-theme={theme}>
|
||
<Box className="bmi-wrap">
|
||
<Box className="bmi-head">
|
||
<Txt className="bmi-head-kicker">Body Mass Index</Txt>
|
||
<Txt className="bmi-head-title">BMI 指数</Txt>
|
||
</Box>
|
||
|
||
<Box className="bmi-panel">
|
||
<Box className="bmi-seg">
|
||
<Box
|
||
className={`bmi-seg-item${gender === 'male' ? ' is-on' : ''}`}
|
||
onClick={() => {
|
||
setGender('male')
|
||
trackClick('选择性别', { gender: 'male' })
|
||
}}
|
||
>
|
||
<Txt>男</Txt>
|
||
</Box>
|
||
<Box
|
||
className={`bmi-seg-item${gender === 'female' ? ' is-on' : ''}`}
|
||
onClick={() => {
|
||
setGender('female')
|
||
trackClick('选择性别', { gender: 'female' })
|
||
}}
|
||
>
|
||
<Txt>女</Txt>
|
||
</Box>
|
||
</Box>
|
||
|
||
<Stepper label="身高" unit="cm" value={height} min={100} max={230} step={1} onChange={setHeight} />
|
||
<Stepper label="体重" unit="kg" value={weight} min={30} max={200} step={0.5} onChange={setWeight} />
|
||
</Box>
|
||
|
||
<Box className="bmi-result">
|
||
<Box className="bmi-result-main">
|
||
<Box className="bmi-result-left">
|
||
<Txt className="bmi-result-k">BMI</Txt>
|
||
<Txt className="bmi-result-v" style={level ? { color: level.color } : undefined}>
|
||
{bmi != null ? bmi.toFixed(1) : '—'}
|
||
</Txt>
|
||
</Box>
|
||
{level ? (
|
||
<Box className="bmi-result-badge" style={{ background: `${level.color}22`, color: level.color }}>
|
||
<Txt>{level.label}</Txt>
|
||
</Box>
|
||
) : null}
|
||
</Box>
|
||
|
||
<Box className="bmi-bar" aria-hidden>
|
||
<Box className="bmi-bar-track">
|
||
{LEVELS.map((l) => (
|
||
<Box key={l.key} className={`bmi-bar-seg bmi-bar-seg--${l.key}`} />
|
||
))}
|
||
</Box>
|
||
{bmi != null ? <Box className="bmi-bar-dot" style={{ left: `${gaugePercent(bmi)}%` }} /> : null}
|
||
</Box>
|
||
|
||
<Box className="bmi-stats">
|
||
{ideal ? (
|
||
<Box className="bmi-stat">
|
||
<Txt className="bmi-stat-k">参考体重</Txt>
|
||
<Txt className="bmi-stat-v">
|
||
{ideal.low}–{ideal.high} kg
|
||
</Txt>
|
||
</Box>
|
||
) : null}
|
||
{bmr != null ? (
|
||
<Box className="bmi-stat">
|
||
<Txt className="bmi-stat-k">估算消耗</Txt>
|
||
<Txt className="bmi-stat-v">{bmr} kcal</Txt>
|
||
</Box>
|
||
) : null}
|
||
</Box>
|
||
|
||
{level ? <Txt className="bmi-tip">{level.tip}</Txt> : null}
|
||
|
||
<Box className={`bmi-btn${justSaved ? ' is-ok' : ''}`} onClick={saveRecord}>
|
||
<Txt>{justSaved ? '已保存' : '保存本次记录'}</Txt>
|
||
</Box>
|
||
</Box>
|
||
|
||
<Box className="bmi-section">
|
||
<Txt className="bmi-section-h">参考区间</Txt>
|
||
<Box className="bmi-legend">
|
||
{LEVELS.map((l) => (
|
||
<Box key={l.key} className={`bmi-legend-row${level?.key === l.key ? ' is-on' : ''}`}>
|
||
<Box className="bmi-legend-dot" style={{ background: l.color }} />
|
||
<Txt className="bmi-legend-name">{l.label}</Txt>
|
||
<Txt className="bmi-legend-range">
|
||
{l.key === 'obese' ? '≥ 28' : `${l.min} – ${(l.max - 0.1).toFixed(1)}`}
|
||
</Txt>
|
||
</Box>
|
||
))}
|
||
</Box>
|
||
</Box>
|
||
|
||
<Box className="bmi-section">
|
||
<Box className="bmi-section-top">
|
||
<Txt className="bmi-section-h">本机记录</Txt>
|
||
{history.length > 0 ? (
|
||
<Box className="bmi-clear" onClick={clearHistory}>
|
||
<Txt>清空</Txt>
|
||
</Box>
|
||
) : null}
|
||
</Box>
|
||
{history.length === 0 ? (
|
||
<Txt className="bmi-empty">还没有记录,算出结果后点保存即可</Txt>
|
||
) : (
|
||
<Box className="bmi-history">
|
||
{history.map((h) => (
|
||
<Box key={h.id} className="bmi-history-row">
|
||
<Txt className="bmi-history-bmi">{h.bmi.toFixed(1)}</Txt>
|
||
<Txt className="bmi-history-meta">
|
||
{h.heightCm}cm · {h.weightKg}kg · {h.label}
|
||
</Txt>
|
||
<Txt className="bmi-history-time">
|
||
{h.at.slice(5, 10).replace('-', '/')} {h.at.slice(11, 16)}
|
||
</Txt>
|
||
</Box>
|
||
))}
|
||
</Box>
|
||
)}
|
||
</Box>
|
||
|
||
<Txt className="bmi-foot">计算结果仅供参考,数据只存在本机。</Txt>
|
||
</Box>
|
||
</Box>
|
||
)
|
||
}
|