File
Metadata
selector |
app-home |
styleUrls |
home.component.scss |
templateUrl |
home.component.html |
Constructor
constructor(formBuilder: FormBuilder, alertController: AlertController)
|
Methods
isNewDay
|
isNewDay()
|
Returns: any
|
getProfilePointer
|
getProfilePointer()
|
Returns: void
|
upload
|
upload()
|
Returns: void
|
Eatupload
|
Eatupload()
|
Returns: void
|
getLastUploadTime
|
getLastUploadTime()
|
Returns: any
|
presentAlert
|
presentAlert()
|
Returns: void
|
initializeHighlightedDates
|
initializeHighlightedDates()
|
Returns: void
|
getDate
|
getDate()
|
Returns: any
|
alertButtons
|
alertButtons: string[]
|
highlightedDates
|
highlightedDates: { date: string; textColor: string; backgroundColor: string; }[]
|
lastUploadDate
|
lastUploadDate: any
|
title
|
title: string
|
Default value: app-angular
|
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { AlertController } from '@ionic/angular';
import * as Parse from "parse"
(Parse as any).serverURL = "http://metapunk.cn:9999/parse"
Parse.initialize("dev")
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
alertButtons = ['好的'];
myForm: any;
ngOnInit() {
this.myForm = this.formBuilder.group({
height: ['', Validators.required],
weight: ['', Validators.required]
});
}
title = 'app-angular';
height: number | null = null;
weight: number | null = null;
food: string | null = null;
today: Date = new Date();
lastUploadDate: Promise<Date | null> = this.getLastUploadTime();
async isNewDay(): Promise<boolean> {
const currentDate = new Date();
const lastUploadDate = await this.lastUploadDate;
return (
lastUploadDate === null ||
currentDate.getDate() !== lastUploadDate.getDate() ||
currentDate.getMonth() !== lastUploadDate.getMonth() ||
currentDate.getFullYear() !== lastUploadDate.getFullYear()
);
}
async getProfilePointer() {
// 定义要查询的profileId
const profileId = "肖宇杰";
// 创建查询对象
const Profile = Parse.Object.extend("Profile");
const query = new Parse.Query(Profile);
// 设置查询条件
query.equalTo("name", profileId);
try {
// 执行查询
const result = await query.first();
if (result) {
// 查询成功,result为查询到的行对象
const profilePointer = {
__type: "Pointer",
className: "Profile",
objectId: result.id
};
console.log("指向Profile表中name为profileId的行的指针:", profilePointer);
return profilePointer;
} else {
// 没有找到匹配的行
console.log("找不到匹配的行");
return null;
}
} catch (error) {
// 查询出错
console.log("查询出错:", error);
return null;
}
}
async upload() {
// await this.isNewDay()
if (true) {
if (this.height && this.weight) { // 添加验证条件
// 在这里编写上传逻辑
let YourClass = Parse.Object.extend("HealthData");
let yourObject = new YourClass();
yourObject.set("profile", await this.getProfilePointer());
yourObject.set({ "height": this.height, "weight": this.weight });
yourObject.save().then((result: any) => {
console.log("数据上传成功", result);
this.height = null;
this.weight = null;
}).catch((error: any) => {
console.error("数据上传失败", error);
});
this.lastUploadDate = Promise.resolve(this.today);
} else {
console.log("身高或体重为空");
}
} else {
console.log("今天已经上传过了");
this.height = null;
this.weight = null;
}
}
async Eatupload() {
if (!this.food) {
// 显示错误消息或执行其他操作
console.log("请输入食物");
return;
}
let YourClass = Parse.Object.extend("HealthEat");
let yourObject = new YourClass();
yourObject.set({ "food": this.food, });
yourObject.save().then((result: any) => {
console.log("数据上传成功", result);
this.food = ""
}).catch((error: any) => {
console.error("数据上传失败", error);
});
}
async getLastUploadTime(): Promise<Date | null> {
const Upload = Parse.Object.extend("HealthData");
const query = new Parse.Query(Upload);
query.descending("createdAt");
try {
const result = await query.first();
if (result) {
const lastUploadTime = result.get("createdAt");
console.log("最后一次上传时间:", lastUploadTime);
return lastUploadTime;
} else {
return null;
}
} catch (error) {
console.error("Error:", error);
return null;
}
}
date: Promise<string[] | null> = this.getDate();
highlightedDates: { date: string | null, textColor: string, backgroundColor: string }[] = [];
constructor(private formBuilder: FormBuilder, private alertController: AlertController) {
this.initializeHighlightedDates();
myForm: FormGroup;
}
async presentAlert() {
const alert = await this.alertController.create({
message: '上传成功',
buttons: ['OK'],
});
await alert.present();
}
async initializeHighlightedDates() {
const dates = await this.date;
if (dates && dates.length > 0) {
this.highlightedDates = dates.map(date => ({
date,
textColor: '#09721b',
backgroundColor: '#c8e5d0',
}));
}
}
async getDate(): Promise<string[] | null> {
const Upload = Parse.Object.extend("HealthData");
const query = new Parse.Query(Upload);
query.descending("createdAt");
try {
const result = await query.find();
if (result.length > 0) {
const Dates = result.map((result) => {
const date = result.get("createdAt");
return date.toISOString().split('T')[0];
}).reverse();
console.log("日期数组", Dates);
return Dates;
} else {
return null;
}
} catch (error) {
console.error("Error:", error);
return null;
}
}
}