menu.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. local fw = require("fastweb")
  2. local config = require("fwutils.config")
  3. local M = {}
  4. -- CREATE TABLE `fw_menu` (
  5. -- `id` int NOT NULL AUTO_INCREMENT,
  6. -- `role_id` int DEFAULT NULL,
  7. -- `title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  8. -- `path` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  9. -- `icon` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  10. -- `sort` int DEFAULT NULL,
  11. -- `parent_id` int DEFAULT NULL,
  12. -- PRIMARY KEY (`id`)
  13. -- ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  14. -- 更新
  15. M.update = function(role_id,conn)
  16. -- 查询菜单表
  17. local select = conn:select()
  18. select:table("fw_menu")
  19. select:where_expression("AND delete_time IS NULL")
  20. if role_id ~= nil then
  21. select:where_i32("role_id", "=", role_id)
  22. end
  23. local result = select:query()
  24. local bc = {}
  25. local items_by_id = {} -- 通过id快速查找菜单项:{id = {role_id, title, item, parent_id}}
  26. local items_with_parent = {} -- 存储有父级的菜单项
  27. -- 第一遍:读取所有菜单项并存储
  28. while result:next() do
  29. local id = result:get("id")
  30. local role_id = tostring(result:get("role_id"))
  31. local title = result:get("title")
  32. local path = result:get("path")
  33. local icon = result:get("icon")
  34. local sort = result:get("sort")
  35. local parent_id = result:get("parent_id")
  36. if not title or title == "" then
  37. goto continue
  38. end
  39. if bc[role_id] == nil then
  40. bc[role_id] = {}
  41. end
  42. local item = {
  43. path = path,
  44. icon = icon,
  45. sort = sort,
  46. }
  47. -- 存储所有菜单项信息
  48. items_by_id[id] = {
  49. role_id = role_id,
  50. title = title,
  51. item = item,
  52. parent_id = parent_id
  53. }
  54. -- 如果parent_id为空,则作为顶层菜单项
  55. if parent_id == nil or parent_id == 0 then
  56. bc[role_id][title] = item
  57. else
  58. -- 有父级,记录下来稍后处理
  59. table.insert(items_with_parent, {
  60. id = id,
  61. role_id = role_id,
  62. title = title,
  63. item = item,
  64. parent_id = parent_id
  65. })
  66. end
  67. ::continue::
  68. end
  69. -- 第二遍:处理有父级的菜单项,构建children结构
  70. for _, menu_item in ipairs(items_with_parent) do
  71. local parent_info = items_by_id[menu_item.parent_id]
  72. if parent_info then
  73. local parent_item = parent_info.item
  74. -- 如果父项还没有children表,创建它
  75. if not parent_item.children then
  76. parent_item.children = {}
  77. end
  78. -- 将子项添加到父项的children中
  79. parent_item.children[menu_item.title] = menu_item.item
  80. end
  81. end
  82. local code = "return " .. require("serpent").serialize(bc, {comment = false})
  83. utils.save_file(fw.website_dir().."/"..(config.path.luabytecode:gsub("%.", "/")).."/menu_bc.lua",code)
  84. return true
  85. end
  86. M.get = function(role_id)
  87. local menu_bc = require(config.path.luabytecode..".menu_bc")
  88. return menu_bc[string.format("%d",role_id)]
  89. end
  90. return M