diff --git a/WeatherAPI_Integration_Summary.md b/WeatherAPI_Integration_Summary.md deleted file mode 100644 index 9a2b8b9..0000000 --- a/WeatherAPI_Integration_Summary.md +++ /dev/null @@ -1,115 +0,0 @@ -# WeatherAPI 集成总结 - -## 集成状态 -✅ **成功完成** - WeatherAPI类已成功集成到word_main_window.py文件中 - -## 集成功能概览 - -### 1. 核心集成组件 -- **WeatherAPI实例**: 在WordStyleMainWindow类中创建了WeatherAPI实例 -- **WeatherFetchThread线程**: 使用WeatherAPI获取实时天气数据 -- **状态栏显示**: 在状态栏中显示当前天气信息 - -### 2. 新增功能 - -#### 2.1 天气数据获取 -- **自动获取**: WeatherFetchThread线程自动获取北京天气数据 -- **数据格式化**: 将原始天气数据格式化为用户友好的格式 -- **错误处理**: 包含完整的异常处理机制 - -#### 2.2 用户界面功能 -- **状态栏显示**: 在状态栏右侧显示当前温度、天气描述、湿度和风力 -- **菜单集成**: 在"视图"菜单中添加"天气信息"子菜单 -- **快捷键支持**: F5快捷键刷新天气信息 - -#### 2.3 详细天气信息 -- **对话框显示**: 点击"显示详细天气"打开详细天气信息对话框 -- **实时刷新**: 对话框内可手动刷新天气数据 -- **预报信息**: 显示未来3天的天气预报 - -### 3. 技术实现细节 - -#### 3.1 类结构修改 -```python -class WordStyleMainWindow(QMainWindow): - def __init__(self): - # ... 其他初始化代码 - self.weather_api = WeatherAPI() # 新增WeatherAPI实例 - # ... -``` - -#### 3.2 线程实现 -```python -class WeatherFetchThread(QThread): - def __init__(self): - super().__init__() - self.weather_api = WeatherAPI() # 使用WeatherAPI替代NetworkService - - def run(self): - # 使用WeatherAPI获取天气数据 - weather_data = self.weather_api.get_weather_data("北京") - # 格式化并发送信号 -``` - -#### 3.3 菜单集成 -```python -def create_menu_bar(self): - # 在视图菜单中添加天气信息子菜单 - weather_submenu = view_menu.addMenu('天气信息') - - # 刷新天气菜单项 - refresh_weather_action = QAction('刷新天气', self) - refresh_weather_action.setShortcut('F5') - refresh_weather_action.triggered.connect(self.refresh_weather) - - # 显示详细天气菜单项 - show_weather_action = QAction('显示详细天气', self) - show_weather_action.triggered.connect(self.show_detailed_weather) -``` - -## 测试验证 - -### 测试结果 -- ✅ 导入测试: WeatherAPI类导入成功 -- ✅ 方法存在性测试: 所有相关类和方法存在 -- ✅ 功能完整性测试: WeatherAPI功能完整可用 - -### 测试覆盖率 -- WeatherAPI实例创建和初始化 -- 天气数据获取和格式化 -- 用户界面集成 -- 错误处理机制 - -## 使用说明 - -### 基本使用 -1. 启动应用程序 -2. 天气信息自动显示在状态栏右侧 -3. 使用F5快捷键或菜单刷新天气 -4. 点击"显示详细天气"查看详细信息 - -### 功能特点 -- **实时更新**: 天气数据自动更新 -- **用户友好**: 简洁的界面和操作 -- **错误处理**: 网络异常时显示友好提示 -- **扩展性**: 支持未来添加更多城市 - -## 技术优势 - -1. **模块化设计**: WeatherAPI独立封装,便于维护 -2. **线程安全**: 使用QThread避免界面卡顿 -3. **信号机制**: 使用pyqtSignal进行线程间通信 -4. **错误恢复**: 完善的异常处理机制 - -## 未来扩展建议 - -1. **多城市支持**: 添加城市选择功能 -2. **天气预警**: 集成天气预警信息 -3. **主题适配**: 根据天气调整界面主题 -4. **数据缓存**: 添加天气数据缓存机制 - ---- - -**集成完成时间**: 2024年 -**测试状态**: 全部通过 -**代码质量**: 优秀 \ No newline at end of file diff --git a/src/ui/word_style_ui.py b/src/ui/word_style_ui.py index 39a4b9f..a96f0f9 100644 --- a/src/ui/word_style_ui.py +++ b/src/ui/word_style_ui.py @@ -758,4 +758,46 @@ class WeatherAPI: if weather_data: results[city] = weather_data return results - \ No newline at end of file +class daily_sentence_API: + def __init__(self, api_url): + self.api_url = api_url or "https://api.nxvav.cn/api/yiyan" + + def get_sentence(self, format_type='json'): + """ + 获取每日一言数据。 + + 参数: + format_type (str): 指定返回格式,可选值: 'json', 'js', 'js1', 'text'。默认为 'json'。 + + 返回: + 如果 format_type 为 'json',返回解析后的字典(包含 id, yiyan, createTime, nick)。 + 如果 format_type 为 'js', 'js1', 或 'text',返回字符串形式的响应内容。 + 如果请求失败或格式不支持,返回 None。 + """ + # 定义查询参数字典,根据 format_type 添加对应参数 + params = {} + if format_type == 'json': + params['json'] = 'true' + elif format_type == 'js': + params['js'] = 'true' + elif format_type == 'js1': + params['js1'] = 'true' + elif format_type == 'text': + params['text'] = 'true' + else: + print(f"错误:不支持的格式类型 '{format_type}',使用默认 'json'。") + params['json'] = 'true' # 默认为 JSON + + try: + # 发送 GET 请求 + response = requests.get(self.api_url, params=params) + response.raise_for_status() # 如果请求失败(如 4xx 或 5xx),抛出异常 + + # 根据格式类型处理响应 + if format_type == 'json': + return response.json() # 解析为字典 + else: + return response.text # 返回文本内容 + except requests.exceptions.RequestException as e: + print(f"请求失败: {e}") + return None