routes.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. from flask import jsonify, request, make_response
  2. from app.api.data_parse import bp
  3. from app.core.data_parse.parse import parse_data, process_business_card, update_business_card, get_business_cards, update_business_card_status, get_minio_client, MINIO_BUCKET
  4. import logging
  5. # 测试用的解析数据接口。没有实际使用。
  6. @bp.route('/parse', methods=['POST'])
  7. def parse():
  8. """
  9. 解析数据的接口
  10. """
  11. try:
  12. data = request.get_json()
  13. if not data:
  14. return jsonify({'error': 'No data provided'}), 400
  15. result = parse_data(data)
  16. return jsonify(result), 200
  17. except Exception as e:
  18. return jsonify({'error': str(e)}), 500
  19. # 名片解析接口
  20. @bp.route('/business-card-parse', methods=['POST'])
  21. def parse_business_card_route():
  22. """
  23. 处理名片图片并提取信息的API接口
  24. 请求参数:
  25. - image: 名片图片文件 (multipart/form-data)
  26. 返回:
  27. - JSON: 包含提取的名片信息和处理状态
  28. """
  29. # 检查是否上传了文件
  30. if 'image' not in request.files:
  31. return jsonify({
  32. 'success': False,
  33. 'message': '未上传图片',
  34. 'data': None
  35. }), 400
  36. image_file = request.files['image']
  37. # 检查文件是否为空
  38. if image_file.filename == '':
  39. return jsonify({
  40. 'success': False,
  41. 'message': '未选择文件',
  42. 'data': None
  43. }), 400
  44. # 检查文件类型是否为图片
  45. if not image_file.content_type.startswith('image/'):
  46. return jsonify({
  47. 'success': False,
  48. 'message': '上传的文件不是图片',
  49. 'data': None
  50. }), 400
  51. # 处理名片图片
  52. result = process_business_card(image_file)
  53. if result['success']:
  54. return jsonify(result), 200
  55. else:
  56. return jsonify(result), 500
  57. # 更新名片信息接口
  58. @bp.route('/business-cards/<int:card_id>', methods=['PUT'])
  59. def update_business_card_route(card_id):
  60. """
  61. 更新名片信息的API接口
  62. 路径参数:
  63. - card_id: 名片记录ID
  64. 请求参数:
  65. - JSON格式的名片信息
  66. 返回:
  67. - JSON: 包含更新后的名片信息和处理状态
  68. """
  69. # 获取请求数据
  70. data = request.json
  71. if not data:
  72. return jsonify({
  73. 'success': False,
  74. 'message': '请求数据为空',
  75. 'data': None
  76. }), 400
  77. # 调用业务逻辑函数处理更新
  78. result = update_business_card(card_id, data)
  79. # 根据处理结果设置HTTP状态码
  80. status_code = 200 if result['success'] else 500
  81. if 'not found' in result.get('message', '').lower() or '未找到' in result.get('message', ''):
  82. status_code = 404
  83. return jsonify(result), status_code
  84. # 获取所有名片记录的API接口
  85. @bp.route('/get-business-cards', methods=['GET'])
  86. def get_business_cards_route():
  87. """
  88. 获取所有名片记录的API接口
  89. 返回:
  90. - JSON: 包含名片记录列表和处理状态
  91. """
  92. # 调用业务逻辑函数获取名片列表
  93. result = get_business_cards()
  94. # 根据处理结果设置HTTP状态码
  95. status_code = 200 if result['success'] else 500
  96. return jsonify(result), status_code
  97. @bp.route('/update-business-cards/<int:card_id>/status', methods=['PUT'])
  98. def update_business_card_status_route(card_id):
  99. """
  100. 更新名片状态的API接口
  101. 路径参数:
  102. - card_id: 名片记录ID
  103. 请求参数:
  104. - JSON格式,包含status字段
  105. 返回:
  106. - JSON: 包含更新后的名片信息和处理状态
  107. """
  108. # 获取请求数据
  109. data = request.json
  110. if not data or 'status' not in data:
  111. return jsonify({
  112. 'success': False,
  113. 'message': '请求数据为空或缺少status字段',
  114. 'data': None
  115. }), 400
  116. status = data['status']
  117. # 调用业务逻辑函数处理状态更新
  118. result = update_business_card_status(card_id, status)
  119. # 根据处理结果设置HTTP状态码
  120. status_code = 200 if result['success'] else 500
  121. if 'not found' in result.get('message', '').lower() or '未找到' in result.get('message', ''):
  122. status_code = 404
  123. return jsonify(result), status_code
  124. # 从MinIO获取名片图片的API接口
  125. @bp.route('/business-cards/image/<path:image_path>', methods=['GET'])
  126. def get_business_card_image(image_path):
  127. """
  128. 从MinIO获取名片图片的API接口
  129. 路径参数:
  130. - image_path: MinIO中的图片路径
  131. 返回:
  132. - 图片数据流
  133. """
  134. try:
  135. # 获取MinIO客户端
  136. minio_client = get_minio_client()
  137. if not minio_client:
  138. return jsonify({
  139. 'success': False,
  140. 'message': 'MinIO客户端初始化失败',
  141. }), 500
  142. # 从MinIO获取图片
  143. response = minio_client.get_object(
  144. Bucket=MINIO_BUCKET,
  145. Key=image_path
  146. )
  147. # 读取图片数据
  148. image_data = response['Body'].read()
  149. # 确定内容类型
  150. content_type = response.get('ContentType', 'image/jpeg')
  151. # 创建响应
  152. response = make_response(image_data)
  153. response.headers.set('Content-Type', content_type)
  154. # 设置缓存,提高性能
  155. response.headers.set('Cache-Control', 'public, max-age=31536000')
  156. return response
  157. except Exception as e:
  158. logging.error(f"获取图片失败: {str(e)}")
  159. return jsonify({
  160. 'success': False,
  161. 'message': f'获取图片失败: {str(e)}',
  162. }), 404