添加了药物管理的核心功能,包括: - 药物列表展示和状态管理 - 添加新药物的完整流程 - 服药记录的创建和状态更新 - 药物管理界面,支持激活/停用操作 - Redux状态管理和API服务层 - 相关类型定义和辅助函数 主要文件: - app/(tabs)/medications.tsx - 主界面,集成Redux数据 - app/medications/add-medication.tsx - 添加药物流程 - app/medications/manage-medications.tsx - 药物管理界面 - store/medicationsSlice.ts - Redux状态管理 - services/medications.ts - API服务层 - types/medication.ts - 类型定义
91 lines
2.4 KiB
TypeScript
91 lines
2.4 KiB
TypeScript
/**
|
||
* 药物管理辅助函数
|
||
*/
|
||
|
||
import type { Medication, MedicationDisplayItem, MedicationRecord } from '@/types/medication';
|
||
|
||
/**
|
||
* 将药物和服药记录转换为展示项
|
||
* @param medication 药物信息
|
||
* @param record 服药记录
|
||
* @param imageMap 图片映射(可选)
|
||
* @returns 展示项
|
||
*/
|
||
export function convertToDisplayItem(
|
||
medication: Medication,
|
||
record: MedicationRecord,
|
||
imageMap?: Record<string, any>
|
||
): MedicationDisplayItem {
|
||
// 格式化剂量字符串
|
||
const dosage = `${medication.dosageValue} ${medication.dosageUnit}`;
|
||
|
||
// 提取时间(HH:mm格式)
|
||
const scheduledTime = record.scheduledTime.split('T')[1]?.substring(0, 5) || '00:00';
|
||
|
||
// 频率描述
|
||
const frequency = medication.repeatPattern === 'daily' ? '每日' : '自定义';
|
||
|
||
return {
|
||
id: record.id,
|
||
name: medication.name,
|
||
dosage,
|
||
scheduledTime,
|
||
frequency,
|
||
status: record.status,
|
||
image: imageMap?.[medication.form] || null,
|
||
recordId: record.id,
|
||
medicationId: medication.id,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 批量转换药物记录为展示项
|
||
* @param records 服药记录列表
|
||
* @param medications 药物列表
|
||
* @param imageMap 图片映射(可选)
|
||
* @returns 展示项列表
|
||
*/
|
||
export function convertRecordsToDisplayItems(
|
||
records: MedicationRecord[],
|
||
medications: Medication[],
|
||
imageMap?: Record<string, any>
|
||
): MedicationDisplayItem[] {
|
||
const medicationMap = new Map<string, Medication>();
|
||
medications.forEach((med) => medicationMap.set(med.id, med));
|
||
|
||
return records
|
||
.map((record) => {
|
||
const medication = record.medication || medicationMap.get(record.medicationId);
|
||
if (!medication) return null;
|
||
return convertToDisplayItem(medication, record, imageMap);
|
||
})
|
||
.filter((item): item is MedicationDisplayItem => item !== null);
|
||
}
|
||
|
||
/**
|
||
* 格式化剂量字符串
|
||
* @param value 剂量值
|
||
* @param unit 剂量单位
|
||
* @returns 格式化后的字符串
|
||
*/
|
||
export function formatDosage(value: number, unit: string): string {
|
||
return `${value} ${unit}`;
|
||
}
|
||
|
||
/**
|
||
* 根据剂型获取描述
|
||
* @param form 剂型
|
||
* @returns 描述文本
|
||
*/
|
||
export function getMedicationFormLabel(form: string): string {
|
||
const formLabels: Record<string, string> = {
|
||
capsule: '胶囊',
|
||
pill: '药片',
|
||
injection: '注射',
|
||
spray: '喷雾',
|
||
drop: '滴剂',
|
||
syrup: '糖浆',
|
||
other: '其他',
|
||
};
|
||
return formLabels[form] || '其他';
|
||
} |