|
@@ -1,44 +1,34 @@
|
|
|
-import { Component, OnInit,OnDestroy } from '@angular/core';
|
|
|
+import { Component, OnInit, OnDestroy } from '@angular/core';
|
|
|
import { AlertController, NavController } from '@ionic/angular';
|
|
|
import { Router } from '@angular/router';
|
|
|
import * as Parse from 'parse';
|
|
|
-import { interval, Subscription } from 'rxjs';
|
|
|
|
|
|
@Component({
|
|
|
- selector: 'app-tab1',
|
|
|
- templateUrl: 'tab1.page.html',
|
|
|
- styleUrls: ['tab1.page.scss']
|
|
|
+ selector: 'app-home',
|
|
|
+ templateUrl: './home.page.html',
|
|
|
+ styleUrls: ['./home.page.scss'],
|
|
|
})
|
|
|
-export class Tab1Page implements OnInit,OnDestroy {
|
|
|
- initial: string = '0KG';
|
|
|
- current: string = '0KG';
|
|
|
- target: string = '0KG';
|
|
|
- reward: string = '0元';
|
|
|
+export class HomePage implements OnInit {
|
|
|
+ initial: number = 0;
|
|
|
+ current: number = 0;
|
|
|
+ target: number = 0;
|
|
|
+ reward: number = 0;
|
|
|
username: string = ''; // 当前用户的用户名
|
|
|
blueColor: boolean = false;
|
|
|
- private loadUserDataSubscription: Subscription | undefined;
|
|
|
|
|
|
constructor(
|
|
|
private alertController: AlertController,
|
|
|
private router: Router,
|
|
|
private navCtrl: NavController,
|
|
|
-
|
|
|
) {}
|
|
|
|
|
|
- ngOnInit() {
|
|
|
+ ngOnInit() {}
|
|
|
+
|
|
|
+ ionViewDidEnter() {
|
|
|
this.loadUserData();
|
|
|
- //每隔一秒检测登录状况
|
|
|
- this.loadUserDataSubscription = interval(1000).subscribe(() => {
|
|
|
- this.loadUserData();
|
|
|
- console.log(this.username);
|
|
|
- });
|
|
|
}
|
|
|
|
|
|
- ngOnDestroy() {
|
|
|
- if (this.loadUserDataSubscription) {
|
|
|
- this.loadUserDataSubscription.unsubscribe();
|
|
|
- }
|
|
|
- }
|
|
|
+ ngOnDestroy() {}
|
|
|
|
|
|
async loadUserData() {
|
|
|
const currentUser = Parse.User.current();
|
|
@@ -49,6 +39,7 @@ export class Tab1Page implements OnInit,OnDestroy {
|
|
|
this.username = '未登录';
|
|
|
this.loadData();
|
|
|
}
|
|
|
+ console.log("tab1:" + this.username);
|
|
|
}
|
|
|
|
|
|
async loadData() {
|
|
@@ -57,16 +48,20 @@ export class Tab1Page implements OnInit,OnDestroy {
|
|
|
query.equalTo('username', this.username); // 查找当前用户的数据
|
|
|
const weightStatus = await query.first();
|
|
|
if (weightStatus) {
|
|
|
- this.initial = weightStatus.get('initial') + 'KG' || '--KG';
|
|
|
- this.current = weightStatus.get('current') + 'KG' || '--KG';
|
|
|
- this.target = weightStatus.get('target') + 'KG' || '--KG';
|
|
|
- this.reward = weightStatus.get('reward') + '元' || '--元';
|
|
|
+ this.initial = weightStatus.get('initial') || 0;
|
|
|
+ this.current = weightStatus.get('current') || 0;
|
|
|
+ this.target = weightStatus.get('target') || 0;
|
|
|
+ this.reward = this.calculateReward(this.initial, this.target);
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.error('Error loading data', error);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ calculateReward(initial: number, target: number): number {
|
|
|
+ return (initial - target) * 10;
|
|
|
+ }
|
|
|
+
|
|
|
async openEditModal() {
|
|
|
if (this.username == '未登录') {
|
|
|
// 如果用户未登录,则显示登录提示
|
|
@@ -76,30 +71,32 @@ export class Tab1Page implements OnInit,OnDestroy {
|
|
|
|
|
|
const alert = await this.alertController.create({
|
|
|
header: '编辑体重信息',
|
|
|
+ subHeader: '请根据您的需求编辑以下信息',
|
|
|
inputs: [
|
|
|
{
|
|
|
name: 'initial',
|
|
|
- type: 'text',
|
|
|
+ type: 'number',
|
|
|
placeholder: '初始',
|
|
|
- value: this.initial.replace('KG', '')
|
|
|
+ value: this.initial.toString()
|
|
|
},
|
|
|
{
|
|
|
name: 'current',
|
|
|
- type: 'text',
|
|
|
+ type: 'number',
|
|
|
placeholder: '当前',
|
|
|
- value: this.current.replace('KG', '')
|
|
|
+ value: this.current.toString()
|
|
|
},
|
|
|
{
|
|
|
name: 'target',
|
|
|
- type: 'text',
|
|
|
+ type: 'number',
|
|
|
placeholder: '目标',
|
|
|
- value: this.target.replace('KG', '')
|
|
|
+ value: this.target.toString()
|
|
|
},
|
|
|
{
|
|
|
name: 'reward',
|
|
|
- type: 'text',
|
|
|
+ type: 'number',
|
|
|
placeholder: '赏金',
|
|
|
- value: this.reward.replace('元', '')
|
|
|
+ value: this.reward.toString(),
|
|
|
+ disabled: true // 禁用输入框,显示但不能编辑
|
|
|
}
|
|
|
],
|
|
|
buttons: [
|
|
@@ -117,11 +114,12 @@ export class Tab1Page implements OnInit,OnDestroy {
|
|
|
if (!weightStatus) {
|
|
|
weightStatus = new Parse.Object('weight_status');
|
|
|
}
|
|
|
+ const rewardValue = this.calculateReward(Number(data.initial), Number(data.target));
|
|
|
weightStatus.set('username', this.username);
|
|
|
- weightStatus.set('initial', data.initial);
|
|
|
- weightStatus.set('current', data.current);
|
|
|
- weightStatus.set('target', data.target);
|
|
|
- weightStatus.set('reward', data.reward);
|
|
|
+ weightStatus.set('initial', Number(data.initial));
|
|
|
+ weightStatus.set('current', Number(data.current));
|
|
|
+ weightStatus.set('target', Number(data.target));
|
|
|
+ weightStatus.set('reward', rewardValue);
|
|
|
await weightStatus.save();
|
|
|
this.loadData(); // 更新本地数据
|
|
|
} catch (error) {
|
|
@@ -160,7 +158,6 @@ export class Tab1Page implements OnInit,OnDestroy {
|
|
|
this.blueColor = !this.blueColor;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
//每日习惯按钮颜色变化
|
|
|
buttons = [
|
|
|
{ text: '足量饮水', color: 'light', icon: 'water' },
|
|
@@ -208,8 +205,7 @@ export class Tab1Page implements OnInit,OnDestroy {
|
|
|
return this.buttons.some(btn => btn.color === 'danger');
|
|
|
}
|
|
|
|
|
|
-
|
|
|
navigateToTreePage() {
|
|
|
- this.router.navigate(['/tabs/tree']);
|
|
|
+ this.router.navigate(['/tabs/timer']);
|
|
|
}
|
|
|
}
|