feat:
1. 管理后台支持为不同 Agent 应用上传和管理视频、PDF 等新手指南资料。 2. 前端聊天界面根据后台配置,动态展示“新手指南”入口。 3. 用户点击后可直接在系统内预览视频或 PDF 文档。
This commit is contained in:
1 parent
cc53d29e76
commit
f8de4630a5
19 files changed
+742
-122
No files matched your search
+101
-1
@@ -5,7 +5,8 @@ backend/app/api/admin.py
|
||||
逻辑关系:提供 Agent 配置管理、用户与权限管理接口,供前端 Admin 控制台使用
|
||||
"""
|
||||
from flask import Blueprint, request, jsonify, current_app
|
||||
from app.models import Agent, User, PermissionRequest, ChatLog, Feedback, GeneralFeedback, Department, UserFavoriteAgent, UserDefaultAgent, QuickQuestion, UserHomepageConfig, LoginAudit, WecomNotifyAudit
|
||||
from app.models import Agent, User, PermissionRequest, ChatLog, Feedback, GeneralFeedback, Department, UserFavoriteAgent, UserDefaultAgent, QuickQuestion, UserHomepageConfig, LoginAudit, WecomNotifyAudit, AgentGuide
|
||||
from app.services.minio_service import minio_service
|
||||
from app.decorators import require_admin
|
||||
from app.config import Config
|
||||
from app import db
|
||||
@@ -847,6 +848,105 @@ def list_wecom_bindings():
|
||||
users = query.order_by(User.id.asc()).all()
|
||||
return jsonify([{'id': u.id, 'username': u.username, 'real_name': u.real_name, 'email': u.email, 'role': u.role, 'status': u.status, 'wecom_userid': u.wecom_userid, 'wecom_unionid': u.wecom_unionid, 'bound': bool(u.wecom_userid)} for u in users])
|
||||
|
||||
@admin_bp.route('/guides/upload', methods=['POST'])
|
||||
@require_admin
|
||||
def upload_guide():
|
||||
if 'file' not in request.files:
|
||||
raise ValidationError('No file part')
|
||||
file = request.files['file']
|
||||
if file.filename == '':
|
||||
raise ValidationError('No selected file')
|
||||
|
||||
content_type = file.content_type
|
||||
file_size = len(file.read())
|
||||
file.seek(0)
|
||||
|
||||
try:
|
||||
object_name = minio_service.upload_file(file, file.filename, content_type, file_size)
|
||||
return jsonify({
|
||||
'file_path': object_name,
|
||||
'file_name': file.filename,
|
||||
'file_size': file_size,
|
||||
'file_type': 'video' if content_type.startswith('video') else 'pdf' if 'pdf' in content_type else 'image'
|
||||
})
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
raise AppError(f"Upload failed: {str(e)}")
|
||||
|
||||
@admin_bp.route('/guides', methods=['POST'])
|
||||
@require_admin
|
||||
def create_guide():
|
||||
data = request.json
|
||||
guide = AgentGuide(
|
||||
agent_id=data['agent_id'],
|
||||
file_name=data['file_name'],
|
||||
file_type=data['file_type'],
|
||||
file_path=data['file_path'],
|
||||
file_size=data.get('file_size'),
|
||||
remark=data.get('remark'),
|
||||
sort_order=data.get('sort_order', 0),
|
||||
status=data.get('status', 1),
|
||||
created_by=_get_auth_user().id
|
||||
)
|
||||
db.session.add(guide)
|
||||
db.session.commit()
|
||||
return jsonify({'id': guide.id})
|
||||
|
||||
@admin_bp.route('/guides', methods=['GET'])
|
||||
@require_admin
|
||||
def list_guides():
|
||||
agent_id = request.args.get('agent_id')
|
||||
query = db.session.query(AgentGuide)
|
||||
if agent_id:
|
||||
query = query.filter_by(agent_id=agent_id)
|
||||
guides = query.order_by(AgentGuide.sort_order).all()
|
||||
|
||||
result = []
|
||||
for g in guides:
|
||||
result.append({
|
||||
'id': g.id,
|
||||
'agent_id': g.agent_id,
|
||||
'file_name': g.file_name,
|
||||
'file_type': g.file_type,
|
||||
'file_path': g.file_path,
|
||||
'file_url': minio_service.get_public_url(g.file_path),
|
||||
'file_size': g.file_size,
|
||||
'remark': g.remark,
|
||||
'sort_order': g.sort_order,
|
||||
'status': g.status,
|
||||
'created_at': g.created_at.isoformat()
|
||||
})
|
||||
return jsonify(result)
|
||||
|
||||
@admin_bp.route('/guides/<int:id>', methods=['PUT'])
|
||||
@require_admin
|
||||
def update_guide(id):
|
||||
guide = db.session.query(AgentGuide).get(id)
|
||||
if not guide:
|
||||
raise NotFoundError('Guide not found')
|
||||
|
||||
data = request.json
|
||||
if 'file_name' in data: guide.file_name = data['file_name']
|
||||
if 'remark' in data: guide.remark = data['remark']
|
||||
if 'sort_order' in data: guide.sort_order = data['sort_order']
|
||||
if 'status' in data: guide.status = data['status']
|
||||
|
||||
db.session.commit()
|
||||
return jsonify({'success': True})
|
||||
|
||||
@admin_bp.route('/guides/<int:id>', methods=['DELETE'])
|
||||
@require_admin
|
||||
def delete_guide(id):
|
||||
guide = db.session.query(AgentGuide).get(id)
|
||||
if not guide:
|
||||
raise NotFoundError('Guide not found')
|
||||
|
||||
minio_service.delete_file(guide.file_path)
|
||||
db.session.delete(guide)
|
||||
db.session.commit()
|
||||
return jsonify({'success': True})
|
||||
|
||||
@admin_bp.route('/wecom/bind', methods=['POST'])
|
||||
@require_admin
|
||||
def bind_wecom_user():
|
||||
|
||||
@@ -4,11 +4,13 @@ backend/app/api/agents.py
|
||||
引用文件:flask, models, decorators
|
||||
逻辑关系:根据当前用户的权限返回其可访问的 Agent 集合
|
||||
"""
|
||||
from flask import Blueprint, jsonify
|
||||
import os
|
||||
from flask import Blueprint, jsonify, send_from_directory
|
||||
|
||||
from app import db
|
||||
from app.decorators import _get_current_user # 内部工具函数
|
||||
from app.models import Agent, User
|
||||
from app.models import Agent, User, AgentGuide
|
||||
from app.services.minio_service import minio_service
|
||||
|
||||
# 定义 Blueprint,将在应用工厂中注册为 /api/agents
|
||||
agents_bp = Blueprint('agents', __name__)
|
||||
@@ -58,6 +60,41 @@ def list_user_agents():
|
||||
# 序列化为前端所需字段
|
||||
return jsonify([_serialize_agent_row(row) for row in agent_rows])
|
||||
|
||||
@agents_bp.route('/<int:agent_id>/guides', methods=['GET'])
|
||||
def list_agent_guides(agent_id):
|
||||
"""
|
||||
获取指定应用的新手指南列表 (需登录)
|
||||
用途:客户端 Chat 界面点击新手指南
|
||||
"""
|
||||
_get_current_user() # 确保已登录
|
||||
|
||||
if agent_id == 0:
|
||||
generic_agent = db.session.query(Agent).filter_by(name='通用入口').first()
|
||||
if generic_agent:
|
||||
agent_id = generic_agent.id
|
||||
else:
|
||||
return jsonify([])
|
||||
|
||||
guides = (
|
||||
db.session.query(AgentGuide)
|
||||
.filter_by(agent_id=agent_id, status=1)
|
||||
.order_by(AgentGuide.sort_order.asc(), AgentGuide.created_at.desc())
|
||||
.all()
|
||||
)
|
||||
|
||||
result = []
|
||||
for g in guides:
|
||||
result.append({
|
||||
'id': g.id,
|
||||
'file_name': g.file_name,
|
||||
'file_type': g.file_type,
|
||||
'file_url': minio_service.get_public_url(g.file_path),
|
||||
'file_size': g.file_size,
|
||||
'remark': g.remark,
|
||||
'updated_at': g.updated_at.isoformat()
|
||||
})
|
||||
return jsonify(result)
|
||||
|
||||
@agents_bp.route('/public', methods=['GET'])
|
||||
def list_public_agents():
|
||||
"""
|
||||
@@ -73,4 +110,9 @@ def list_public_agents():
|
||||
)
|
||||
return jsonify([_serialize_agent_row(row) for row in agent_rows])
|
||||
|
||||
# 为了兼容本地无MinIO的环境,暴露一个静态文件读取接口
|
||||
@agents_bp.route('/guides/files/<path:filename>', methods=['GET'])
|
||||
def serve_guide_file(filename):
|
||||
return send_from_directory(os.path.join(os.getcwd(), 'data', 'agent-guides'), filename)
|
||||
|
||||
|
||||
Reference in new issue
Block a user