from loguru import logger import uuid from flask import Blueprint, jsonify, request, current_app, render_template from .auth import login_required virtual_group_bp = Blueprint('virtual_group', __name__) # 添加虚拟群组管理视图 @virtual_group_bp.route('/') @login_required def virtual_group(): return render_template('virtual_group_management.html') @virtual_group_bp.route('/api/virtual_groups', methods=['GET']) @login_required def get_virtual_groups(): """获取所有虚拟群组""" try: server = current_app.dashboard_server redis_client = server.db_manager.get_redis_connection() # 从Redis获取虚拟群组数据 chat_groups_json = redis_client.get("group_virtual:chat_groups") if not chat_groups_json: return jsonify({"success": True, "data": {"chatGroups": []}}) import json chat_groups = json.loads(chat_groups_json) # 确保群组名称是最新的 for chat_group in chat_groups.get("chatGroups", []): for group in chat_group.get("groups", []): group_id = group.get("id") if group_id: group_name = server.contact_manager.get_nickname(group_id) if group_name: group["name"] = group_name return jsonify({"success": True, "data": chat_groups}) except Exception as e: logger.error(f"获取虚拟群组失败: {e}") return jsonify({"success": False, "error": str(e)}), 500 @virtual_group_bp.route('/api/virtual_groups', methods=['POST']) @login_required def create_virtual_group(): """创建新的虚拟群组""" try: server = current_app.dashboard_server redis_client = server.db_manager.get_redis_connection() data = request.json group_name = data.get('name') if not group_name: return jsonify({"success": False, "error": "群组名称不能为空"}), 400 # 从Redis获取现有虚拟群组 import json chat_groups_json = redis_client.get("group_virtual:chat_groups") if chat_groups_json: chat_groups = json.loads(chat_groups_json) else: chat_groups = {"chatGroups": []} # 创建新的虚拟群组 new_group = { "id": str(uuid.uuid4()), "name": group_name, "groups": [] } chat_groups["chatGroups"].append(new_group) # 保存回Redis redis_client.set("group_virtual:chat_groups", json.dumps(chat_groups, ensure_ascii=False)) return jsonify({ "success": True, "message": f"虚拟群组 {group_name} 创建成功", "data": new_group }) except Exception as e: logger.error(f"创建虚拟群组失败: {e}") return jsonify({"success": False, "error": str(e)}), 500 @virtual_group_bp.route('/api/virtual_groups/', methods=['DELETE']) @login_required def delete_virtual_group(group_id): """删除虚拟群组""" try: server = current_app.dashboard_server redis_client = server.db_manager.get_redis_connection() # 从Redis获取现有虚拟群组 import json chat_groups_json = redis_client.get("group_virtual:chat_groups") if not chat_groups_json: return jsonify({"success": False, "error": "虚拟群组不存在"}), 404 chat_groups = json.loads(chat_groups_json) # 查找并删除指定的虚拟群组 found = False for i, group in enumerate(chat_groups.get("chatGroups", [])): if group.get("id") == group_id: del chat_groups["chatGroups"][i] found = True break if not found: return jsonify({"success": False, "error": "虚拟群组不存在"}), 404 # 保存回Redis redis_client.set("group_virtual:chat_groups", json.dumps(chat_groups, ensure_ascii=False)) return jsonify({ "success": True, "message": "虚拟群组删除成功" }) except Exception as e: logger.error(f"删除虚拟群组失败: {e}") return jsonify({"success": False, "error": str(e)}), 500 @virtual_group_bp.route('/api/virtual_groups//groups', methods=['POST']) @login_required def add_group_to_virtual(group_id): """向虚拟群组添加微信群""" try: server = current_app.dashboard_server redis_client = server.db_manager.get_redis_connection() data = request.json wx_group_id = data.get('wx_group_id') if not wx_group_id: return jsonify({"success": False, "error": "微信群ID不能为空"}), 400 # 检查微信群是否存在 group_name = server.contact_manager.get_nickname(wx_group_id) if not group_name: return jsonify({"success": False, "error": "微信群不存在或未被机器人识别"}), 404 # 从Redis获取现有虚拟群组 import json chat_groups_json = redis_client.get("group_virtual:chat_groups") if not chat_groups_json: return jsonify({"success": False, "error": "虚拟群组不存在"}), 404 chat_groups = json.loads(chat_groups_json) # 查找指定的虚拟群组 found = False for chat_group in chat_groups.get("chatGroups", []): if chat_group.get("id") == group_id: # 检查群是否已在虚拟群组中 for group in chat_group.get("groups", []): if group.get("id") == wx_group_id: return jsonify({"success": False, "error": "该微信群已在虚拟群组中"}), 400 # 添加微信群到虚拟群组 chat_group["groups"].append({ "id": wx_group_id, "name": group_name }) found = True break if not found: return jsonify({"success": False, "error": "虚拟群组不存在"}), 404 # 保存回Redis redis_client.set("group_virtual:chat_groups", json.dumps(chat_groups, ensure_ascii=False)) return jsonify({ "success": True, "message": f"微信群 {group_name} 已添加到虚拟群组", "data": { "id": wx_group_id, "name": group_name } }) except Exception as e: logger.error(f"向虚拟群组添加微信群失败: {e}") return jsonify({"success": False, "error": str(e)}), 500 @virtual_group_bp.route('/api/virtual_groups//groups/', methods=['DELETE']) @login_required def remove_group_from_virtual(group_id, wx_group_id): """从虚拟群组移除微信群""" try: server = current_app.dashboard_server redis_client = server.db_manager.get_redis_connection() # 从Redis获取现有虚拟群组 import json chat_groups_json = redis_client.get("group_virtual:chat_groups") if not chat_groups_json: return jsonify({"success": False, "error": "虚拟群组不存在"}), 404 chat_groups = json.loads(chat_groups_json) # 查找指定的虚拟群组和微信群 found_group = False found_wx_group = False for chat_group in chat_groups.get("chatGroups", []): if chat_group.get("id") == group_id: found_group = True for i, group in enumerate(chat_group.get("groups", [])): if group.get("id") == wx_group_id: del chat_group["groups"][i] found_wx_group = True break break if not found_group: return jsonify({"success": False, "error": "虚拟群组不存在"}), 404 if not found_wx_group: return jsonify({"success": False, "error": "该微信群不在虚拟群组中"}), 404 # 保存回Redis redis_client.set("group_virtual:chat_groups", json.dumps(chat_groups, ensure_ascii=False)) return jsonify({ "success": True, "message": "微信群已从虚拟群组移除" }) except Exception as e: logger.error(f"从虚拟群组移除微信群失败: {e}") return jsonify({"success": False, "error": str(e)}), 500 @virtual_group_bp.route('/api/virtual_groups/', methods=['PUT']) @login_required def update_virtual_group(group_id): """更新虚拟群组信息""" try: server = current_app.dashboard_server redis_client = server.db_manager.get_redis_connection() data = request.json group_name = data.get('name') if not group_name: return jsonify({"success": False, "error": "群组名称不能为空"}), 400 # 从Redis获取现有虚拟群组 import json chat_groups_json = redis_client.get("group_virtual:chat_groups") if not chat_groups_json: return jsonify({"success": False, "error": "虚拟群组不存在"}), 404 chat_groups = json.loads(chat_groups_json) # 查找并更新指定的虚拟群组 found = False for chat_group in chat_groups.get("chatGroups", []): if chat_group.get("id") == group_id: chat_group["name"] = group_name found = True break if not found: return jsonify({"success": False, "error": "虚拟群组不存在"}), 404 # 保存回Redis redis_client.set("group_virtual:chat_groups", json.dumps(chat_groups, ensure_ascii=False)) return jsonify({ "success": True, "message": "虚拟群组更新成功", "data": { "id": group_id, "name": group_name } }) except Exception as e: logger.error(f"更新虚拟群组失败: {e}") return jsonify({"success": False, "error": str(e)}), 500