v2 4.9 该版本集成了Excel导出功能
This commit is contained in:
1 parent
e5030abf39
commit
86567eece5
16 files changed
+2516
-30
No files matched your search
@@ -15,8 +15,8 @@ CORS(app, resources={
|
||||
report_service = NutritionReportService()
|
||||
|
||||
|
||||
@app.route("/api/food-record", methods=["POST"])
|
||||
def food_record():
|
||||
@app.route("/api/generate-pdf", methods=["POST"])
|
||||
def generate_pdf():
|
||||
try:
|
||||
front_json = request.get_json()
|
||||
|
||||
@@ -31,7 +31,7 @@ def food_record():
|
||||
file_name = f"nutrition_report_{uuid.uuid4().hex}.pdf"
|
||||
output_path = os.path.join(output_dir, file_name)
|
||||
|
||||
report_service.generate_report_from_json(front_json, output_path)
|
||||
report_service.generate_pdf_from_json(front_json, output_path)
|
||||
|
||||
return send_file(
|
||||
output_path,
|
||||
@@ -48,5 +48,38 @@ def food_record():
|
||||
}), 500
|
||||
|
||||
|
||||
@app.route("/api/generate-excel", methods=["POST"])
|
||||
def generate_excel():
|
||||
try:
|
||||
front_json = request.get_json()
|
||||
|
||||
if not front_json:
|
||||
return jsonify({"message": "没有收到 JSON 数据"}), 400
|
||||
|
||||
print("收到前端数据:", front_json)
|
||||
|
||||
output_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "output", "reports")
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
file_name = f"nutrition_report_{uuid.uuid4().hex}.xlsx"
|
||||
output_path = os.path.join(output_dir, file_name)
|
||||
|
||||
report_service.generate_excel_from_json(front_json, output_path)
|
||||
|
||||
return send_file(
|
||||
output_path,
|
||||
as_attachment=True,
|
||||
download_name="nutrition_report.xlsx",
|
||||
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print("后端报错:", e)
|
||||
return jsonify({
|
||||
"message": "处理失败",
|
||||
"error": str(e)
|
||||
}), 500
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True, port=5000)
|
||||
Reference in new issue
Block a user