插件群状态弹窗支持按群开启/关闭,复用原有群权限缓存管理逻辑
This commit is contained in:
@@ -134,6 +134,55 @@ def get_plugin_group_status():
|
||||
return jsonify({"success": False, "message": f"获取插件群状态失败: {str(e)}"})
|
||||
|
||||
|
||||
@plugin_routes.route('/api/plugins/group_status/toggle', methods=['POST'])
|
||||
@login_required
|
||||
def toggle_plugin_group_status():
|
||||
"""切换插件在指定群的启用状态(开启/关闭)"""
|
||||
try:
|
||||
server = current_app.dashboard_server
|
||||
data = request.get_json() or {}
|
||||
plugin_name = (data.get('plugin_name') or '').strip()
|
||||
group_id = (data.get('group_id') or '').strip()
|
||||
status = (data.get('status') or '').strip().lower()
|
||||
|
||||
# 参数校验:保证前端传入完整且合法的切换目标。
|
||||
if not plugin_name:
|
||||
return jsonify({"success": False, "message": "缺少插件名称参数"})
|
||||
if not group_id:
|
||||
return jsonify({"success": False, "message": "缺少群ID参数"})
|
||||
if status not in ('enabled', 'disabled'):
|
||||
return jsonify({"success": False, "message": "状态参数非法,仅支持 enabled / disabled"})
|
||||
|
||||
# 查找插件实例,沿用现有查找逻辑,兼容模块名与展示名。
|
||||
display_name, plugin = server.plugin_manager.find_plugin_by_name(plugin_name)
|
||||
if not plugin:
|
||||
return jsonify({"success": False, "message": f"未找到插件: {plugin_name}"})
|
||||
|
||||
# 插件必须注册了 feature 才能进行群级开关;否则无法映射到权限系统。
|
||||
plugin_feature = getattr(plugin, "feature", None)
|
||||
if plugin_feature is None:
|
||||
return jsonify({"success": False, "message": "该插件未接入群级开关能力,无法直接切换"})
|
||||
|
||||
# 将字符串状态映射到统一权限枚举,然后写入群权限缓存与Redis。
|
||||
target_status = PermissionStatus.ENABLED if status == 'enabled' else PermissionStatus.DISABLED
|
||||
GroupBotManager.set_group_permission(group_id, plugin_feature, target_status)
|
||||
|
||||
# 返回简洁确认信息,方便前端即时提示与二次渲染。
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"message": f"已将群 {group_id} 的插件“{plugin.name}”设置为{'开启' if target_status == PermissionStatus.ENABLED else '关闭'}",
|
||||
"data": {
|
||||
"plugin_name": plugin.name,
|
||||
"group_id": group_id,
|
||||
"status": target_status.value,
|
||||
"feature_key": getattr(plugin_feature, "name", ""),
|
||||
}
|
||||
})
|
||||
except Exception as e:
|
||||
LOG.error(f"切换插件群状态失败: {str(e)}", exc_info=True)
|
||||
return jsonify({"success": False, "message": f"切换插件群状态失败: {str(e)}"})
|
||||
|
||||
|
||||
@plugin_routes.route('/api/plugins/info', methods=['GET'])
|
||||
@login_required
|
||||
def get_plugin_info():
|
||||
|
||||
Reference in New Issue
Block a user