52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
import os
|
|
import uuid
|
|
from flask import Flask, request, jsonify, send_file
|
|
from flask_cors import CORS
|
|
from services.report_service import NutritionReportService
|
|
|
|
app = Flask(__name__)
|
|
|
|
CORS(app, resources={
|
|
r"/api/*": {
|
|
"origins": ["http://localhost:5173", "http://127.0.0.1:5173"]
|
|
}
|
|
})
|
|
|
|
report_service = NutritionReportService()
|
|
|
|
|
|
@app.route("/api/food-record", methods=["POST"])
|
|
def food_record():
|
|
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}.pdf"
|
|
output_path = os.path.join(output_dir, file_name)
|
|
|
|
report_service.generate_report_from_json(front_json, output_path)
|
|
|
|
return send_file(
|
|
output_path,
|
|
as_attachment=True,
|
|
download_name="nutrition_report.pdf",
|
|
mimetype="application/pdf"
|
|
)
|
|
|
|
except Exception as e:
|
|
print("后端报错:", e)
|
|
return jsonify({
|
|
"message": "处理失败",
|
|
"error": str(e)
|
|
}), 500
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=True, port=5000) |