feat(hrv): 添加心率变异性监控和压力评估功能

- 新增 HRV 监听服务,实时监控心率变异性数据
- 实现 HRV 到压力指数的转换算法和压力等级评估
- 添加智能通知服务,在压力偏高时推送健康建议
- 优化日志系统,修复日志丢失问题并增强刷新机制
- 改进个人页面下拉刷新,支持并行数据加载
- 优化勋章数据缓存策略,减少不必要的网络请求
- 重构应用初始化流程,优化权限服务和健康监听服务的启动顺序
- 移除冗余日志输出,提升应用性能
This commit is contained in:
richarjiang
2025-11-18 14:08:20 +08:00
parent 3f21f521ea
commit 21e57634e0
15 changed files with 791 additions and 288 deletions

View File

@@ -0,0 +1,73 @@
import { StressLevel } from '@/utils/stress';
import { notificationService, NotificationData, NotificationTypes } from './notifications';
interface HRVStressNotificationPayload {
hrvValue: number;
stressIndex: number;
stressLevel: StressLevel;
recordedAt: string;
interpretation: string;
recommendations: string[];
dataSource: string;
message?: string;
}
const LEVEL_CONFIG: Record<
StressLevel,
{
emoji: string;
title: string;
priority: NotificationData['priority'];
}
> = {
low: {
emoji: '🧘',
title: '状态放松',
priority: 'normal',
},
moderate: {
emoji: '🙂',
title: '压力适中',
priority: 'normal',
},
high: {
emoji: '⚠️',
title: '压力偏高',
priority: 'high',
},
};
export async function sendHRVStressNotification(
payload: HRVStressNotificationPayload
): Promise<void> {
const config = LEVEL_CONFIG[payload.stressLevel];
const recommendation = payload.recommendations[0];
const lines = [
`HRV ${payload.hrvValue}ms · 压力指数 ${payload.stressIndex}`,
payload.interpretation,
`数据来源:${payload.dataSource}`,
];
if (payload.message) {
lines.push(payload.message);
}
if (recommendation) {
lines.push(`建议:${recommendation}`);
}
await notificationService.sendImmediateNotification({
title: `${config.emoji} ${config.title}`,
body: lines.join('\n'),
data: {
type: NotificationTypes.HRV_STRESS_ALERT,
stressLevel: payload.stressLevel,
stressIndex: payload.stressIndex,
hrvValue: payload.hrvValue,
recordedAt: payload.recordedAt,
url: '/(tabs)/statistics',
},
sound: true,
priority: config.priority ?? 'normal',
});
}