feat(health): 统一使用共享的HealthKit权限初始化方法并简化配置
将sleepHealthKit模块中的HealthKit权限初始化逻辑替换为使用health工具中的通用ensureHealthPermissions方法,移除重复的权限配置代码。同时更新后台任务标识符以保持一致性。
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
<dict>
|
<dict>
|
||||||
<key>BGTaskSchedulerPermittedIdentifiers</key>
|
<key>BGTaskSchedulerPermittedIdentifiers</key>
|
||||||
<array>
|
<array>
|
||||||
<string>com.expo.modules.backgroundtask.processing</string>
|
<string>backgroundtask</string>
|
||||||
</array>
|
</array>
|
||||||
<key>CADisableMinimumFrameDurationOnPhone</key>
|
<key>CADisableMinimumFrameDurationOnPhone</key>
|
||||||
<true/>
|
<true/>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import * as BackgroundTask from 'expo-background-task';
|
|||||||
import * as TaskManager from 'expo-task-manager';
|
import * as TaskManager from 'expo-task-manager';
|
||||||
import { TaskManagerTaskBody } from 'expo-task-manager';
|
import { TaskManagerTaskBody } from 'expo-task-manager';
|
||||||
|
|
||||||
export const BACKGROUND_TASK_IDENTIFIER = 'com.expo.modules.backgroundtask.processing';
|
export const BACKGROUND_TASK_IDENTIFIER = 'backgroundtask';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -77,21 +77,21 @@ export async function ensureHealthPermissions(): Promise<boolean> {
|
|||||||
console.log('开始初始化HealthKit...');
|
console.log('开始初始化HealthKit...');
|
||||||
|
|
||||||
resolve(true)
|
resolve(true)
|
||||||
// AppleHealthKit.initHealthKit(PERMISSIONS, (error) => {
|
AppleHealthKit.initHealthKit(PERMISSIONS, (error) => {
|
||||||
// if (error) {
|
if (error) {
|
||||||
// console.error('HealthKit初始化失败:', error);
|
console.error('HealthKit初始化失败:', error);
|
||||||
// // 常见错误处理
|
// 常见错误处理
|
||||||
// if (typeof error === 'string') {
|
if (typeof error === 'string') {
|
||||||
// if (error.includes('not available')) {
|
if (error.includes('not available')) {
|
||||||
// console.warn('HealthKit不可用 - 可能在模拟器上运行或非iOS设备');
|
console.warn('HealthKit不可用 - 可能在模拟器上运行或非iOS设备');
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// resolve(false);
|
resolve(false);
|
||||||
// return;
|
return;
|
||||||
// }
|
}
|
||||||
// console.log('HealthKit初始化成功');
|
console.log('HealthKit初始化成功');
|
||||||
// resolve(true);
|
resolve(true);
|
||||||
// });
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import HealthKit from 'react-native-health';
|
import HealthKit from 'react-native-health';
|
||||||
|
import { ensureHealthPermissions } from './health';
|
||||||
|
|
||||||
// 睡眠阶段枚举
|
// 睡眠阶段枚举
|
||||||
export enum SleepStage {
|
export enum SleepStage {
|
||||||
@@ -59,34 +60,6 @@ export type CompleteSleepData = {
|
|||||||
recommendation: string;
|
recommendation: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
// HealthKit 权限配置
|
|
||||||
const permissions = {
|
|
||||||
permissions: {
|
|
||||||
read: [
|
|
||||||
HealthKit.Constants.Permissions.SleepAnalysis,
|
|
||||||
HealthKit.Constants.Permissions.HeartRate,
|
|
||||||
],
|
|
||||||
write: [], // 我们只读取数据,不写入
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化 HealthKit 权限
|
|
||||||
*/
|
|
||||||
export const initializeHealthKit = (): Promise<boolean> => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
HealthKit.initHealthKit(permissions, (error: string) => {
|
|
||||||
if (error) {
|
|
||||||
console.error('[HealthKit] 权限初始化失败:', error);
|
|
||||||
reject(new Error(error));
|
|
||||||
} else {
|
|
||||||
console.log('[HealthKit] 权限初始化成功');
|
|
||||||
resolve(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建睡眠日期范围 (从前一天 18:00 到当天 12:00)
|
* 创建睡眠日期范围 (从前一天 18:00 到当天 12:00)
|
||||||
*/
|
*/
|
||||||
@@ -397,9 +370,7 @@ export const fetchCompleteSleepData = async (date: Date): Promise<CompleteSleepD
|
|||||||
try {
|
try {
|
||||||
console.log('[Sleep] 开始获取完整睡眠数据...', dayjs(date).format('YYYY-MM-DD'));
|
console.log('[Sleep] 开始获取完整睡眠数据...', dayjs(date).format('YYYY-MM-DD'));
|
||||||
|
|
||||||
// 确保 HealthKit 已初始化
|
await ensureHealthPermissions()
|
||||||
await initializeHealthKit();
|
|
||||||
|
|
||||||
// 获取睡眠样本
|
// 获取睡眠样本
|
||||||
const sleepSamples = await fetchSleepSamples(date);
|
const sleepSamples = await fetchSleepSamples(date);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user