前后端更新

This commit is contained in:
super committed 2026-03-04 22:55:36 +08:00
1 parent a0c8721fbb
commit 24da604727
33 files changed
+1951 -580

No files matched your search

+100
View File
@@ -15,6 +15,31 @@ import io
admin_bp = Blueprint('admin', __name__)
@admin_bp.route('/dashboard', methods=['GET'])
@require_admin
def dashboard_stats():
"""
Dashboard statistics
"""
total_users = db.session.query(func.count(User.id)).scalar()
active_agents = db.session.query(func.count(Agent.id)).filter_by(is_active=True).scalar()
total_conversations = db.session.query(func.count(ChatLog.id)).scalar()
# Average rating
avg_score = db.session.query(func.avg(Feedback.score)).scalar()
avg_score = float(avg_score) if avg_score else 0.0
# Pending approvals
pending_approvals = db.session.query(func.count(User.id)).filter_by(status='pending').scalar()
return jsonify({
'total_users': total_users,
'active_agents': active_agents,
'total_conversations': total_conversations,
'avg_score': round(avg_score, 1),
'pending_approvals': pending_approvals
})
# --- Departments ---
@admin_bp.route('/departments', methods=['GET'])
@require_admin
@@ -140,6 +165,43 @@ def delete_agent(id):
return jsonify({'status': 'ok'})
@admin_bp.route('/agents/<int:id>/permissions', methods=['POST'])
@require_admin
def update_agent_permissions(id):
agent = db.session.get(Agent, id)
if not agent: return jsonify({'error': 'Not found'}), 404
data = request.get_json() or {}
user_ids = data.get('user_ids', [])
users = db.session.query(User).filter(User.id.in_(user_ids)).all()
# We need to update the relationship from the other side (User.agents)
# Strategy:
# 1. Find all users who currently have this agent
# 2. For users in user_ids but not in current list -> add agent
# 3. For users in current list but not in user_ids -> remove agent
# Current users with this agent
current_users = db.session.query(User).filter(User.agents.contains(agent)).all()
current_user_ids = set(u.id for u in current_users)
new_user_ids = set(user_ids)
# Add
to_add = new_user_ids - current_user_ids
if to_add:
users_to_add = db.session.query(User).filter(User.id.in_(to_add)).all()
for u in users_to_add:
u.agents.append(agent)
# Remove
to_remove = current_user_ids - new_user_ids
if to_remove:
users_to_remove = db.session.query(User).filter(User.id.in_(to_remove)).all()
for u in users_to_remove:
u.agents.remove(agent)
db.session.commit()
return jsonify({'status': 'ok'})
# --- Users ---
@admin_bp.route('/users', methods=['GET'])
@require_admin
@@ -222,6 +284,44 @@ def batch_delete_users():
db.session.commit()
return jsonify({'deleted_count': cnt})
@admin_bp.route('/permissions/batch', methods=['POST'])
@require_admin
def batch_update_permissions():
"""
Batch update user permissions
"""
data = request.get_json() or {}
user_ids = data.get('user_ids', [])
agent_ids = data.get('agent_ids', [])
action = data.get('action', 'add') # add, remove, set
if not user_ids:
return jsonify({'error': 'No users selected'}), 400
users = db.session.query(User).filter(User.id.in_(user_ids)).all()
agents = db.session.query(Agent).filter(Agent.id.in_(agent_ids)).all()
if not agents and action != 'set':
# If action is set and agents is empty, it means clear permissions, which is valid
# But if action is add/remove and agents is empty, it's a no-op
if action == 'remove': return jsonify({'status': 'ok'})
# For add, we might want to warn or just do nothing
for user in users:
if action == 'set':
user.agents = agents
elif action == 'add':
for agent in agents:
if agent not in user.agents:
user.agents.append(agent)
elif action == 'remove':
for agent in agents:
if agent in user.agents:
user.agents.remove(agent)
db.session.commit()
return jsonify({'status': 'ok', 'updated_count': len(users)})
@admin_bp.route('/users/<int:id>/permissions', methods=['POST'])
@require_admin
def update_user_permissions(id):