格式化代码
This commit is contained in:
@@ -3,15 +3,16 @@ 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():
|
||||
@@ -19,15 +20,15 @@ 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", []):
|
||||
@@ -36,7 +37,7 @@ def get_virtual_groups():
|
||||
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}")
|
||||
@@ -51,11 +52,11 @@ def create_virtual_group():
|
||||
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")
|
||||
@@ -63,21 +64,21 @@ def create_virtual_group():
|
||||
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,
|
||||
"success": True,
|
||||
"message": f"虚拟群组 {group_name} 创建成功",
|
||||
"data": new_group
|
||||
})
|
||||
@@ -93,15 +94,15 @@ 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", [])):
|
||||
@@ -109,15 +110,15 @@ def delete_virtual_group(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,
|
||||
"success": True,
|
||||
"message": "虚拟群组删除成功"
|
||||
})
|
||||
except Exception as e:
|
||||
@@ -133,24 +134,24 @@ def add_group_to_virtual(group_id):
|
||||
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", []):
|
||||
@@ -159,7 +160,7 @@ def add_group_to_virtual(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,
|
||||
@@ -167,15 +168,15 @@ def add_group_to_virtual(group_id):
|
||||
})
|
||||
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,
|
||||
"success": True,
|
||||
"message": f"微信群 {group_name} 已添加到虚拟群组",
|
||||
"data": {
|
||||
"id": wx_group_id,
|
||||
@@ -194,15 +195,15 @@ 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
|
||||
@@ -215,18 +216,18 @@ def remove_group_from_virtual(group_id, wx_group_id):
|
||||
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,
|
||||
"success": True,
|
||||
"message": "微信群已从虚拟群组移除"
|
||||
})
|
||||
except Exception as e:
|
||||
@@ -242,19 +243,19 @@ def update_virtual_group(group_id):
|
||||
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", []):
|
||||
@@ -262,15 +263,15 @@ def update_virtual_group(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,
|
||||
"success": True,
|
||||
"message": "虚拟群组更新成功",
|
||||
"data": {
|
||||
"id": group_id,
|
||||
@@ -279,4 +280,4 @@ def update_virtual_group(group_id):
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"更新虚拟群组失败: {e}")
|
||||
return jsonify({"success": False, "error": str(e)}), 500
|
||||
return jsonify({"success": False, "error": str(e)}), 500
|
||||
|
||||
Reference in New Issue
Block a user