simulate_api_visible.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. import pymysql
  3. M = dict(
  4. host='cq-cdb-8fjmemkb.sql.tencentcdb.com',
  5. port=27220,
  6. user='root',
  7. password='Ylrz_1q2w3e4r5t6y',
  8. database='ylrz_saas',
  9. charset='utf8mb4',
  10. )
  11. def build_tree(rows, parent_id=0):
  12. tree = []
  13. for r in rows:
  14. if r['parent_id'] == parent_id:
  15. node = dict(r)
  16. node['children'] = build_tree(rows, r['menu_id'])
  17. tree.append(node)
  18. tree.sort(key=lambda x: (x.get('order_num') or 0, x['menu_id']))
  19. return tree
  20. def print_tree(nodes, depth=0, max_depth=2):
  21. for n in nodes:
  22. print(' ' * depth + '- %s (id=%s, path=%s)' % (n['menu_name'], n['menu_id'], n['path']))
  23. if depth < max_depth and n.get('children'):
  24. print_tree(n['children'], depth + 1, max_depth)
  25. c = pymysql.connect(**M)
  26. cur = c.cursor(pymysql.cursors.DictCursor)
  27. cur.execute(
  28. "SELECT menu_id, menu_name, parent_id, order_num, path, menu_type, visible "
  29. "FROM tenant_sys_menu WHERE visible='0' ORDER BY parent_id, order_num, menu_id"
  30. )
  31. rows = cur.fetchall()
  32. tree = build_tree(rows, 0)
  33. print('API visible=0 top level count:', len(tree))
  34. print_tree(tree, 0, 2)
  35. cur.close()
  36. c.close()