125 lines
3.4 KiB
Python
125 lines
3.4 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
import pymysql
|
||
|
||
# 数据库连接配置
|
||
db_config = {
|
||
'host': '1.15.149.240',
|
||
'user': 'zhibo',
|
||
'password': 'zCETFpGMwYN3CNeH',
|
||
'database': 'zhibo',
|
||
'charset': 'utf8mb4'
|
||
}
|
||
|
||
# 连接数据库
|
||
conn = pymysql.connect(**db_config)
|
||
cursor = conn.cursor(pymysql.cursors.DictCursor)
|
||
|
||
# 查询所有显示的菜单
|
||
sql = """
|
||
SELECT id, pid, name, component, menu_type, sort, is_show
|
||
FROM eb_system_menu
|
||
WHERE is_show = 1
|
||
ORDER BY pid, sort, id
|
||
"""
|
||
|
||
cursor.execute(sql)
|
||
all_menus = cursor.fetchall()
|
||
|
||
# 构建菜单树
|
||
menu_tree = {}
|
||
for menu in all_menus:
|
||
if menu['pid'] == 0:
|
||
if menu['id'] not in menu_tree:
|
||
menu_tree[menu['id']] = {'info': menu, 'children': []}
|
||
else:
|
||
menu_tree[menu['id']]['info'] = menu
|
||
|
||
# 添加子菜单
|
||
for menu in all_menus:
|
||
if menu['pid'] != 0:
|
||
parent_id = menu['pid']
|
||
if parent_id not in menu_tree:
|
||
menu_tree[parent_id] = {'info': None, 'children': []}
|
||
menu_tree[parent_id]['children'].append(menu)
|
||
|
||
# 生成Markdown文档
|
||
output = """# 系统完整菜单结构
|
||
|
||
> 生成时间:2024-12-04
|
||
> 说明:本文档记录了系统中所有一级菜单及其子菜单的完整层级结构
|
||
|
||
---
|
||
|
||
## 📋 菜单统计
|
||
|
||
- **一级菜单总数**: {total_level1}
|
||
- **子菜单总数**: {total_children}
|
||
- **菜单总数**: {total_all}
|
||
|
||
---
|
||
|
||
## 🗂️ 完整菜单结构
|
||
|
||
"""
|
||
|
||
# 统计
|
||
total_level1 = len([m for m in all_menus if m['pid'] == 0])
|
||
total_children = len([m for m in all_menus if m['pid'] != 0])
|
||
total_all = len(all_menus)
|
||
|
||
output = output.format(
|
||
total_level1=total_level1,
|
||
total_children=total_children,
|
||
total_all=total_all
|
||
)
|
||
|
||
# 按sort排序一级菜单
|
||
sorted_level1 = sorted(
|
||
[(k, v) for k, v in menu_tree.items() if v['info'] is not None],
|
||
key=lambda x: (x[1]['info']['sort'], x[1]['info']['id'])
|
||
)
|
||
|
||
# 生成菜单列表
|
||
for idx, (menu_id, menu_data) in enumerate(sorted_level1, 1):
|
||
menu_info = menu_data['info']
|
||
children = menu_data['children']
|
||
|
||
# 一级菜单标题
|
||
output += f"\n### {idx}. {menu_info['name']}\n\n"
|
||
output += f"- **菜单ID**: {menu_info['id']}\n"
|
||
output += f"- **路由路径**: `{menu_info['component']}`\n"
|
||
output += f"- **菜单类型**: {menu_info['menu_type']}\n"
|
||
output += f"- **排序**: {menu_info['sort']}\n"
|
||
|
||
if children:
|
||
output += f"- **子菜单数量**: {len(children)}\n\n"
|
||
output += "#### 子菜单列表:\n\n"
|
||
# 按sort排序子菜单
|
||
sorted_children = sorted(children, key=lambda x: (x['sort'], x['id']))
|
||
for child_idx, child in enumerate(sorted_children, 1):
|
||
output += f"{child_idx}. **{child['name']}**\n"
|
||
output += f" - ID: {child['id']}\n"
|
||
output += f" - 路径: `{child['component']}`\n"
|
||
output += f" - 类型: {child['menu_type']}\n"
|
||
output += f" - 排序: {child['sort']}\n\n"
|
||
else:
|
||
output += f"- **子菜单数量**: 0(无子菜单)\n\n"
|
||
|
||
output += "---\n"
|
||
|
||
# 保存文件
|
||
output_file = r'小张\行动\完整菜单结构.md'
|
||
with open(output_file, 'w', encoding='utf-8') as f:
|
||
f.write(output)
|
||
|
||
print(f"✅ 菜单结构已生成到文件: {output_file}")
|
||
print(f"📊 统计信息:")
|
||
print(f" - 一级菜单: {total_level1} 个")
|
||
print(f" - 子菜单: {total_children} 个")
|
||
print(f" - 总计: {total_all} 个")
|
||
|
||
# 关闭连接
|
||
cursor.close()
|
||
conn.close()
|