diff --git a/app/api/history.py b/app/api/history.py index f6c2993..a206fe5 100644 --- a/app/api/history.py +++ b/app/api/history.py @@ -48,6 +48,10 @@ async def search_history(history: HistorySearchIn) -> HistorySearchOut: for date, users in data.items(): for user, records in users.items(): record = await Config.merge_statistic_info(records) + # 安全检查:确保 index 字段存在 + if "index" not in record: + logger.warning(f"合并统计信息缺少 index 字段,用户: {user}, 日期: {date}") + record["index"] = [] record["index"] = [HistoryIndexItem(**_) for _ in record["index"]] record = HistoryData(**record) data[date][user] = record diff --git a/app/core/config.py b/app/core/config.py index c7c1857..b36de0c 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -2272,7 +2272,12 @@ class AppConfig(GlobalConfig): logger.success(f"统计信息合并完成, 共计 {len(data['index'])} 条记录") - return {k: v for k, v in data.items() if v} + # 确保返回的字典始终包含 index 字段,即使为空 + result = {k: v for k, v in data.items() if v} + if "index" not in result: + result["index"] = [] + + return result async def search_history(self, mode: str, start_date: date, end_date: date) -> dict: """