新增群级插件配置表与服务层,采用MySQL持久化+Redis长期缓存(TTL=-1);后台新增群级插件配置管理页面与API,支持按群按插件维护JSON配置并在修改后同步回填MySQL和刷新Redis;已将群成员变更监控插件接入该配置,支持欢迎文案与卡片URL等按群差异化。
104 lines
4.0 KiB
Python
104 lines
4.0 KiB
Python
# -*- coding: utf-8 -*-
|
||
import json
|
||
from datetime import datetime
|
||
from flask import Blueprint, current_app, jsonify, render_template, request
|
||
|
||
from .auth import login_required
|
||
|
||
group_plugin_config_bp = Blueprint("group_plugin_config", __name__, url_prefix="/group_plugin_config")
|
||
|
||
|
||
def _normalize_datetime_text(value):
|
||
if value is None:
|
||
return value
|
||
if isinstance(value, datetime):
|
||
return value.strftime("%Y-%m-%d %H:%M:%S")
|
||
text = str(value)
|
||
if "T" in text:
|
||
return text.replace("T", " ")[:19]
|
||
return text
|
||
|
||
|
||
@group_plugin_config_bp.route("/")
|
||
@login_required
|
||
def page_group_plugin_config():
|
||
return render_template("group_plugin_config.html")
|
||
|
||
|
||
@group_plugin_config_bp.route("/api/list", methods=["GET"])
|
||
@login_required
|
||
def api_list_group_plugin_config():
|
||
server = current_app.dashboard_server
|
||
service = server.group_plugin_config_service
|
||
group_id = str(request.args.get("group_id", "") or "").strip()
|
||
plugin_name = str(request.args.get("plugin_name", "") or "").strip()
|
||
rows = service.list_configs(group_id=group_id, plugin_name=plugin_name)
|
||
for row in rows:
|
||
row["created_at"] = _normalize_datetime_text(row.get("created_at"))
|
||
row["updated_at"] = _normalize_datetime_text(row.get("updated_at"))
|
||
return jsonify({"success": True, "data": rows})
|
||
|
||
|
||
@group_plugin_config_bp.route("/api/plugins", methods=["GET"])
|
||
@login_required
|
||
def api_list_plugins():
|
||
server = current_app.dashboard_server
|
||
plugin_names = sorted([str(p.name) for p in server.plugin_manager.plugins.values()])
|
||
return jsonify({"success": True, "data": plugin_names})
|
||
|
||
|
||
@group_plugin_config_bp.route("/api/upsert", methods=["POST"])
|
||
@login_required
|
||
def api_upsert_group_plugin_config():
|
||
server = current_app.dashboard_server
|
||
service = server.group_plugin_config_service
|
||
payload = request.get_json(silent=True) or {}
|
||
|
||
group_id = str(payload.get("group_id") or "").strip()
|
||
plugin_name = str(payload.get("plugin_name") or "").strip()
|
||
config_key = str(payload.get("config_key") or "default").strip() or "default"
|
||
enabled = bool(payload.get("enabled", True))
|
||
config_data = payload.get("config_json")
|
||
updated_by = str(payload.get("updated_by") or "dashboard").strip() or "dashboard"
|
||
|
||
if not group_id or not plugin_name:
|
||
return jsonify({"success": False, "message": "group_id 或 plugin_name 不能为空"}), 400
|
||
|
||
if isinstance(config_data, str):
|
||
try:
|
||
config_data = json.loads(config_data)
|
||
except Exception:
|
||
return jsonify({"success": False, "message": "config_json 不是合法 JSON"}), 400
|
||
if not isinstance(config_data, dict):
|
||
return jsonify({"success": False, "message": "config_json 必须是对象"}), 400
|
||
|
||
ok = service.upsert_config(
|
||
group_id=group_id,
|
||
plugin_name=plugin_name,
|
||
config_key=config_key,
|
||
config_data=config_data,
|
||
enabled=enabled,
|
||
updated_by=updated_by,
|
||
)
|
||
if not ok:
|
||
return jsonify({"success": False, "message": "保存失败"}), 500
|
||
return jsonify({"success": True, "message": "保存成功(MySQL + Redis已刷新)"})
|
||
|
||
|
||
@group_plugin_config_bp.route("/api/delete", methods=["POST"])
|
||
@login_required
|
||
def api_delete_group_plugin_config():
|
||
server = current_app.dashboard_server
|
||
service = server.group_plugin_config_service
|
||
payload = request.get_json(silent=True) or {}
|
||
group_id = str(payload.get("group_id") or "").strip()
|
||
plugin_name = str(payload.get("plugin_name") or "").strip()
|
||
config_key = str(payload.get("config_key") or "default").strip() or "default"
|
||
if not group_id or not plugin_name:
|
||
return jsonify({"success": False, "message": "group_id 或 plugin_name 不能为空"}), 400
|
||
|
||
ok = service.delete_config(group_id=group_id, plugin_name=plugin_name, config_key=config_key)
|
||
if not ok:
|
||
return jsonify({"success": False, "message": "删除失败"}), 500
|
||
return jsonify({"success": True, "message": "删除成功(MySQL + Redis已同步)"})
|