-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealth-page-fixed.js
More file actions
4889 lines (4078 loc) · 161 KB
/
health-page-fixed.js
File metadata and controls
4889 lines (4078 loc) · 161 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 健康页面功能
class HealthPage {
constructor() {
this.currentDate = new Date();
this.sleepCalendarDate = new Date();
this.skincareCalendarDate = new Date();
this.periodCalendarDate = new Date();
this.bowelCalendarDate = new Date();
this.mealDate = new Date();
this.selectedSleepDate = null;
this.selectedSkincareDate = null;
this.selectedSkincareType = 'skincare';
this.selectedPeriodDate = null;
this.selectedExerciseDay = null;
this.selectedMealType = null;
this.selectedMealRating = 0;
this.currentExerciseWeek = new Date();
this.healthData = this.loadHealthData();
this.init();
}
init() {
try {
// 先初始化所有数据结构
this.initializeAllData();
// 然后渲染日历
this.renderSleepCalendar();
this.renderSkincareCalendars();
this.renderPeriodCalendar();
this.renderExerciseWeek();
this.generateBowelCalendar();
this.renderMealRecords();
// 最后绑定事件和加载数据
this.bindEvents();
this.loadTodayData();
} catch (error) {
console.error('初始化失败:', error);
}
}
// 初始化所有数据结构
initializeAllData() {
// 确保所有数据结构都存在
if (!this.healthData.sleep) {
this.healthData.sleep = {};
}
if (!this.healthData.skincare) {
this.healthData.skincare = {};
}
if (!this.healthData.skinStatus) {
this.healthData.skinStatus = {};
}
if (!this.healthData.period) {
this.healthData.period = {
records: {}, // 存储每日的生理期状态
cycles: [], // 存储月经周期记录 [{start: '2026-01-15', end: '2026-01-20', length: 28}]
settings: {
averageCycle: 28, // 平均周期长度
averagePeriod: 5 // 平均月经天数
}
};
}
if (!this.healthData.periodHistory) {
this.healthData.periodHistory = [];
}
if (!this.healthData.periodColors) {
this.healthData.periodColors = {
menstrual: '#ff6b6b',
follicular: '#4ecdc4',
ovulation: '#45b7d1',
luteal: '#f9ca24',
safe: '#6c5ce7'
};
}
if (!this.healthData.bowel) {
this.healthData.bowel = {};
}
if (!this.healthData.meals) {
this.healthData.meals = {};
}
if (!this.healthData.bowelSettings) {
this.healthData.bowelSettings = {
icon: '💩'
};
}
// 初始化护肤类型
this.initializeSkincareTypes();
}
// 加载健康数据
loadHealthData() {
const saved = localStorage.getItem('health-page-data');
if (saved) {
return JSON.parse(saved);
}
return {
sleep: {},
skincare: {},
skinStatus: {},
period: {},
exercise: {},
bowel: {},
meals: {},
skincareTypes: [
{ id: 1, name: '基础护肤', color: '#c9e4c9' },
{ id: 2, name: '面膜', color: '#e4c9f5' },
{ id: 3, name: '精华', color: '#f5e4c9' }
],
skinStatusTypes: [
{ id: 1, name: '状态良好', color: '#c9f5c9' },
{ id: 2, name: '干燥', color: '#f5d4c9' },
{ id: 3, name: '出油', color: '#c9d4f5' }
],
colorPalette: ['#c9e4c9', '#e4c9f5', '#f5e4c9', '#c9f5c9', '#f5d4c9', '#c9d4f5', '#f5c9c9', '#c9f5f5']
};
}
// 保存健康数据
saveHealthData() {
localStorage.setItem('health-page-data', JSON.stringify(this.healthData));
}
// 渲染护肤日历
renderSkincareCalendars() {
this.renderSkincareCalendar();
this.renderSkinStatusCalendar();
this.updateSkincareMonthDisplay();
}
// 更新护肤月份显示
updateSkincareMonthDisplay() {
const monthDisplay = document.getElementById('skincareCalendarMonth');
if (monthDisplay) {
const year = this.skincareCalendarDate.getFullYear();
const month = this.skincareCalendarDate.getMonth() + 1;
monthDisplay.textContent = `${year}年${month}月护肤记录`;
}
}
// 渲染护肤记录日历
renderSkincareCalendar() {
const calendar = document.getElementById('skincareCalendar');
if (calendar) {
this.renderCalendarGrid(calendar, 'skincare');
}
}
// 渲染皮肤状态日历
renderSkinStatusCalendar() {
const calendar = document.getElementById('skinStatusCalendar');
if (calendar) {
this.renderCalendarGrid(calendar, 'skinStatus');
}
}
// 通用日历网格渲染
renderCalendarGrid(calendar, dataType) {
const year = this.skincareCalendarDate.getFullYear();
const month = this.skincareCalendarDate.getMonth();
const firstDay = new Date(year, month, 1).getDay();
const daysInMonth = new Date(year, month + 1, 0).getDate();
const startDay = firstDay === 0 ? 6 : firstDay - 1;
calendar.innerHTML = '';
// 确保数据类型存在
if (!this.healthData[dataType]) {
this.healthData[dataType] = {};
}
// 添加星期标题
const weekdays = ['一', '二', '三', '四', '五', '六', '日'];
weekdays.forEach(day => {
const weekdayCell = document.createElement('div');
weekdayCell.className = 'weekday-header';
weekdayCell.textContent = day;
calendar.appendChild(weekdayCell);
});
// 添加空白格子
for (let i = 0; i < startDay; i++) {
const emptyCell = document.createElement('div');
emptyCell.className = 'calendar-day empty';
calendar.appendChild(emptyCell);
}
// 添加当月日期
for (let day = 1; day <= daysInMonth; day++) {
const dayElement = document.createElement('div');
dayElement.className = 'calendar-day';
const dateKey = `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
const record = this.healthData[dataType][dateKey];
dayElement.innerHTML = `<div class="day-number">${day}</div>`;
if (record) {
dayElement.style.backgroundColor = record.color;
dayElement.classList.add('has-record');
}
// 标记今天
const today = new Date();
if (year === today.getFullYear() &&
month === today.getMonth() &&
day === today.getDate()) {
dayElement.classList.add('today');
}
// 添加点击事件
dayElement.addEventListener('click', () => {
this.openSkincareSelectModal(dateKey, day, month + 1, year, dataType);
});
calendar.appendChild(dayElement);
}
}
// 初始化护肤类型
initializeSkincareTypes() {
// 确保有默认的护肤类型
if (!this.healthData.skincareTypes || this.healthData.skincareTypes.length === 0) {
this.healthData.skincareTypes = [
{ id: 1, name: '基础护肤', color: '#c9e4c9' },
{ id: 2, name: '面膜', color: '#e4c9f5' },
{ id: 3, name: '精华', color: '#f5e4c9' }
];
}
if (!this.healthData.skinStatusTypes || this.healthData.skinStatusTypes.length === 0) {
this.healthData.skinStatusTypes = [
{ id: 1, name: '状态良好', color: '#c9f5c9' },
{ id: 2, name: '干燥', color: '#f5d4c9' },
{ id: 3, name: '出油', color: '#c9d4f5' }
];
}
if (!this.healthData.colorPalette || this.healthData.colorPalette.length === 0) {
this.healthData.colorPalette = ['#c9e4c9', '#e4c9f5', '#f5e4c9', '#c9f5c9', '#f5d4c9', '#c9d4f5', '#f5c9c9', '#c9f5f5'];
}
this.saveHealthData();
}
// 切换护肤日历月份
changeSkincareMonth(delta) {
this.skincareCalendarDate.setMonth(this.skincareCalendarDate.getMonth() + delta);
this.renderSkincareCalendars();
}
// 打开护肤类型管理弹窗
openSkincareTypeModal() {
this.renderSkincareTypeList();
this.renderColorPalette('skincarePalette');
document.getElementById('skincareTypeModal').classList.add('active');
}
// 打开皮肤状态类型管理弹窗
openSkinStatusTypeModal() {
this.renderSkinStatusTypeList();
this.renderColorPalette('skinStatusPalette');
document.getElementById('skinStatusTypeModal').classList.add('active');
}
// 渲染护肤类型列表
renderSkincareTypeList() {
const list = document.getElementById('skincareTypeList');
if (!list) return;
list.innerHTML = '';
this.healthData.skincareTypes.forEach(type => {
const item = document.createElement('div');
item.className = 'type-item';
item.innerHTML = `
<div class="type-info">
<div class="type-color" style="background-color: ${type.color}"></div>
<span class="type-name">${type.name}</span>
</div>
<div class="type-actions">
<button class="delete-type-btn" onclick="deleteSkincareType(${type.id})">删除</button>
</div>
`;
list.appendChild(item);
});
}
// 渲染皮肤状态类型列表
renderSkinStatusTypeList() {
const list = document.getElementById('skinStatusTypeList');
if (!list) return;
list.innerHTML = '';
this.healthData.skinStatusTypes.forEach(type => {
const item = document.createElement('div');
item.className = 'type-item';
item.innerHTML = `
<div class="type-info">
<div class="type-color" style="background-color: ${type.color}"></div>
<span class="type-name">${type.name}</span>
</div>
<div class="type-actions">
<button class="delete-type-btn" onclick="deleteSkinStatusType(${type.id})">删除</button>
</div>
`;
list.appendChild(item);
});
}
// 渲染颜色调色板
renderColorPalette(paletteId) {
const palette = document.getElementById(paletteId);
if (!palette) return;
palette.innerHTML = '';
this.healthData.colorPalette.forEach(color => {
const colorDiv = document.createElement('div');
colorDiv.className = 'palette-color';
colorDiv.style.backgroundColor = color;
colorDiv.addEventListener('click', () => {
if (paletteId === 'skincarePalette') {
const colorInput = document.getElementById('newSkincareTypeColor');
if (colorInput) colorInput.value = color;
} else {
const colorInput = document.getElementById('newSkinStatusTypeColor');
if (colorInput) colorInput.value = color;
}
});
palette.appendChild(colorDiv);
});
}
// 其他必要的方法(完整版本)
renderSleepCalendar() {
const calendar = document.getElementById('sleepCalendar');
const monthDisplay = document.getElementById('sleepCalendarMonth');
if (!calendar) {
console.log('睡眠日历元素不存在');
return;
}
const year = this.sleepCalendarDate.getFullYear();
const month = this.sleepCalendarDate.getMonth();
// 更新月份显示
if (monthDisplay) {
monthDisplay.textContent = `${year}年${month + 1}月睡眠记录`;
}
const firstDay = new Date(year, month, 1).getDay();
const daysInMonth = new Date(year, month + 1, 0).getDate();
const startDay = firstDay === 0 ? 6 : firstDay - 1; // 周一为第一天
calendar.innerHTML = '';
// 确保睡眠数据存在
if (!this.healthData.sleep) {
this.healthData.sleep = {};
}
// 添加星期标题
const weekdays = ['一', '二', '三', '四', '五', '六', '日'];
weekdays.forEach(day => {
const weekdayCell = document.createElement('div');
weekdayCell.className = 'weekday-header';
weekdayCell.textContent = day;
calendar.appendChild(weekdayCell);
});
// 添加空白格子(上个月的日期)
for (let i = 0; i < startDay; i++) {
const emptyCell = document.createElement('div');
emptyCell.className = 'calendar-day empty';
calendar.appendChild(emptyCell);
}
// 添加当月日期
for (let day = 1; day <= daysInMonth; day++) {
const dayElement = document.createElement('div');
dayElement.className = 'calendar-day';
const dateKey = `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
const sleepRecord = this.healthData.sleep[dateKey];
if (sleepRecord && sleepRecord.duration) {
// 显示睡眠时长
dayElement.innerHTML = `
<div class="day-number">${day}</div>
<div class="sleep-duration">${sleepRecord.duration}</div>
`;
dayElement.classList.add('has-sleep-record');
} else {
dayElement.innerHTML = `<div class="day-number">${day}</div>`;
}
// 标记今天
const today = new Date();
if (year === today.getFullYear() &&
month === today.getMonth() &&
day === today.getDate()) {
dayElement.classList.add('today');
}
// 添加点击事件
dayElement.addEventListener('click', () => {
this.openSleepEditModal(dateKey, day, month + 1, year);
});
calendar.appendChild(dayElement);
}
}
// 睡眠相关功能
calculateSleepDuration(sleepHour, sleepMinute, wakeHour, wakeMinute) {
let sleepTime = sleepHour * 60 + sleepMinute;
let wakeTime = wakeHour * 60 + wakeMinute;
// 如果起床时间小于睡觉时间,说明跨夜了
if (wakeTime < sleepTime) {
wakeTime += 24 * 60; // 加一天的分钟数
}
const durationMinutes = wakeTime - sleepTime;
const hours = Math.floor(durationMinutes / 60);
const minutes = durationMinutes % 60;
return `${hours}H${String(minutes).padStart(2, '0')}M`;
}
// 保存睡眠记录
saveSleepRecord() {
const sleepHour = parseInt(document.getElementById('sleepHour').value);
const sleepMinute = parseInt(document.getElementById('sleepMinute').value);
const wakeHour = parseInt(document.getElementById('wakeHour').value);
const wakeMinute = parseInt(document.getElementById('wakeMinute').value);
// 验证输入
if (isNaN(sleepHour) || isNaN(sleepMinute) || isNaN(wakeHour) || isNaN(wakeMinute)) {
alert('请填写完整的睡眠时间');
return;
}
if (sleepHour < 0 || sleepHour > 23 || wakeHour < 0 || wakeHour > 23 ||
sleepMinute < 0 || sleepMinute > 59 || wakeMinute < 0 || wakeMinute > 59) {
alert('请输入有效的时间(小时:0-23,分钟:0-59)');
return;
}
// 计算睡眠时长
const duration = this.calculateSleepDuration(sleepHour, sleepMinute, wakeHour, wakeMinute);
// 确定保存日期(起床日期)
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
const dateKey = `${year}-${month}-${day}`;
// 保存睡眠记录
this.healthData.sleep[dateKey] = {
sleepTime: `${String(sleepHour).padStart(2, '0')}:${String(sleepMinute).padStart(2, '0')}`,
wakeTime: `${String(wakeHour).padStart(2, '0')}:${String(wakeMinute).padStart(2, '0')}`,
duration: duration,
date: dateKey
};
this.saveHealthData();
// 强制重新渲染睡眠日历
setTimeout(() => {
this.renderSleepCalendar();
}, 100);
// 清空输入框
const inputs = ['sleepHour', 'sleepMinute', 'wakeHour', 'wakeMinute'];
inputs.forEach(id => {
const element = document.getElementById(id);
if (element) element.value = '';
});
alert(`睡眠记录已保存!睡眠时长:${duration}`);
}
// 切换睡眠日历月份
changeSleepMonth(delta) {
this.sleepCalendarDate.setMonth(this.sleepCalendarDate.getMonth() + delta);
this.renderSleepCalendar();
}
// 打开睡眠编辑弹窗
openSleepEditModal(dateKey, day, month, year) {
this.selectedSleepDate = dateKey;
// 设置弹窗标题和日期
const titleElement = document.getElementById('sleepEditTitle');
const dateElement = document.getElementById('sleepEditDate');
if (titleElement) titleElement.textContent = '编辑睡眠记录';
if (dateElement) dateElement.textContent = `${year}年${month}月${day}日`;
// 检查是否已有记录
const existingRecord = this.healthData.sleep[dateKey];
const deleteBtn = document.getElementById('deleteSleepBtn');
if (existingRecord) {
// 填充现有数据
const sleepTime = existingRecord.sleepTime.split(':');
const wakeTime = existingRecord.wakeTime.split(':');
const editSleepHour = document.getElementById('editSleepHour');
const editSleepMinute = document.getElementById('editSleepMinute');
const editWakeHour = document.getElementById('editWakeHour');
const editWakeMinute = document.getElementById('editWakeMinute');
if (editSleepHour) editSleepHour.value = parseInt(sleepTime[0]);
if (editSleepMinute) editSleepMinute.value = parseInt(sleepTime[1]);
if (editWakeHour) editWakeHour.value = parseInt(wakeTime[0]);
if (editWakeMinute) editWakeMinute.value = parseInt(wakeTime[1]);
if (deleteBtn) deleteBtn.style.display = 'block';
} else {
// 清空输入框
const inputs = ['editSleepHour', 'editSleepMinute', 'editWakeHour', 'editWakeMinute'];
inputs.forEach(id => {
const element = document.getElementById(id);
if (element) element.value = '';
});
if (deleteBtn) deleteBtn.style.display = 'none';
}
// 显示弹窗
const modal = document.getElementById('sleepEditModal');
if (modal) modal.classList.add('active');
}
// 关闭睡眠编辑弹窗
closeSleepEditModal() {
const modal = document.getElementById('sleepEditModal');
if (modal) modal.classList.remove('active');
this.selectedSleepDate = null;
}
// 保存编辑的睡眠记录
saveEditedSleepRecord() {
const sleepHour = parseInt(document.getElementById('editSleepHour').value);
const sleepMinute = parseInt(document.getElementById('editSleepMinute').value);
const wakeHour = parseInt(document.getElementById('editWakeHour').value);
const wakeMinute = parseInt(document.getElementById('editWakeMinute').value);
// 验证输入
if (isNaN(sleepHour) || isNaN(sleepMinute) || isNaN(wakeHour) || isNaN(wakeMinute)) {
alert('请填写完整的睡眠时间');
return;
}
if (sleepHour < 0 || sleepHour > 23 || wakeHour < 0 || wakeHour > 23 ||
sleepMinute < 0 || sleepMinute > 59 || wakeMinute < 0 || wakeMinute > 59) {
alert('请输入有效的时间(小时:0-23,分钟:0-59)');
return;
}
// 计算睡眠时长
const duration = this.calculateSleepDuration(sleepHour, sleepMinute, wakeHour, wakeMinute);
// 保存睡眠记录
this.healthData.sleep[this.selectedSleepDate] = {
sleepTime: `${String(sleepHour).padStart(2, '0')}:${String(sleepMinute).padStart(2, '0')}`,
wakeTime: `${String(wakeHour).padStart(2, '0')}:${String(wakeMinute).padStart(2, '0')}`,
duration: duration,
date: this.selectedSleepDate
};
this.saveHealthData();
this.renderSleepCalendar();
this.closeSleepEditModal();
alert(`睡眠记录已保存!睡眠时长:${duration}`);
}
// 删除选中的睡眠记录
deleteSelectedSleepRecord() {
if (confirm('确定删除这条睡眠记录吗?')) {
delete this.healthData.sleep[this.selectedSleepDate];
this.saveHealthData();
this.renderSleepCalendar();
this.closeSleepEditModal();
alert('睡眠记录已删除');
}
}
generateBowelCalendar() {
const calendar = document.getElementById('bowelCalendar');
const monthDisplay = document.getElementById('bowelCalendarMonth');
if (!calendar) {
console.log('排便日历元素不存在');
return;
}
const year = this.bowelCalendarDate.getFullYear();
const month = this.bowelCalendarDate.getMonth();
// 更新月份显示
if (monthDisplay) {
monthDisplay.textContent = `${year}年${month + 1}月排便记录`;
}
const firstDay = new Date(year, month, 1).getDay();
const daysInMonth = new Date(year, month + 1, 0).getDate();
const startDay = firstDay === 0 ? 6 : firstDay - 1; // 周一为第一天
calendar.innerHTML = '';
// 确保排便数据存在
if (!this.healthData.bowel) {
this.healthData.bowel = {};
}
// 添加星期标题
const weekdays = ['一', '二', '三', '四', '五', '六', '日'];
weekdays.forEach(day => {
const weekdayCell = document.createElement('div');
weekdayCell.className = 'weekday-header';
weekdayCell.textContent = day;
calendar.appendChild(weekdayCell);
});
// 添加空白格子(上个月的日期)
for (let i = 0; i < startDay; i++) {
const emptyCell = document.createElement('div');
emptyCell.className = 'calendar-day empty';
calendar.appendChild(emptyCell);
}
// 添加当月日期
for (let day = 1; day <= daysInMonth; day++) {
const dayElement = document.createElement('div');
dayElement.className = 'calendar-day';
const dateKey = `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
const bowelRecord = this.healthData.bowel[dateKey];
dayElement.innerHTML = `<div class="day-number">${day}</div>`;
if (bowelRecord) {
dayElement.classList.add('has-bowel-record');
// 添加自定义图标
const iconElement = document.createElement('div');
iconElement.className = 'bowel-icon';
iconElement.textContent = this.healthData.bowelSettings.icon || '💩';
dayElement.appendChild(iconElement);
}
// 标记今天
const today = new Date();
if (year === today.getFullYear() &&
month === today.getMonth() &&
day === today.getDate()) {
dayElement.classList.add('today');
}
// 添加点击事件
dayElement.addEventListener('click', () => {
this.toggleBowelRecord(dateKey, dayElement);
});
calendar.appendChild(dayElement);
}
}
// 切换排便记录
toggleBowelRecord(dateKey, element) {
if (this.healthData.bowel[dateKey]) {
// 删除记录
delete this.healthData.bowel[dateKey];
element.classList.remove('has-bowel-record');
// 移除图标
const iconElement = element.querySelector('.bowel-icon');
if (iconElement) {
iconElement.remove();
}
} else {
// 添加记录
this.healthData.bowel[dateKey] = {
date: dateKey,
time: new Date().toTimeString().slice(0, 5),
recorded: true
};
element.classList.add('has-bowel-record');
// 添加图标
const iconElement = document.createElement('div');
iconElement.className = 'bowel-icon';
iconElement.textContent = this.healthData.bowelSettings.icon || '💩';
element.appendChild(iconElement);
}
this.saveHealthData();
}
// 切换排便日历月份
changeBowelMonth(delta) {
this.bowelCalendarDate.setMonth(this.bowelCalendarDate.getMonth() + delta);
this.generateBowelCalendar();
}
// 打开排便设置弹窗
openBowelSettingsModal() {
this.loadBowelSettings();
document.getElementById('bowelSettingsModal').classList.add('active');
}
// 加载排便设置
loadBowelSettings() {
const iconInput = document.getElementById('bowelIconInput');
const iconPreview = document.getElementById('bowelIconPreview');
const currentIcon = this.healthData.bowelSettings.icon || '💩';
if (iconInput) iconInput.value = currentIcon;
if (iconPreview) iconPreview.textContent = currentIcon;
// 更新选中状态
document.querySelectorAll('.icon-option').forEach(option => {
option.classList.remove('selected');
if (option.textContent === currentIcon) {
option.classList.add('selected');
}
});
}
// 预览排便图标
previewBowelIcon() {
const iconInput = document.getElementById('bowelIconInput');
const iconPreview = document.getElementById('bowelIconPreview');
if (iconInput && iconPreview) {
const newIcon = iconInput.value.trim() || '💩';
iconPreview.textContent = newIcon;
}
}
// 选择排便图标
selectBowelIcon(icon) {
const iconInput = document.getElementById('bowelIconInput');
const iconPreview = document.getElementById('bowelIconPreview');
if (iconInput) iconInput.value = icon;
if (iconPreview) iconPreview.textContent = icon;
// 更新选中状态
document.querySelectorAll('.icon-option').forEach(option => {
option.classList.remove('selected');
if (option.textContent === icon) {
option.classList.add('selected');
}
});
}
// 保存排便设置
saveBowelSettings() {
const iconInput = document.getElementById('bowelIconInput');
if (iconInput) {
const newIcon = iconInput.value.trim() || '💩';
this.healthData.bowelSettings.icon = newIcon;
this.saveHealthData();
this.generateBowelCalendar(); // 重新渲染日历以显示新图标
this.closeBowelSettingsModal();
alert('排便图标设置已保存!');
}
}
// 重置排便图标
resetBowelIcon() {
this.healthData.bowelSettings.icon = '💩';
this.loadBowelSettings();
this.saveHealthData();
this.generateBowelCalendar();
}
// 关闭排便设置弹窗
closeBowelSettingsModal() {
const modal = document.getElementById('bowelSettingsModal');
if (modal) modal.classList.remove('active');
}
// 餐饮记录相关功能
renderMealRecords() {
this.updateMealDateTitle();
this.loadMealData();
this.renderOtherItems(); // 添加渲染其他条目
}
// 更新餐饮日期标题
updateMealDateTitle() {
const titleElement = document.getElementById('mealDateTitle');
if (titleElement) {
const year = this.mealDate.getFullYear();
const month = this.mealDate.getMonth() + 1;
const day = this.mealDate.getDate();
titleElement.textContent = `${year}年${month}月${day}日餐饮记录`;
}
}
// 加载餐饮数据
loadMealData() {
// 使用本地日期而不是UTC日期
const year = this.mealDate.getFullYear();
const month = String(this.mealDate.getMonth() + 1).padStart(2, '0');
const day = String(this.mealDate.getDate()).padStart(2, '0');
const dateKey = `${year}-${month}-${day}`;
const mealData = this.healthData.meals[dateKey];
const mealTypes = ['breakfast', 'lunch', 'dinner', 'other'];
mealTypes.forEach(type => {
const input = document.getElementById(`${type}Input`);
const imagesContainer = document.getElementById(`${type}Images`);
if (input) {
input.value = mealData && mealData[type] ? mealData[type].description || '' : '';
}
if (imagesContainer) {
this.renderMealImages(type, mealData && mealData[type] ? mealData[type].details || [] : []);
}
});
// 绑定输入事件
this.bindMealInputEvents();
}
// 绑定餐饮输入事件
bindMealInputEvents() {
const mealTypes = ['breakfast', 'lunch', 'dinner', 'other'];
mealTypes.forEach(type => {
const input = document.getElementById(`${type}Input`);
if (input) {
input.addEventListener('input', () => {
this.saveMealDescription(type, input.value);
});
}
});
}
// 保存餐饮描述
saveMealDescription(mealType, description) {
// 使用本地日期而不是UTC日期
const year = this.mealDate.getFullYear();
const month = String(this.mealDate.getMonth() + 1).padStart(2, '0');
const day = String(this.mealDate.getDate()).padStart(2, '0');
const dateKey = `${year}-${month}-${day}`;
if (!this.healthData.meals[dateKey]) {
this.healthData.meals[dateKey] = {};
}
if (!this.healthData.meals[dateKey][mealType]) {
this.healthData.meals[dateKey][mealType] = {
description: '',
details: []
};
}
this.healthData.meals[dateKey][mealType].description = description;
this.saveHealthData();
}
// 渲染餐饮图片
renderMealImages(mealType, details) {
const container = document.getElementById(`${mealType}Images`);
if (!container) return;
container.innerHTML = '';
details.forEach((detail, index) => {
if (detail.imageUrl) {
const imageItem = document.createElement('div');
imageItem.className = 'meal-image-item';
imageItem.innerHTML = `
<img src="${detail.imageUrl}" alt="食物图片" onerror="this.parentElement.style.display='none'">
${detail.rating ? `<div class="meal-rating">${detail.rating}⭐</div>` : ''}
`;
// 添加点击查看详情
imageItem.addEventListener('click', () => {
this.viewMealDetail(mealType, index);
});
container.appendChild(imageItem);
}
});
}
// 切换餐饮日期
changeMealDate(delta) {
this.mealDate.setDate(this.mealDate.getDate() + delta);
this.renderMealRecords();
}
// 打开餐饮详情弹窗
openMealDetailModal(mealType) {
this.selectedMealType = mealType;
const titleElement = document.getElementById('mealDetailTitle');
const dateElement = document.getElementById('mealDetailDate');
const mealNames = {
breakfast: '早餐',
lunch: '午餐',
dinner: '晚餐',
other: '其他'
};
if (titleElement) titleElement.textContent = `添加${mealNames[mealType]}详情`;
if (dateElement) {
const year = this.mealDate.getFullYear();
const month = this.mealDate.getMonth() + 1;
const day = this.mealDate.getDate();
dateElement.textContent = `${year}年${month}月${day}日`;
}
// 清空表单
this.clearMealDetailForm();
// 显示弹窗
const modal = document.getElementById('mealDetailModal');
if (modal) modal.classList.add('active');
}
// 清空餐饮详情表单
clearMealDetailForm() {
const descriptionInput = document.getElementById('mealDescription');
const imageUrlInput = document.getElementById('mealImageUrl');
const reviewInput = document.getElementById('mealReview');
const imagePreview = document.getElementById('mealImagePreview');
if (descriptionInput) descriptionInput.value = '';
if (imageUrlInput) imageUrlInput.value = '';
if (reviewInput) reviewInput.value = '';
if (imagePreview) imagePreview.style.display = 'none';
// 重置星级评分
this.selectedMealRating = 0;
this.updateStarRating();
// 隐藏清除按钮
const clearBtn = document.getElementById('clearMealDetailBtn');
if (clearBtn) clearBtn.style.display = 'none';
}
// 预览餐饮图片
previewMealImage() {
const imageUrlInput = document.getElementById('mealImageUrl');
const imagePreview = document.getElementById('mealImagePreview');
const previewImg = document.getElementById('previewImg');
if (imageUrlInput && imagePreview && previewImg) {
const url = imageUrlInput.value.trim();
if (url) {
previewImg.src = url;
imagePreview.style.display = 'block';
previewImg.onerror = () => {
alert('图片加载失败,请检查URL是否正确');
imagePreview.style.display = 'none';
};
} else {
imagePreview.style.display = 'none';
}
}
}
// 更新星级评分显示
updateStarRating() {
const stars = document.querySelectorAll('.star');
stars.forEach((star, index) => {
if (index < this.selectedMealRating) {
star.classList.add('active');
} else {
star.classList.remove('active');