feat: 支持食物库接口

This commit is contained in:
richarjiang
2025-08-29 09:41:05 +08:00
parent c15a9176f4
commit 8d567fb4cb
14 changed files with 1349 additions and 234 deletions

View File

@@ -65,11 +65,11 @@ export async function getDietRecords({
total: number
page: number
limit: number
}>(`/users/diet-records${params}`);
}>(`/diet-records${params}`);
}
export async function deleteDietRecord(recordId: number): Promise<void> {
await api.delete(`/users/diet-records/${recordId}`);
await api.delete(`/diet-records/${recordId}`);
}
export function calculateNutritionSummary(records: DietRecord[]): NutritionSummary {

View File

@@ -0,0 +1,49 @@
import type {
FoodItemDto,
FoodLibraryResponseDto,
GetFoodByIdParams,
SearchFoodsParams
} from '@/types/food';
import { api } from './api';
/**
* 食物库 API 服务
*/
export class FoodLibraryApi {
private static readonly BASE_PATH = '/food-library';
/**
* 获取食物库列表
*/
static async getFoodLibrary(): Promise<FoodLibraryResponseDto> {
return api.get<FoodLibraryResponseDto>(this.BASE_PATH);
}
/**
* 搜索食物
*/
static async searchFoods(params: SearchFoodsParams): Promise<FoodItemDto[]> {
const { keyword } = params;
if (!keyword || keyword.trim().length === 0) {
return [];
}
const encodedKeyword = encodeURIComponent(keyword.trim());
return api.get<FoodItemDto[]>(`${this.BASE_PATH}/search?keyword=${encodedKeyword}`);
}
/**
* 根据ID获取食物详情
*/
static async getFoodById(params: GetFoodByIdParams): Promise<FoodItemDto> {
const { id } = params;
return api.get<FoodItemDto>(`${this.BASE_PATH}/${id}`);
}
}
// 导出便捷方法
export const foodLibraryApi = {
getFoodLibrary: () => FoodLibraryApi.getFoodLibrary(),
searchFoods: (keyword: string) => FoodLibraryApi.searchFoods({ keyword }),
getFoodById: (id: number) => FoodLibraryApi.getFoodById({ id }),
};