Files
bmi-tdee-calculator/tdee_calculator.py
T

124 lines
6.0 KiB
Python
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.
import { useState } from 'react';
import { Flame, Scale, Footprints, Activity } from 'lucide-react';
const stepBands = [
{ min: 0, max: 5000, factor: 1.2, label: '久坐(<5,000 步)' },
{ min: 5000, max: 7500, factor: 1.375, label: '轻度活动(5,000–7,499 步)' },
{ min: 7500, max: 10000, factor: 1.55, label: '中度活动(7,500–9,999 步)' },
{ min: 10000, max: 12500, factor: 1.65, label: '高度活动(10,000–12,499 步)' },
{ min: 12500, max: Infinity, factor: 1.8, label: '非常活跃(≥12,500 步)' },
];
function bandFor(steps) {
return stepBands.find(b => steps >= b.min && steps < b.max) || stepBands[stepBands.length - 1];
}
function bmiCategory(bmi) {
if (bmi < 18.5) return { text: '偏瘦', color: 'text-amber-600' };
if (bmi < 24) return { text: '正常', color: 'text-emerald-600' };
if (bmi < 28) return { text: '超重', color: 'text-orange-600' };
return { text: '肥胖', color: 'text-red-600' };
}
export default function CalorieCalculator() {
const [sex, setSex] = useState('female');
const [age, setAge] = useState(30);
const [height, setHeight] = useState(164);
const [weight, setWeight] = useState(51);
const [steps, setSteps] = useState(10000);
const hM = height / 100;
const bmi = hM > 0 ? weight / (hM * hM) : 0;
const cat = bmiCategory(bmi);
const bmr = sex === 'male'
? 10 * weight + 6.25 * height - 5 * age + 5
: 10 * weight + 6.25 * height - 5 * age - 161;
const band = bandFor(steps);
const tdee = bmr * band.factor;
const round = n => Math.round(n);
const inputCls = 'w-full px-3 py-2 rounded-lg border border-slate-200 focus:outline-none focus:ring-2 focus:ring-sky-400';
return (
<div className="max-w-2xl mx-auto p-6 bg-slate-50 rounded-2xl">
<h2 className="text-2xl font-bold text-slate-800 mb-1">每日热量需求计算器</h2>
<p className="text-sm text-slate-500 mb-6">Mifflin-St Jeor 公式 + 步数活动系数</p>
<div className="grid grid-cols-2 gap-4 mb-6">
<div className="col-span-2">
<label className="block text-sm font-medium text-slate-600 mb-2">性别</label>
<div className="flex gap-3">
<button
onClick={() => setSex('female')}
className={`flex-1 py-2 rounded-lg border font-medium transition ${sex === 'female' ? 'bg-rose-500 text-white border-rose-500' : 'bg-white text-slate-600 border-slate-200'}`}
>女</button>
<button
onClick={() => setSex('male')}
className={`flex-1 py-2 rounded-lg border font-medium transition ${sex === 'male' ? 'bg-sky-500 text-white border-sky-500' : 'bg-white text-slate-600 border-slate-200'}`}
>男</button>
</div>
</div>
<div>
<label className="block text-sm font-medium text-slate-600 mb-2">年龄(岁)</label>
<input type="number" value={age} onChange={e => setAge(Number(e.target.value))} className={inputCls} />
</div>
<div>
<label className="block text-sm font-medium text-slate-600 mb-2">身高(cm)</label>
<input type="number" value={height} onChange={e => setHeight(Number(e.target.value))} className={inputCls} />
</div>
<div>
<label className="block text-sm font-medium text-slate-600 mb-2">体重(kg)</label>
<input type="number" value={weight} onChange={e => setWeight(Number(e.target.value))} className={inputCls} />
</div>
<div>
<label className="block text-sm font-medium text-slate-600 mb-2">每日步数</label>
<input type="number" value={steps} onChange={e => setSteps(Number(e.target.value))} className={inputCls} />
</div>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="bg-white rounded-xl p-4 border border-slate-100">
<div className="flex items-center gap-2 text-slate-500 text-sm mb-1"><Scale size={16} /> BMI</div>
<div className="text-2xl font-bold text-slate-800">{bmi.toFixed(1)}</div>
<div className={`text-sm font-medium ${cat.color}`}>{cat.text}</div>
</div>
<div className="bg-white rounded-xl p-4 border border-slate-100">
<div className="flex items-center gap-2 text-slate-500 text-sm mb-1"><Flame size={16} /> 基础代谢 BMR</div>
<div className="text-2xl font-bold text-slate-800">{round(bmr)}</div>
<div className="text-sm text-slate-400">千卡 / 天</div>
</div>
<div className="bg-white rounded-xl p-4 border border-slate-100">
<div className="flex items-center gap-2 text-slate-500 text-sm mb-1"><Footprints size={16} /> 活动系数</div>
<div className="text-2xl font-bold text-slate-800">× {band.factor}</div>
<div className="text-sm text-slate-400">{band.label}</div>
</div>
<div className="bg-gradient-to-br from-emerald-500 to-teal-600 rounded-xl p-4 text-white">
<div className="flex items-center gap-2 text-emerald-50 text-sm mb-1"><Activity size={16} /> 每日总消耗 TDEE</div>
<div className="text-3xl font-bold">{round(tdee)}</div>
<div className="text-sm text-emerald-50">千卡 / 天(维持体重)</div>
</div>
<div className="col-span-2 bg-white rounded-xl p-4 border border-slate-100">
<div className="text-sm font-medium text-slate-600 mb-2">建议目标热量</div>
<div className="flex justify-between text-sm py-1">
<span className="text-slate-500">增重(每周 +0.3 kg 左右)</span>
<span className="font-semibold text-slate-800">{round(tdee + 300)} 千卡</span>
</div>
<div className="flex justify-between text-sm py-1">
<span className="text-slate-500">维持体重</span>
<span className="font-semibold text-slate-800">{round(tdee)} 千卡</span>
</div>
</div>
</div>
<p className="text-xs text-slate-400 mt-4">* 结果仅供参考,个体肌肉量、代谢与食物热效应等会影响实际需求。</p>
</div>
);
}