Files
digital-pilates/services/moodCheckins.ts
richarjiang 83b77615cf feat: Enhance Oxygen Saturation Card with health permissions and loading state management
feat(i18n): Add common translations and mood-related strings in English and Chinese

fix(i18n): Update metabolism titles for consistency in health translations

chore: Update Podfile.lock to include SDWebImage 5.21.4 and other dependency versions

refactor(moodCheckins): Improve mood configuration retrieval with optional translation support

refactor(sleepHealthKit): Replace useI18n with direct i18n import for sleep quality descriptions
2025-11-28 23:48:38 +08:00

172 lines
5.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { api } from './api';
// 心情打卡记录类型
export type MoodCheckin = {
id: string;
userId: string;
moodType: MoodType;
intensity: number; // 1-10
description?: string;
checkinDate: string; // YYYY-MM-DD
createdAt: string; // ISO
updatedAt: string; // ISO
metadata?: Record<string, any>;
};
// 创建心情打卡请求
export type CreateMoodCheckinDto = {
moodType: MoodType;
intensity: number; // 1-10
description?: string;
checkinDate?: string; // YYYY-MM-DD默认今天
metadata?: Record<string, any>;
};
// 更新心情打卡请求
export type UpdateMoodCheckinDto = {
id: string;
moodType?: MoodType;
intensity?: number;
description?: string;
metadata?: Record<string, any>;
};
// 删除心情打卡请求
export type DeleteMoodCheckinDto = {
id: string;
};
// 心情统计数据
export type MoodStatistics = {
totalCheckins: number;
averageIntensity: number;
moodDistribution: Record<MoodType, number>;
mostFrequentMood: MoodType;
};
// 创建心情打卡
export async function createMoodCheckin(dto: CreateMoodCheckinDto): Promise<MoodCheckin> {
return await api.post('/api/mood-checkins', dto);
}
// 更新心情打卡
export async function updateMoodCheckin(dto: UpdateMoodCheckinDto): Promise<MoodCheckin> {
return await api.put('/api/mood-checkins', dto);
}
// 删除心情打卡
export async function deleteMoodCheckin(dto: DeleteMoodCheckinDto): Promise<boolean> {
return await api.delete('/api/mood-checkins', { body: dto });
}
// 获取每日心情记录
export async function getDailyMoodCheckins(date?: string): Promise<MoodCheckin[]> {
const path = date ? `/api/mood-checkins/daily?date=${encodeURIComponent(date)}` : '/api/mood-checkins/daily';
const data = await api.get<MoodCheckin[]>(path);
return Array.isArray(data) ? data : [];
}
// 获取心情历史记录
export async function getMoodCheckinsHistory(params: {
startDate: string;
endDate: string;
moodType?: MoodType;
}): Promise<MoodCheckin[]> {
const queryParams = new URLSearchParams({
startDate: params.startDate,
endDate: params.endDate,
});
if (params.moodType) {
queryParams.append('moodType', params.moodType);
}
const path = `/api/mood-checkins/history?${queryParams.toString()}`;
const data = await api.get<MoodCheckin[]>(path);
return Array.isArray(data) ? data : [];
}
// 获取心情统计数据
export async function getMoodStatistics(params: {
startDate: string;
endDate: string;
}): Promise<MoodStatistics> {
const queryParams = new URLSearchParams({
startDate: params.startDate,
endDate: params.endDate,
});
const path = `/api/mood-checkins/statistics?${queryParams.toString()}`;
return await api.get<MoodStatistics>(path);
}
// 心情类型配置
export const MOOD_CONFIG = {
happy: { image: require('@/assets/images/icons/mood/kaixin.png'), label: '开心', color: '#4CAF50' },
excited: { image: require('@/assets/images/icons/mood/xindong.png'), label: '心动', color: '#E91E63' },
thrilled: { image: require('@/assets/images/icons/mood/xingfen.png'), label: '兴奋', color: '#FF9800' },
calm: { image: require('@/assets/images/icons/mood/pingjing.png'), label: '平静', color: '#2196F3' },
anxious: { image: require('@/assets/images/icons/mood/jiaolv.png'), label: '焦虑', color: '#FF9800' },
sad: { image: require('@/assets/images/icons/mood/nanguo.png'), label: '难过', color: '#2196F3' },
lonely: { image: require('@/assets/images/icons/mood/weiqu.png'), label: '孤独', color: '#9C27B0' },
wronged: { image: require('@/assets/images/icons/mood/weiqu.png'), label: '委屈', color: '#607D8B' },
angry: { image: require('@/assets/images/icons/mood/shengqi.png'), label: '生气', color: '#F44336' },
tired: { image: require('@/assets/images/icons/mood/xinlei.png'), label: '心累', color: '#9C27B0' },
} as const;
// 心情类型定义
export type MoodType =
| 'happy' // 开心
| 'excited' // 心动
| 'thrilled' // 兴奋
| 'calm' // 平静
| 'anxious' // 焦虑
| 'sad' // 难过
| 'lonely' // 孤独
| 'wronged' // 委屈
| 'angry' // 生气
| 'tired'; // 心累
// 心情配置类型
export type MoodConfig = {
[K in MoodType]: {
image: any;
label: string;
color: string;
}
};
// 单个心情选项类型
export type MoodOption = {
type: MoodType;
image: any;
label: string;
color: string;
};
// 获取心情配置
export function getMoodConfig(moodType: MoodType, t?: (key: string) => string) {
const config = MOOD_CONFIG[moodType];
// When translation function is provided, prefer localized labels.
if (t) {
return {
...config,
label: t(`mood.types.${moodType}`),
};
}
return config;
}
// 获取所有心情选项
export function getMoodOptions(t?: (key: string) => string): MoodOption[] {
return Object.entries(MOOD_CONFIG).map(([type, config]) => ({
type: type as MoodType,
...config,
// 如果提供了翻译函数,则使用翻译后的标签,否则使用默认的中文标签
label: t ? t(`mood.types.${type}`) : config.label,
}));
}