web.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import asyncio
  2. import json
  3. import os
  4. from datetime import time, datetime
  5. from flask import Flask, request, jsonify, send_from_directory, send_file
  6. app = Flask(__name__)
  7. cur_dir = os.getcwd()
  8. @app.route('/sysvent/draw/<int:template_id>', methods=['POST'])
  9. def post_json(template_id):
  10. # 检查请求是否包含JSON
  11. if not request.is_json:
  12. return jsonify({"error": "缺少JSON数据"}), 400
  13. # 从数据库中查找template
  14. # template = select_template_by_id(template_id) template = 'template/moban.dxf'
  15. data = request.json
  16. now = datetime.now()
  17. current_time_str = now.strftime("%Y_%m_%d_%H_%M_%S")
  18. json_file_name = f'{cur_dir}\\save\\{current_time_str}.json'
  19. dxf_file_name = f'{cur_dir}\\save\\{current_time_str}.DXF'
  20. with open(json_file_name, 'w', encoding='utf-8') as f:
  21. json.dump(data, f, ensure_ascii=False, indent=4)
  22. # asyncio.run(async_write_dxf_file(templateFile=template, node_list=data, save_name=dxf_file_name))
  23. res = {
  24. "code": 200,
  25. "msg": "success",
  26. 'data': {
  27. 'filename': f'{current_time_str}.DXF'
  28. }
  29. }
  30. return jsonify(res), 200
  31. @app.route('/sysvent/download/<filename>', methods=['GET'])
  32. def download_dxf_file(filename):
  33. # 指定文件的完整路径
  34. file_path = f'save\\{filename}'
  35. # 使用send_file发送文件,as_attachment=True表示以附件形式发送
  36. return send_file(file_path, as_attachment=True)
  37. if __name__ == '__main__':
  38. app.run(debug=True)