from flask import Flask, render_template, request, jsonify, send_file from services import RollCallService, ScoreService import io app = Flask(__name__) roll_call_service = RollCallService() score_service = ScoreService() @app.route('/') def index(): """主页面""" return render_template('index.html') @app.route('/roll_call') def roll_call_page(): """点名页面""" return render_template('roll_call.html') @app.route('/scores') def scores_page(): """积分页面""" return render_template('scores.html') # API接口 @app.route('/api/roll_call/random', methods=['POST']) def random_roll_call(): """随机点名API""" class_name = request.json.get('class_name', '软工K班') student = roll_call_service.random_roll_call(class_name) if student: return jsonify({ 'success': True, 'data': student.to_dict() }) else: return jsonify({ 'success': False, 'message': '没有找到学生' }) @app.route('/api/scores/update', methods=['POST']) def update_score(): """更新积分API""" data = request.json student_id = data.get('student_id') answer_type = data.get('answer_type') performance = data.get('performance') success, score_delta = roll_call_service.update_student_score( student_id, answer_type, performance ) return jsonify({ 'success': success, 'score_delta': score_delta }) @app.route('/api/scores/ranking') def get_ranking(): """获取积分排名API""" class_name = request.args.get('class_name', '软工K班') ranking = score_service.get_class_ranking(class_name) return jsonify({ 'success': True, 'data': [student.to_dict() for student in ranking] }) @app.route('/api/scores/export') def export_scores(): """导出积分Excel API""" class_name = request.args.get('class_name', '软工K班') excel_data = score_service.export_scores_excel(class_name) return send_file( excel_data, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', as_attachment=True, download_name=f'{class_name}_积分统计.xlsx' ) @app.route('/api/roll_call/history') def get_roll_call_history(): """获取点名历史API""" db = roll_call_service.db recent_records = db.get_recent_records(10) return jsonify({ 'success': True, 'data': recent_records }) if __name__ == '__main__': app.run(debug=True, port=5000)