275 lines
6.6 KiB
TypeScript
275 lines
6.6 KiB
TypeScript
/**
|
||
* 健康档案 API 服务
|
||
* Base URL: /api/health-profiles
|
||
*/
|
||
|
||
import { api } from './api';
|
||
|
||
// ==================== 类型定义 ====================
|
||
|
||
// 健康史分类
|
||
export type HealthHistoryCategory = 'allergy' | 'disease' | 'surgery' | 'familyDisease';
|
||
|
||
// 健康史条目
|
||
export interface HealthHistoryItem {
|
||
id: string;
|
||
name: string;
|
||
diagnosisDate?: string; // YYYY-MM-DD
|
||
isRecommendation?: boolean;
|
||
note?: string;
|
||
}
|
||
|
||
// 健康史分类数据
|
||
export interface HealthHistoryCategoryData {
|
||
hasHistory: boolean | null;
|
||
items: HealthHistoryItem[];
|
||
}
|
||
|
||
// 完整健康史数据
|
||
export interface HealthHistoryData {
|
||
allergy: HealthHistoryCategoryData;
|
||
disease: HealthHistoryCategoryData;
|
||
surgery: HealthHistoryCategoryData;
|
||
familyDisease: HealthHistoryCategoryData;
|
||
}
|
||
|
||
// 健康档案概览
|
||
export interface HealthProfileOverview {
|
||
basicInfo: {
|
||
progress: number;
|
||
data: {
|
||
height: string;
|
||
weight: string;
|
||
bmi: string;
|
||
waistCircumference: number | null;
|
||
};
|
||
};
|
||
healthHistory: {
|
||
progress: number;
|
||
answeredCategories: HealthHistoryCategory[];
|
||
pendingCategories: HealthHistoryCategory[];
|
||
};
|
||
medications: {
|
||
activeCount: number;
|
||
todayCompletionRate: number;
|
||
};
|
||
}
|
||
|
||
// 健康史进度
|
||
export interface HealthHistoryProgress {
|
||
progress: number;
|
||
details: Record<HealthHistoryCategory, boolean>;
|
||
}
|
||
|
||
// 更新健康史请求
|
||
export interface UpdateHealthHistoryRequest {
|
||
hasHistory: boolean;
|
||
items?: Array<{
|
||
name: string;
|
||
date?: string;
|
||
isRecommendation?: boolean;
|
||
note?: string;
|
||
}>;
|
||
}
|
||
|
||
// ==================== 家庭健康管理类型 ====================
|
||
|
||
// 家庭成员角色
|
||
export type FamilyRole = 'owner' | 'admin' | 'member';
|
||
|
||
// 家庭组
|
||
export interface FamilyGroup {
|
||
id: string;
|
||
name: string;
|
||
ownerId: string;
|
||
memberCount: number;
|
||
maxMembers: number;
|
||
createdAt: string;
|
||
}
|
||
|
||
// 家庭成员
|
||
export interface FamilyMember {
|
||
id: string;
|
||
userId: string;
|
||
nickname: string;
|
||
avatar: string;
|
||
role: FamilyRole;
|
||
relationship: string | null;
|
||
canViewHealthData: boolean;
|
||
canManageHealthData: boolean;
|
||
receiveAlerts: boolean;
|
||
joinedAt: string;
|
||
}
|
||
|
||
// 邀请码
|
||
export interface InviteCode {
|
||
inviteCode: string;
|
||
expiresAt: string;
|
||
}
|
||
|
||
// 更新成员权限请求
|
||
export interface UpdateMemberPermissionsRequest {
|
||
role?: 'admin' | 'member';
|
||
canViewHealthData?: boolean;
|
||
canManageHealthData?: boolean;
|
||
receiveAlerts?: boolean;
|
||
}
|
||
|
||
// ==================== 健康档案概览 API ====================
|
||
|
||
/**
|
||
* 获取健康档案概览
|
||
*/
|
||
export async function getHealthProfileOverview(): Promise<HealthProfileOverview> {
|
||
return api.get<HealthProfileOverview>('/api/health-profiles/overview');
|
||
}
|
||
|
||
// ==================== 健康史 API ====================
|
||
|
||
/**
|
||
* 获取完整健康史
|
||
*/
|
||
export async function getHealthHistory(): Promise<HealthHistoryData> {
|
||
return api.get<HealthHistoryData>('/api/health-profiles/history');
|
||
}
|
||
|
||
/**
|
||
* 更新指定分类的健康史
|
||
* @param category 分类: allergy | disease | surgery | familyDisease
|
||
* @param data 更新数据
|
||
*/
|
||
export async function updateHealthHistory(
|
||
category: HealthHistoryCategory,
|
||
data: UpdateHealthHistoryRequest
|
||
): Promise<HealthHistoryCategoryData> {
|
||
return api.put<HealthHistoryCategoryData>(
|
||
`/api/health-profiles/history/${category}`,
|
||
data
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 获取健康史完成度
|
||
*/
|
||
export async function getHealthHistoryProgress(): Promise<HealthHistoryProgress> {
|
||
return api.get<HealthHistoryProgress>('/api/health-profiles/history/progress');
|
||
}
|
||
|
||
// ==================== 就医资料 API ====================
|
||
|
||
export type MedicalRecordType = 'medical_record' | 'prescription';
|
||
|
||
export interface MedicalRecordItem {
|
||
id: string;
|
||
type: MedicalRecordType;
|
||
title: string;
|
||
date: string; // YYYY-MM-DD
|
||
images: string[]; // Image URLs
|
||
note?: string;
|
||
createdAt: string; // ISO 8601
|
||
updatedAt: string; // ISO 8601
|
||
}
|
||
|
||
export interface MedicalRecordsData {
|
||
records: MedicalRecordItem[];
|
||
prescriptions: MedicalRecordItem[];
|
||
}
|
||
|
||
/**
|
||
* 获取就医资料列表
|
||
*/
|
||
export async function getMedicalRecords(): Promise<MedicalRecordsData> {
|
||
return api.get<MedicalRecordsData>('/api/health-profiles/medical-records');
|
||
}
|
||
|
||
/**
|
||
* 添加就医资料
|
||
*/
|
||
export async function addMedicalRecord(data: Omit<MedicalRecordItem, 'id' | 'createdAt' | 'updatedAt'>): Promise<MedicalRecordItem> {
|
||
return api.post<MedicalRecordItem>('/api/health-profiles/medical-records', data);
|
||
}
|
||
|
||
/**
|
||
* 删除就医资料
|
||
*/
|
||
export async function deleteMedicalRecord(id: string): Promise<void> {
|
||
return api.delete<void>(`/api/health-profiles/medical-records/${id}`);
|
||
}
|
||
|
||
// ==================== 家庭健康管理 API ====================
|
||
|
||
/**
|
||
* 获取用户所属家庭组
|
||
*/
|
||
export async function getFamilyGroup(): Promise<FamilyGroup | null> {
|
||
try {
|
||
return await api.get<FamilyGroup>('/api/health-profiles/family/group');
|
||
} catch (error: any) {
|
||
// 如果用户没有家庭组,返回 null
|
||
if (error?.status === 404) {
|
||
return null;
|
||
}
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 生成家庭组邀请码
|
||
* @param expiresInHours 过期时间(小时),默认24小时
|
||
*/
|
||
export async function generateInviteCode(expiresInHours: number = 24): Promise<InviteCode> {
|
||
return api.post<InviteCode>('/api/health-profiles/family/group/invite', { expiresInHours });
|
||
}
|
||
|
||
/**
|
||
* 通过邀请码加入家庭组
|
||
* @param inviteCode 邀请码
|
||
* @param relationship 与创建者的关系(如:配偶、父母、子女等)
|
||
*/
|
||
export async function joinFamilyGroup(
|
||
inviteCode: string,
|
||
relationship: string
|
||
): Promise<FamilyGroup> {
|
||
return api.post<FamilyGroup>('/api/health-profiles/family/group/join', {
|
||
inviteCode,
|
||
relationship,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 获取家庭成员列表
|
||
*/
|
||
export async function getFamilyMembers(): Promise<FamilyMember[]> {
|
||
return api.get<FamilyMember[]>('/api/health-profiles/family/members');
|
||
}
|
||
|
||
/**
|
||
* 更新家庭成员权限(仅 owner/admin 可操作)
|
||
* @param memberId 成员ID
|
||
* @param permissions 权限设置
|
||
*/
|
||
export async function updateFamilyMember(
|
||
memberId: string,
|
||
permissions: UpdateMemberPermissionsRequest
|
||
): Promise<FamilyMember> {
|
||
return api.put<FamilyMember>(
|
||
`/api/health-profiles/family/members/${memberId}`,
|
||
permissions
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 移除家庭成员(仅 owner/admin 可操作)
|
||
* @param memberId 成员ID
|
||
*/
|
||
export async function removeFamilyMember(memberId: string): Promise<void> {
|
||
return api.delete(`/api/health-profiles/family/members/${memberId}`);
|
||
}
|
||
|
||
/**
|
||
* 退出家庭组(非 owner 成员使用)
|
||
*/
|
||
export async function leaveFamilyGroup(): Promise<void> {
|
||
return api.post('/api/health-profiles/family/leave');
|
||
}
|