123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- import { Component, OnInit } from '@angular/core';
- import { Router } from '@angular/router';
- import { AlertController } from '@ionic/angular';
- import { FormsModule } from '@angular/forms'; // 导入 FormsModule
- import { CommonModule } from '@angular/common'; // 导入 CommonModule
- import { IonicModule } from '@ionic/angular'; // 导入 IonicModule
- @Component({
- selector: 'app-page-test',
- templateUrl: './page-test.component.html',
- styleUrls: ['./page-test.component.scss'],
- standalone: true,
- imports: [FormsModule, CommonModule, IonicModule], // 在这里加入 IonicModule
- })
- export class PageBodyFatComponent implements OnInit {
- // 页面属性
- height: number | null = null; // 身高
- weight: number | null = null; // 体重
- age: number | null = null; // 年龄
- gender: string = 'male'; // 性别
- waist: number | null = null; // 腰围
- bodyFatPercentage: number | null = null; // 体脂率
- bodyFatStatus: string | null = null; // 体脂率状态提示
- constructor(private router: Router, private alertController: AlertController) { }
- ngOnInit() {
- this.resetForm(); // 页面加载时清空数据
- }
- // 计算体脂率
- async calculateBodyFat() {
- if (this.height && this.weight && this.age && this.waist) {
- if (this.height > 0 && this.weight > 0 && this.age > 0 && this.waist > 0) {
- // 计算 BMI
- const heightInMeters = this.height / 100; // 将身高转换为米
- const bmi = this.weight / (heightInMeters * heightInMeters);
- // 根据性别和腰围、BMI、年龄计算体脂率
- let bodyFatPercentage: number;
-
- if (this.gender === 'female') {
- // 女性的体脂率估算公式
- bodyFatPercentage = 0.1 * this.waist + 0.23 * bmi + 0.15 * this.age;
- } else {
- // 男性的体脂率估算公式
- bodyFatPercentage = 0.1 * this.waist + 0.23 * bmi + 0.15 * this.age;
- }
- this.bodyFatPercentage = bodyFatPercentage;
- this.checkBodyFatStatus(this.bodyFatPercentage);
- } else {
- const alert = await this.alertController.create({
- header: '身高、体重、年龄和腰围不能为零!',
- buttons: ['确定']
- });
- await alert.present(); // 显示弹出框
- }
- } else {
- const alert = await this.alertController.create({
- header: '请确保输入完整的身高、体重、年龄和腰围!',
- buttons: ['确定']
- });
- await alert.present(); // 显示弹出框
- }
- }
- // 检查体脂率并给出分类提示
- checkBodyFatStatus(bodyFatPercentage: number) {
- if (this.gender === 'female') {
- if (bodyFatPercentage < 18) {
- this.bodyFatStatus = '低体脂:体脂率小于18%,建议增加体脂';
- } else if (bodyFatPercentage >= 18 && bodyFatPercentage < 28) {
- this.bodyFatStatus = '正常体脂:体脂率在18%-28%之间,保持健康';
- } else if (bodyFatPercentage >= 28 && bodyFatPercentage < 33) {
- this.bodyFatStatus = '超重:体脂率在28%-33%之间,建议控制体脂';
- } else {
- this.bodyFatStatus = '肥胖:体脂率大于33%,建议减脂';
- }
- } else {
- if (bodyFatPercentage < 10) {
- this.bodyFatStatus = '低体脂:体脂率小于10%,建议增加体脂';
- } else if (bodyFatPercentage >= 10 && bodyFatPercentage < 20) {
- this.bodyFatStatus = '正常体脂:体脂率在10%-20%之间,保持健康';
- } else if (bodyFatPercentage >= 20 && bodyFatPercentage < 25) {
- this.bodyFatStatus = '超重:体脂率在20%-25%之间,建议控制体脂';
- } else {
- this.bodyFatStatus = '肥胖:体脂率大于25%,建议减脂';
- }
- }
- }
- // 清空数据
- resetForm() {
- this.height = null;
- this.weight = null;
- this.age = null;
- this.gender = 'male';
- this.waist = null;
- this.bodyFatPercentage = null;
- this.bodyFatStatus = null; // 清空体脂率状态
- }
- // 返回上一个页面
- goBack() {
- this.router.navigate(['/tabs/tab2'], { replaceUrl: true }); // 使用 replaceUrl 强制重载页面
- }
- }
|