109 lines
3.8 KiB
Python
109 lines
3.8 KiB
Python
|
|
from flask import request, jsonify, current_app
|
||
|
|
from app import db
|
||
|
|
from app.models import AuditLog
|
||
|
|
from . import api_bp
|
||
|
|
from .decorators import require_admin
|
||
|
|
from datetime import datetime, timedelta
|
||
|
|
import os
|
||
|
|
|
||
|
|
@api_bp.route('/logs', methods=['GET'])
|
||
|
|
@require_admin
|
||
|
|
def get_logs():
|
||
|
|
"""获取操作日志列表"""
|
||
|
|
try:
|
||
|
|
page = request.args.get('page', 1, type=int)
|
||
|
|
per_page = min(request.args.get('per_page', 20, type=int), 100)
|
||
|
|
action = request.args.get('action')
|
||
|
|
target_type = request.args.get('target_type')
|
||
|
|
admin_id = request.args.get('admin_id', type=int)
|
||
|
|
start_date = request.args.get('start_date')
|
||
|
|
end_date = request.args.get('end_date')
|
||
|
|
|
||
|
|
query = AuditLog.query
|
||
|
|
|
||
|
|
# 添加筛选条件
|
||
|
|
if action:
|
||
|
|
query = query.filter(AuditLog.action == action)
|
||
|
|
if target_type:
|
||
|
|
query = query.filter(AuditLog.target_type == target_type)
|
||
|
|
if admin_id:
|
||
|
|
query = query.filter(AuditLog.admin_id == admin_id)
|
||
|
|
if start_date:
|
||
|
|
start_datetime = datetime.strptime(start_date, '%Y-%m-%d')
|
||
|
|
query = query.filter(AuditLog.create_time >= start_datetime)
|
||
|
|
if end_date:
|
||
|
|
end_datetime = datetime.strptime(end_date, '%Y-%m-%d') + timedelta(days=1)
|
||
|
|
query = query.filter(AuditLog.create_time < end_datetime)
|
||
|
|
|
||
|
|
# 按时间倒序排列
|
||
|
|
query = query.order_by(AuditLog.create_time.desc())
|
||
|
|
|
||
|
|
# 分页
|
||
|
|
pagination = query.paginate(page=page, per_page=per_page, error_out=False)
|
||
|
|
|
||
|
|
logs = [log.to_dict() for log in pagination.items]
|
||
|
|
|
||
|
|
return jsonify({
|
||
|
|
'success': True,
|
||
|
|
'data': {
|
||
|
|
'logs': logs,
|
||
|
|
'pagination': {
|
||
|
|
'page': page,
|
||
|
|
'per_page': per_page,
|
||
|
|
'total': pagination.total,
|
||
|
|
'pages': pagination.pages,
|
||
|
|
'has_prev': pagination.has_prev,
|
||
|
|
'has_next': pagination.has_next
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
except Exception as e:
|
||
|
|
current_app.logger.error(f"获取操作日志列表失败: {str(e)}")
|
||
|
|
return jsonify({'success': False, 'message': '服务器内部错误'}), 500
|
||
|
|
|
||
|
|
@api_bp.route('/logs/actions', methods=['GET'])
|
||
|
|
@require_admin
|
||
|
|
def get_log_actions():
|
||
|
|
"""获取所有操作类型"""
|
||
|
|
try:
|
||
|
|
actions = db.session.query(AuditLog.action).distinct().all()
|
||
|
|
action_list = [action[0] for action in actions]
|
||
|
|
return jsonify({
|
||
|
|
'success': True,
|
||
|
|
'data': {
|
||
|
|
'actions': action_list
|
||
|
|
}
|
||
|
|
})
|
||
|
|
except Exception as e:
|
||
|
|
current_app.logger.error(f"获取操作类型列表失败: {str(e)}")
|
||
|
|
return jsonify({'success': False, 'message': '服务器内部错误'}), 500
|
||
|
|
|
||
|
|
@api_bp.route('/logs/file', methods=['GET'])
|
||
|
|
@require_admin
|
||
|
|
def get_log_file():
|
||
|
|
"""获取系统日志文件内容"""
|
||
|
|
try:
|
||
|
|
log_file_path = 'logs/kamaxitong.log'
|
||
|
|
|
||
|
|
# 检查日志文件是否存在
|
||
|
|
if not os.path.exists(log_file_path):
|
||
|
|
return jsonify({
|
||
|
|
'success': False,
|
||
|
|
'message': '日志文件不存在'
|
||
|
|
}), 404
|
||
|
|
|
||
|
|
# 读取日志文件内容
|
||
|
|
with open(log_file_path, 'r', encoding='utf-8') as f:
|
||
|
|
# 读取最后1000行日志
|
||
|
|
lines = f.readlines()[-1000:]
|
||
|
|
log_content = ''.join(lines)
|
||
|
|
|
||
|
|
return jsonify({
|
||
|
|
'success': True,
|
||
|
|
'data': {
|
||
|
|
'content': log_content
|
||
|
|
}
|
||
|
|
})
|
||
|
|
except Exception as e:
|
||
|
|
current_app.logger.error(f"读取日志文件失败: {str(e)}")
|
||
|
|
return jsonify({'success': False, 'message': '服务器内部错误'}), 500
|