debug: 创建简单测试脚本和最终诊断指南

为了准确定位"下载按钮不显示"的问题,创建了:

1. test_simple.py - 简单测试脚本:
   - 自动获取配置并生成日报
   - 查看历史记录
   - 测试下载功能
   - 显示详细的请求和响应数据

2. 最终诊断指南.md:
   - 5个诊断步骤
   - 常见问题快速诊断
   - 需要提供的信息清单
   - 快速修复方案

使用步骤:
1. 运行 python test_simple.py
2. 查看完整输出
3. 检查服务器日志
4. 检查浏览器控制台
5. 提供所有信息以便准确定位问题

Coze-Commit-Type: user
Coze-User-ID: 3722323274763196
Coze-Conversation-ID: 9894087
This commit is contained in:
user9994793890
2026-07-09 13:18:27 +08:00
parent 487a26e704
commit 3a06663f58
2 changed files with 372 additions and 0 deletions

181
test_simple.py Normal file
View File

@@ -0,0 +1,181 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简单测试脚本:测试日报生成和下载
"""
import requests
import json
BASE_URL = "http://localhost:5000"
def test_generate():
"""测试生成日报"""
print("="*50)
print("测试1生成日报")
print("="*50)
# 先获取配置列表
print("\n1. 获取配置列表...")
response = requests.get(f"{BASE_URL}/api/config")
print(f" 状态码: {response.status_code}")
if response.status_code != 200:
print(f" ❌ 获取配置失败: {response.text}")
return
configs = response.json().get('data', [])
print(f" ✓ 找到 {len(configs)} 个配置")
if not configs:
print(" ❌ 没有配置,请先创建配置")
return
# 使用第一个配置
config = configs[0]
config_id = config['id']
config_name = config['config_name']
print(f" 使用配置: {config_name} (ID: {config_id})")
# 生成日报
print("\n2. 生成日报...")
payload = {
'config_id': config_id,
'start_time': '2025-01-07 08:00:00',
'end_time': '2025-01-08 08:00:00'
}
print(f" 请求数据: {json.dumps(payload, ensure_ascii=False)}")
response = requests.post(f"{BASE_URL}/api/report/generate", json=payload)
print(f" 状态码: {response.status_code}")
print(f" 响应: {json.dumps(response.json(), ensure_ascii=False, indent=2)}")
if response.status_code != 200:
print(f" ❌ 生成失败")
return
result = response.json()
if not result.get('success'):
print(f" ❌ 生成失败: {result.get('message')}")
return
print(f" ✓ 生成成功")
print(f" 订单数: {result.get('total_orders')}")
print(f" 总金额: {result.get('total_amount')}")
print(f" 文件路径: {result.get('file_path')}")
return result
def test_history():
"""测试历史记录"""
print("\n" + "="*50)
print("测试2查看历史记录")
print("="*50)
response = requests.get(f"{BASE_URL}/api/report/history")
print(f"状态码: {response.status_code}")
if response.status_code != 200:
print(f"❌ 获取历史记录失败: {response.text}")
return
result = response.json()
if not result.get('success'):
print(f"❌ 获取失败: {result.get('message')}")
return
history = result.get('data', [])
print(f"✓ 找到 {len(history)} 条记录")
if not history:
print("❌ 没有历史记录")
return
print("\n最近的记录:")
for item in history[:3]:
print(f" ID: {item['id']}")
print(f" 状态: {'成功' if item.get('status') == 1 else '失败'}")
print(f" file_path: {item.get('file_path', '')}")
print(f" 日期: {item.get('report_date')}")
print(f" 名称: {item.get('split_name')}")
print()
# 检查是否有可下载的文件
has_file = any(item.get('file_path') for item in history)
if has_file:
print("✓ 有可下载的文件")
else:
print("❌ 没有可下载的文件所有记录的file_path都为空")
def test_download():
"""测试下载"""
print("\n" + "="*50)
print("测试3测试下载")
print("="*50)
# 获取历史记录
response = requests.get(f"{BASE_URL}/api/report/history")
if response.status_code != 200:
print("❌ 获取历史记录失败")
return
history = response.json().get('data', [])
if not history:
print("❌ 没有历史记录")
return
# 找到有文件的记录
file_record = None
for item in history:
if item.get('file_path'):
file_record = item
break
if not file_record:
print("❌ 没有找到有文件的记录")
return
file_path = file_record['file_path']
print(f"找到文件: {file_path}")
# 测试下载
print(f"\n测试下载: {file_path}")
response = requests.get(f"{BASE_URL}/api/download", params={'path': file_path})
print(f"状态码: {response.status_code}")
if response.status_code == 200:
print(f"✓ 下载成功")
print(f"文件大小: {len(response.content)} 字节")
else:
print(f"❌ 下载失败: {response.text}")
def main():
print("\n" + "="*50)
print("日报系统测试工具")
print("="*50)
try:
# 测试生成
result = test_generate()
# 测试历史
test_history()
# 测试下载
test_download()
print("\n" + "="*50)
print("测试完成")
print("="*50)
except requests.exceptions.ConnectionError:
print("\n❌ 无法连接到服务器")
print("请确保服务正在运行: python app.py")
except Exception as e:
print(f"\n❌ 测试出错: {e}")
print("\n按回车键退出...")
input()
if __name__ == '__main__':
main()

191
最终诊断指南.md Normal file
View File

@@ -0,0 +1,191 @@
# 最终诊断指南
## 问题:日报生成成功但没有下载按钮
## 请按以下步骤操作
### 步骤1运行简单测试脚本
```bash
python test_simple.py
```
这个脚本会:
1. 自动获取配置列表
2. 使用第一个配置生成日报
3. 查看历史记录
4. 测试下载功能
**请把完整的输出结果告诉我**,包括:
- 每个测试的状态码
- 响应数据
- file_path 的值
### 步骤2检查服务器日志
在运行 `python app.py` 的窗口中,查看生成日报时的日志。
**请复制完整的日志输出**,应该包含:
```
[日报生成] 开始生成日报
配置ID: xxx
配置名称: xxx
...
[日报生成] Excel文件已生成: /reports/xxx.xlsx
[日报生成] 准备保存历史记录file_path=/reports/xxx.xlsx
[保存历史] 准备保存历史记录
ID: xxx
file_path: '/reports/xxx.xlsx'
status: 1
...
```
### 步骤3检查浏览器控制台
1. 打开 http://localhost:5000
2.**F12** 打开开发者工具
3. 切换到 **Console** 标签
4. 切换到"历史记录"标签
5. **复制控制台中的所有输出**
应该看到类似:
```
历史记录项: {id: xxx, status: 1, file_path: '/reports/xxx.xlsx', ...}
status: 1 类型: number
file_path: /reports/xxx.xlsx 类型: string
是否有文件: true
```
### 步骤4直接测试API
打开命令提示符,运行:
```bash
curl http://localhost:5000/api/report/history
```
或者在浏览器中访问:
```
http://localhost:5000/api/report/history
```
**请把返回的JSON数据复制给我**,特别关注 `file_path` 字段。
### 步骤5检查文件是否存在
```bash
# Windows
dir public\reports\*.xlsx
# 或者在文件资源管理器中打开
# 项目目录\public\reports\
```
**请告诉我**
- 是否有 .xlsx 文件?
- 文件名是什么?
- 文件大小是多少?
## 常见问题快速诊断
### 问题1file_path 为空
如果测试脚本或API返回的 `file_path` 为空字符串或null
**原因**Excel文件没有正确生成或路径没有返回
**解决**
1. 查看服务器日志,看是否有错误
2. 检查 `public/reports/` 目录是否存在
3. 检查是否有写入权限
### 问题2file_path 有值,但下载按钮不显示
如果API返回的 `file_path` 有值,但前端没有显示下载按钮:
**原因**前端JavaScript判断逻辑有问题
**解决**
1. 查看浏览器控制台的调试信息
2. 检查 `file_path` 的类型和值
3. 清除浏览器缓存Ctrl+F5
### 问题3下载按钮显示但下载失败
如果能点击按钮但下载失败:
**原因**:文件不存在或路径错误
**解决**
1. 检查 `public/reports/` 目录中是否有文件
2. 检查文件路径是否正确
3. 查看服务器日志
## 请提供以下信息
为了准确定位问题,请提供:
1. **test_simple.py 的完整输出**
2. **服务器控制台的日志**(从生成到查看历史)
3. **浏览器控制台的输出**F12 → Console
4. **API返回的JSON数据**curl 或浏览器访问)
5. **public/reports/ 目录的文件列表**
6. **截图**:历史记录页面的截图
## 快速修复方案
如果以上步骤都无法解决,尝试:
### 方案1完全重置
```bash
# 1. 停止服务
# Ctrl+C
# 2. 删除所有数据(谨慎!)
python fixDatabase.py
# 输入 y 确认
# 3. 重启服务
python app.py
# 4. 重新创建配置
# 访问 http://localhost:5000
# 5. 生成日报
# 6. 查看历史记录
```
### 方案2检查代码版本
确保你的代码是最新的,特别是:
- `lib/report_generator.py`
- `app.py`
- `templates/index.html`
### 方案3手动检查数据库
如果有MySQL客户端
```bash
mysql -h haoslm2.xicp.net -P 10216 -u root -p
# 密码DsideaL147258369
# 查看表结构
DESCRIBE t_daily_report_history;
# 查看数据
SELECT id, status, file_path, report_date, split_name
FROM t_daily_report_history
ORDER BY create_time DESC
LIMIT 10;
```
## 联系支持
如果所有方案都失败,请提供上述所有信息,我会帮你进一步诊断。
---
**最后更新**: 2025-01-08