|
@@ -0,0 +1,81 @@
|
|
|
+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; // 体脂率
|
|
|
+
|
|
|
+ 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;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ const alert = await this.alertController.create({
|
|
|
+ header: '身高、体重、年龄和腰围不能为零!',
|
|
|
+ buttons: ['确定']
|
|
|
+ });
|
|
|
+ await alert.present(); // 显示弹出框
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ const alert = await this.alertController.create({
|
|
|
+ header: '请确保输入完整的身高、体重、年龄和腰围!',
|
|
|
+ buttons: ['确定']
|
|
|
+ });
|
|
|
+ await alert.present(); // 显示弹出框
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清空数据
|
|
|
+ resetForm() {
|
|
|
+ this.height = null;
|
|
|
+ this.weight = null;
|
|
|
+ this.age = null;
|
|
|
+ this.gender = 'male';
|
|
|
+ this.waist = null; // 清空腰围
|
|
|
+ this.bodyFatPercentage = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回上一个页面
|
|
|
+ goBack() {
|
|
|
+ this.router.navigate(['/tabs/tab2'], { replaceUrl: true }); // 使用 replaceUrl 强制重载页面
|
|
|
+ }
|
|
|
+}
|