page-test.component.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { Component, OnInit } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { AlertController } from '@ionic/angular';
  4. import { FormsModule } from '@angular/forms'; // 导入 FormsModule
  5. import { CommonModule } from '@angular/common'; // 导入 CommonModule
  6. import { IonicModule } from '@ionic/angular'; // 导入 IonicModule
  7. @Component({
  8. selector: 'app-page-test',
  9. templateUrl: './page-test.component.html',
  10. styleUrls: ['./page-test.component.scss'],
  11. standalone: true,
  12. imports: [FormsModule, CommonModule, IonicModule], // 在这里加入 IonicModule
  13. })
  14. export class PageBodyFatComponent implements OnInit {
  15. // 页面属性
  16. height: number | null = null; // 身高
  17. weight: number | null = null; // 体重
  18. age: number | null = null; // 年龄
  19. gender: string = 'male'; // 性别
  20. waist: number | null = null; // 腰围
  21. bodyFatPercentage: number | null = null; // 体脂率
  22. bodyFatStatus: string | null = null; // 体脂率状态提示
  23. constructor(private router: Router, private alertController: AlertController) { }
  24. ngOnInit() {
  25. this.resetForm(); // 页面加载时清空数据
  26. }
  27. // 计算体脂率
  28. async calculateBodyFat() {
  29. if (this.height && this.weight && this.age && this.waist) {
  30. if (this.height > 0 && this.weight > 0 && this.age > 0 && this.waist > 0) {
  31. // 计算 BMI
  32. const heightInMeters = this.height / 100; // 将身高转换为米
  33. const bmi = this.weight / (heightInMeters * heightInMeters);
  34. // 根据性别和腰围、BMI、年龄计算体脂率
  35. let bodyFatPercentage: number;
  36. if (this.gender === 'female') {
  37. // 女性的体脂率估算公式
  38. bodyFatPercentage = 0.1 * this.waist + 0.23 * bmi + 0.15 * this.age;
  39. } else {
  40. // 男性的体脂率估算公式
  41. bodyFatPercentage = 0.1 * this.waist + 0.23 * bmi + 0.15 * this.age;
  42. }
  43. this.bodyFatPercentage = bodyFatPercentage;
  44. this.checkBodyFatStatus(this.bodyFatPercentage);
  45. } else {
  46. const alert = await this.alertController.create({
  47. header: '身高、体重、年龄和腰围不能为零!',
  48. buttons: ['确定']
  49. });
  50. await alert.present(); // 显示弹出框
  51. }
  52. } else {
  53. const alert = await this.alertController.create({
  54. header: '请确保输入完整的身高、体重、年龄和腰围!',
  55. buttons: ['确定']
  56. });
  57. await alert.present(); // 显示弹出框
  58. }
  59. }
  60. // 检查体脂率并给出分类提示
  61. checkBodyFatStatus(bodyFatPercentage: number) {
  62. if (this.gender === 'female') {
  63. if (bodyFatPercentage < 18) {
  64. this.bodyFatStatus = '低体脂:体脂率小于18%,建议增加体脂';
  65. } else if (bodyFatPercentage >= 18 && bodyFatPercentage < 28) {
  66. this.bodyFatStatus = '正常体脂:体脂率在18%-28%之间,保持健康';
  67. } else if (bodyFatPercentage >= 28 && bodyFatPercentage < 33) {
  68. this.bodyFatStatus = '超重:体脂率在28%-33%之间,建议控制体脂';
  69. } else {
  70. this.bodyFatStatus = '肥胖:体脂率大于33%,建议减脂';
  71. }
  72. } else {
  73. if (bodyFatPercentage < 10) {
  74. this.bodyFatStatus = '低体脂:体脂率小于10%,建议增加体脂';
  75. } else if (bodyFatPercentage >= 10 && bodyFatPercentage < 20) {
  76. this.bodyFatStatus = '正常体脂:体脂率在10%-20%之间,保持健康';
  77. } else if (bodyFatPercentage >= 20 && bodyFatPercentage < 25) {
  78. this.bodyFatStatus = '超重:体脂率在20%-25%之间,建议控制体脂';
  79. } else {
  80. this.bodyFatStatus = '肥胖:体脂率大于25%,建议减脂';
  81. }
  82. }
  83. }
  84. // 清空数据
  85. resetForm() {
  86. this.height = null;
  87. this.weight = null;
  88. this.age = null;
  89. this.gender = 'male';
  90. this.waist = null;
  91. this.bodyFatPercentage = null;
  92. this.bodyFatStatus = null; // 清空体脂率状态
  93. }
  94. // 返回上一个页面
  95. goBack() {
  96. this.router.navigate(['/tabs/tab2'], { replaceUrl: true }); // 使用 replaceUrl 强制重载页面
  97. }
  98. }