|
@@ -0,0 +1,234 @@
|
|
|
+import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
|
|
+import { IonContent, IonHeader, IonTitle, IonToolbar, IonButton, IonLabel, IonItem, IonList, IonBackButton, IonButtons, IonIcon, IonItemDivider, IonAvatar, IonThumbnail, IonItemOptions, IonItemOption, IonItemSliding, IonInput, IonCheckbox, IonRadio, IonToggle, IonRadioGroup, IonSearchbar, IonSegment, IonSegmentButton, IonDatetime, IonFooter, IonCardContent, IonCardTitle, IonCardHeader, IonCard, IonCol, IonRow, IonGrid, IonChip, IonImg, PickerController, IonPicker } from '@ionic/angular/standalone';
|
|
|
+import { CommonModule } from '@angular/common';
|
|
|
+import { HttpClient } from '@angular/common/http';
|
|
|
+import { pinyin } from 'pinyin-pro';
|
|
|
+import { CloudQuery, CloudUser } from 'src/lib/ncloud';
|
|
|
+
|
|
|
+@Component({
|
|
|
+ selector: 'app-rili',
|
|
|
+ templateUrl: './rili.component.html',
|
|
|
+ styleUrls: ['./rili.component.scss'],
|
|
|
+ standalone: true,
|
|
|
+ imports: [IonContent, IonHeader, IonTitle, IonToolbar, IonButton, IonInput, IonItem, IonList, IonIcon, IonContent, IonHeader, IonTitle, IonToolbar, IonButton, IonLabel, IonLabel, IonList, IonItem, IonBackButton, IonButtons, IonIcon, IonItemDivider, IonAvatar, IonThumbnail, IonItemOptions, IonItemSliding, IonItemOption, IonItemOptions, IonInput, IonCheckbox, IonRadio, IonToggle, IonRadioGroup, IonSearchbar, IonSegment, IonSegmentButton, IonDatetime, IonFooter, IonCardContent, IonCardTitle, IonCardHeader, IonCard, IonCol, IonRow, IonGrid, IonChip, IonImg, CommonModule, IonPicker]
|
|
|
+})
|
|
|
+export class RiliComponent implements OnInit {
|
|
|
+ curYear: number;
|
|
|
+ curMonth: number;
|
|
|
+ week: string[] = ['一', '二', '三', '四', '五', '六', '日'];
|
|
|
+ firstDays: number[] = [];
|
|
|
+ curDays: number[] = [];
|
|
|
+ nextDaysArray: number[] = [];
|
|
|
+ selectedDate: number | null = null;
|
|
|
+
|
|
|
+ pickerColumns: any[] = [];
|
|
|
+ pickerButtons: any[] = [];
|
|
|
+ checkedDates: string[] = [
|
|
|
+
|
|
|
+ ];
|
|
|
+ selectTime: string = ""
|
|
|
+
|
|
|
+ async loadDates() {
|
|
|
+ let currentUser = new CloudUser();
|
|
|
+ const cloudQuery = new CloudQuery("fitUser");
|
|
|
+ cloudQuery.equalTo("user", currentUser.toPointer());
|
|
|
+ const userData = await cloudQuery.find();
|
|
|
+ console.log("userData::", userData[0].data.checkeddays);
|
|
|
+ this.checkedDates = userData[0].data.checkeddays
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Input() value2: string = "";
|
|
|
+ @Output() ionChange2 = new EventEmitter<string>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ weather: any = [
|
|
|
+ ];
|
|
|
+ fetchWeatherData() {
|
|
|
+
|
|
|
+ const apiUrl = '/api/api/weather/city/101240101';
|
|
|
+ this.http.get(apiUrl)
|
|
|
+ .subscribe((response: any) => {
|
|
|
+ const adcode = response;
|
|
|
+ if (adcode) {
|
|
|
+ this.weather = adcode.data.forecast
|
|
|
+ console.log(this.weather);
|
|
|
+ console.log("adcode", adcode);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+ console.error('adcode not found in response:', response);
|
|
|
+ }
|
|
|
+ }, error => {
|
|
|
+ console.error('Error fetching adcode:', error);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ convertToPinyin(text: string): string {
|
|
|
+ const result = pinyin(text, {
|
|
|
+ toneType: 'none',
|
|
|
+ type: 'array',
|
|
|
+ });
|
|
|
+ return result.join('');
|
|
|
+ }
|
|
|
+ getWeatherType(year: number, month: number, day: number): string {
|
|
|
+
|
|
|
+ const dateStr = `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
|
|
|
+
|
|
|
+
|
|
|
+ const weatherData = this.weather.find((item: { ymd: string; }) => item.ymd === dateStr);
|
|
|
+
|
|
|
+
|
|
|
+ return weatherData ? "assets/durian/" + this.convertToPinyin(weatherData.type) + ".png" : '';
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ constructor(private pickerCtrl: PickerController, private http: HttpClient) {
|
|
|
+ const today = new Date();
|
|
|
+ this.curYear = today.getFullYear();
|
|
|
+ this.curMonth = today.getMonth() + 1;
|
|
|
+ this.selectedDate = today.getDate();
|
|
|
+ this.updateCalendar();
|
|
|
+ this.initializePicker();
|
|
|
+ this.loadDates()
|
|
|
+ }
|
|
|
+
|
|
|
+ ngOnInit() {
|
|
|
+ this.fetchWeatherData();
|
|
|
+ }
|
|
|
+ daka() {
|
|
|
+
|
|
|
+ if (this.checkedDates.includes(this.selectTime)) {
|
|
|
+ console.log("之前就有记录");
|
|
|
+ return
|
|
|
+
|
|
|
+ }
|
|
|
+ this.checkedDates.push(this.selectTime)
|
|
|
+ console.log(this.checkedDates);
|
|
|
+
|
|
|
+ }
|
|
|
+ initializePicker() {
|
|
|
+ this.pickerColumns = [
|
|
|
+ {
|
|
|
+ name: 'year',
|
|
|
+ options: this.createYearOptions(),
|
|
|
+ },
|
|
|
+ {
|
|
|
+ name: 'month',
|
|
|
+ options: this.createMonthOptions(),
|
|
|
+ },
|
|
|
+ ];
|
|
|
+
|
|
|
+ this.pickerButtons = [
|
|
|
+ {
|
|
|
+ text: '取消',
|
|
|
+ role: 'cancel',
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '确定',
|
|
|
+ handler: (value: { year: { value: number; }; month: { value: number; }; }) => {
|
|
|
+ this.curYear = value.year.value;
|
|
|
+ this.curMonth = value.month.value;
|
|
|
+ this.updateCalendar();
|
|
|
+ },
|
|
|
+ },
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ updateCalendar() {
|
|
|
+ const firstDay = this.getFirstDayOfMonth(this.curYear, this.curMonth);
|
|
|
+ const daysInMonth = this.getDaysInMonth(this.curYear, this.curMonth);
|
|
|
+
|
|
|
+ this.firstDays = this.createArray(firstDay === 0 ? 6 : firstDay - 1);
|
|
|
+ this.curDays = this.createArray(daysInMonth);
|
|
|
+ this.nextDaysArray = this.createArray(7 - this.getLastDayOfMonth(this.curYear, this.curMonth));
|
|
|
+ }
|
|
|
+
|
|
|
+ preMonth() {
|
|
|
+ this.curMonth--;
|
|
|
+ if (this.curMonth < 1) {
|
|
|
+ this.curYear--;
|
|
|
+ this.curMonth = 12;
|
|
|
+ }
|
|
|
+ this.updateCalendar();
|
|
|
+ }
|
|
|
+
|
|
|
+ nextMonth() {
|
|
|
+ this.curMonth++;
|
|
|
+ if (this.curMonth > 12) {
|
|
|
+ this.curYear++;
|
|
|
+ this.curMonth = 1;
|
|
|
+ }
|
|
|
+ this.updateCalendar();
|
|
|
+ }
|
|
|
+ formatDate(year: number, month: number, day: number): string {
|
|
|
+ return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
|
|
|
+ }
|
|
|
+ selectDate(day: number) {
|
|
|
+ this.selectedDate = day;
|
|
|
+ this.selectTime = `${this.curYear}-${this.curMonth < 10 ? '0' + this.curMonth : this.curMonth}-${day < 10 ? '0' + day : day}`;
|
|
|
+ console.log("组件运作", this.selectTime);
|
|
|
+ this.ionChange2.emit(this.selectTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ tolast(item: number) {
|
|
|
+ this.nextMonth();
|
|
|
+ this.selectedDate = item;
|
|
|
+ }
|
|
|
+
|
|
|
+ topre(item: number) {
|
|
|
+ this.preMonth();
|
|
|
+ this.selectedDate = item;
|
|
|
+ }
|
|
|
+
|
|
|
+ async openPicker() {
|
|
|
+ const picker = await this.pickerCtrl.create({
|
|
|
+ columns: this.pickerColumns,
|
|
|
+ buttons: this.pickerButtons,
|
|
|
+ });
|
|
|
+ await picker.present();
|
|
|
+ }
|
|
|
+
|
|
|
+ createArray(n: number): number[] {
|
|
|
+ return Array.from({ length: n }, (_, index) => index + 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ getDaysInMonth(year: number, month: number): number {
|
|
|
+ return new Date(year, month, 0).getDate();
|
|
|
+ }
|
|
|
+
|
|
|
+ getFirstDayOfMonth(year: number, month: number): number {
|
|
|
+ return new Date(year, month - 1, 1).getDay();
|
|
|
+ }
|
|
|
+
|
|
|
+ getLastDayOfMonth(year: number, month: number): number {
|
|
|
+ return new Date(year, month, 0).getDay();
|
|
|
+ }
|
|
|
+
|
|
|
+ createYearOptions() {
|
|
|
+ const currentYear = new Date().getFullYear();
|
|
|
+ const years = [];
|
|
|
+ for (let i = currentYear - 10; i <= currentYear + 10; i++) {
|
|
|
+ years.push({ text: `${i}`, value: i });
|
|
|
+ }
|
|
|
+ return years;
|
|
|
+ }
|
|
|
+
|
|
|
+ createMonthOptions() {
|
|
|
+ const months = [];
|
|
|
+ for (let i = 1; i <= 12; i++) {
|
|
|
+ months.push({ text: `${i}`, value: i });
|
|
|
+ }
|
|
|
+ return months;
|
|
|
+ }
|
|
|
+}
|