|
@@ -0,0 +1,84 @@
|
|
|
+from flask import Blueprint, request, jsonify,render_template_string
|
|
|
+from models import db, Activity
|
|
|
+from nlp_utils import classify_activity_type
|
|
|
+from flask import Blueprint, request, jsonify
|
|
|
+from models import db, User
|
|
|
+from ocr_utils import extract_text_from_image
|
|
|
+from nlp_utils import extract_information
|
|
|
+import os
|
|
|
+activity_bp = Blueprint('activity', __name__)
|
|
|
+user_bp = Blueprint('user', __name__)
|
|
|
+
|
|
|
+@activity_bp.route('/activities', methods=['POST'])
|
|
|
+def create_activity():
|
|
|
+ data = request.json
|
|
|
+ title = data.get('title')
|
|
|
+ description = data.get('description')
|
|
|
+ date = data.get('date')
|
|
|
+ location = data.get('location')
|
|
|
+
|
|
|
+ # 使用NLP工具进行活动类型分类
|
|
|
+ activity_type = classify_activity_type(description)
|
|
|
+
|
|
|
+ new_activity = Activity(title=title, description=description, type=activity_type, date=date, location=location)
|
|
|
+ db.session.add(new_activity)
|
|
|
+ db.session.commit()
|
|
|
+
|
|
|
+ return jsonify({'message': 'Activity created successfully', 'type': activity_type}), 201
|
|
|
+
|
|
|
+@activity_bp.route('/activities', methods=['GET'])
|
|
|
+def get_activities():
|
|
|
+ activities = Activity.query.all()
|
|
|
+ result = []
|
|
|
+ for activity in activities:
|
|
|
+ result.append({
|
|
|
+ 'id': activity.id,
|
|
|
+ 'title': activity.title,
|
|
|
+ 'description': activity.description,
|
|
|
+ 'type': activity.type,
|
|
|
+ 'date': activity.date.strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
+ 'location': activity.location
|
|
|
+ })
|
|
|
+ return jsonify(result), 200
|
|
|
+
|
|
|
+@user_bp.route('/register', methods = ['GET'])
|
|
|
+def index():
|
|
|
+ return render_template_string(open('static/register.html').read())
|
|
|
+
|
|
|
+@user_bp.route('/register', methods=['POST'])
|
|
|
+def register_user():
|
|
|
+ data = request.form
|
|
|
+ image_file = request.files.get('id_card_image')
|
|
|
+
|
|
|
+ if not image_file:
|
|
|
+ return jsonify({'error': '请上传身份证图片'}), 400
|
|
|
+
|
|
|
+ # 保存上传的图片到临时目录
|
|
|
+ image_path = os.path.join('/tmp', image_file.filename)
|
|
|
+ image_file.save(image_path)
|
|
|
+
|
|
|
+ try:
|
|
|
+ # 提取文本
|
|
|
+ extracted_text = extract_text_from_image(image_path)
|
|
|
+
|
|
|
+ # 抽取姓名和身份证号
|
|
|
+ user_info = extract_information(extracted_text)
|
|
|
+
|
|
|
+ # 创建新用户
|
|
|
+ new_user = User(
|
|
|
+ username=data.get('username'),
|
|
|
+ email=data.get('email'),
|
|
|
+ name=user_info.get('name'),
|
|
|
+ id_number=user_info.get('id_number')
|
|
|
+ )
|
|
|
+ db.session.add(new_user)
|
|
|
+ db.session.commit()
|
|
|
+
|
|
|
+ return jsonify({'message': '注册成功', 'name': user_info.get('name'), 'id_number': user_info.get('id_number')}), 201
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ return jsonify({'error': str(e)}), 500
|
|
|
+ finally:
|
|
|
+ # 清理临时文件
|
|
|
+ if os.path.exists(image_path):
|
|
|
+ os.remove(image_path)
|