from flask import jsonify, request, make_response from app.api.data_parse import bp 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 import logging # 测试用的解析数据接口。没有实际使用。 @bp.route('/parse', methods=['POST']) def parse(): """ 解析数据的接口 """ try: data = request.get_json() if not data: return jsonify({'error': 'No data provided'}), 400 result = parse_data(data) return jsonify(result), 200 except Exception as e: return jsonify({'error': str(e)}), 500 # 名片解析接口 @bp.route('/business-card-parse', methods=['POST']) def parse_business_card_route(): """ 处理名片图片并提取信息的API接口 请求参数: - image: 名片图片文件 (multipart/form-data) 返回: - JSON: 包含提取的名片信息和处理状态 """ # 检查是否上传了文件 if 'image' not in request.files: return jsonify({ 'success': False, 'message': '未上传图片', 'data': None }), 400 image_file = request.files['image'] # 检查文件是否为空 if image_file.filename == '': return jsonify({ 'success': False, 'message': '未选择文件', 'data': None }), 400 # 检查文件类型是否为图片 if not image_file.content_type.startswith('image/'): return jsonify({ 'success': False, 'message': '上传的文件不是图片', 'data': None }), 400 # 处理名片图片 result = process_business_card(image_file) if result['success']: return jsonify(result), 200 else: return jsonify(result), 500 # 更新名片信息接口 @bp.route('/business-cards/', methods=['PUT']) def update_business_card_route(card_id): """ 更新名片信息的API接口 路径参数: - card_id: 名片记录ID 请求参数: - JSON格式的名片信息 返回: - JSON: 包含更新后的名片信息和处理状态 """ # 获取请求数据 data = request.json if not data: return jsonify({ 'success': False, 'message': '请求数据为空', 'data': None }), 400 # 调用业务逻辑函数处理更新 result = update_business_card(card_id, data) # 根据处理结果设置HTTP状态码 status_code = 200 if result['success'] else 500 if 'not found' in result.get('message', '').lower() or '未找到' in result.get('message', ''): status_code = 404 return jsonify(result), status_code # 获取所有名片记录的API接口 @bp.route('/get-business-cards', methods=['GET']) def get_business_cards_route(): """ 获取所有名片记录的API接口 返回: - JSON: 包含名片记录列表和处理状态 """ # 调用业务逻辑函数获取名片列表 result = get_business_cards() # 根据处理结果设置HTTP状态码 status_code = 200 if result['success'] else 500 return jsonify(result), status_code @bp.route('/update-business-cards//status', methods=['PUT']) def update_business_card_status_route(card_id): """ 更新名片状态的API接口 路径参数: - card_id: 名片记录ID 请求参数: - JSON格式,包含status字段 返回: - JSON: 包含更新后的名片信息和处理状态 """ # 获取请求数据 data = request.json if not data or 'status' not in data: return jsonify({ 'success': False, 'message': '请求数据为空或缺少status字段', 'data': None }), 400 status = data['status'] # 调用业务逻辑函数处理状态更新 result = update_business_card_status(card_id, status) # 根据处理结果设置HTTP状态码 status_code = 200 if result['success'] else 500 if 'not found' in result.get('message', '').lower() or '未找到' in result.get('message', ''): status_code = 404 return jsonify(result), status_code # 从MinIO获取名片图片的API接口 @bp.route('/business-cards/image/', methods=['GET']) def get_business_card_image(image_path): """ 从MinIO获取名片图片的API接口 路径参数: - image_path: MinIO中的图片路径 返回: - 图片数据流 """ try: # 获取MinIO客户端 minio_client = get_minio_client() if not minio_client: return jsonify({ 'success': False, 'message': 'MinIO客户端初始化失败', }), 500 # 从MinIO获取图片 response = minio_client.get_object( Bucket=MINIO_BUCKET, Key=image_path ) # 读取图片数据 image_data = response['Body'].read() # 确定内容类型 content_type = response.get('ContentType', 'image/jpeg') # 创建响应 response = make_response(image_data) response.headers.set('Content-Type', content_type) # 设置缓存,提高性能 response.headers.set('Cache-Control', 'public, max-age=31536000') return response except Exception as e: logging.error(f"获取图片失败: {str(e)}") return jsonify({ 'success': False, 'message': f'获取图片失败: {str(e)}', }), 404