funs.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. local M = {}
  2. M.file_get_contents = function(filepath)
  3. local file, errmsg = io.open(fw.website_dir()..filepath, "r")
  4. if not file then
  5. fw.throw_string(errmsg)
  6. end
  7. local content = file:read("*a")
  8. if content == nil or content == "" then
  9. print("file_get_contents error: ",filepath)
  10. return ""
  11. end
  12. local replaced,c2 = engine.replace(content)
  13. if replaced then
  14. return c2
  15. end
  16. return content
  17. end
  18. M.menu_top = function()
  19. -- 当前请求路径
  20. local request_path = request.filepath() or ""
  21. -- 取配置
  22. local menuData = require("fwutils.develop.function.menu").bytecode(string.format("%d",engine.cfg.role_id()))
  23. if not menuData then
  24. return "<!-- no menu config -->"
  25. end
  26. -- 提取并排序主菜单
  27. local menuItems = {}
  28. for name, item in pairs(menuData) do
  29. table.insert(menuItems, {
  30. name = name,
  31. item = item,
  32. sort = item.sort or 0
  33. })
  34. end
  35. table.sort(menuItems, function(a, b)
  36. return a.sort > b.sort
  37. end)
  38. local html = [[
  39. <ul class="navbar-nav me-auto mb-2 mb-lg-0">
  40. ]]
  41. -- 渲染主菜单
  42. for _, entry in ipairs(menuItems) do
  43. local name = entry.name
  44. local item = entry.item
  45. local icon = item.icon or ""
  46. local path = item.path or "#"
  47. local activeClass = ""
  48. if item.children then
  49. -- 有子菜单
  50. local isMegamenu = item.megamenu and " dropdown-megamenu" or ""
  51. -- 检查子菜单是否有激活
  52. local hasActiveChild = false
  53. local children = {}
  54. for childName, childItem in pairs(item.children) do
  55. table.insert(children, {
  56. name = childName,
  57. item = childItem,
  58. sort = childItem.sort or 0
  59. })
  60. if not hasActiveChild and (request_path == (childItem.path or "")) then
  61. hasActiveChild = true
  62. end
  63. end
  64. table.sort(children, function(a, b)
  65. return a.sort > b.sort
  66. end)
  67. -- 父菜单激活:当前页面是父菜单path,或是任一子菜单path
  68. if request_path == path or hasActiveChild then
  69. activeClass = "active-link"
  70. end
  71. html = html .. [[
  72. <li class="nav-item dropdown ]] .. activeClass .. [[">
  73. <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
  74. <i class="]] .. icon .. [["></i> ]] .. name .. [[
  75. </a>
  76. <ul class="dropdown-menu]] .. isMegamenu .. [[">
  77. ]]
  78. -- 渲染子菜单
  79. for _, child in ipairs(children) do
  80. local childName = child.name
  81. local childItem = child.item
  82. local childPath = childItem.path or "#"
  83. -- 子菜单不设置激活样式
  84. html = html .. [[
  85. <li>
  86. <a class="dropdown-item" href="]] .. childPath .. [[">
  87. <span>]] .. childName .. [[</span>
  88. </a>
  89. </li>
  90. ]]
  91. end
  92. html = html .. [[
  93. </ul>
  94. </li>
  95. ]]
  96. else
  97. -- 没有子菜单
  98. -- 简单的“当前页面是否激活”检查
  99. if request_path == path then
  100. activeClass = "active-link"
  101. end
  102. html = html .. [[
  103. <li class="nav-item ]] .. activeClass .. [[">
  104. <a class="nav-link" href="]] .. path .. [[">
  105. <i class="]] .. icon .. [["></i> ]] .. name .. [[
  106. </a>
  107. </li>
  108. ]]
  109. end
  110. end
  111. html = html .. [[
  112. </ul>
  113. ]]
  114. return [=[
  115. <nav class="navbar navbar-expand-lg">
  116. <div class="container">
  117. <div class="offcanvas offcanvas-end" id="MobileMenu">
  118. <div class="offcanvas-header">
  119. <h5 class="offcanvas-title semibold">Navigation</h5>
  120. <button type="button" class="btn btn-danger btn-sm ms-auto" data-bs-dismiss="offcanvas">
  121. <i class="icon-clear"></i>
  122. </button>
  123. </div>
  124. ]=]..html..[[
  125. </div>
  126. </div>
  127. </nav>
  128. ]]
  129. end
  130. M.regist = function(env)
  131. for key, value in pairs(M) do
  132. if type(value) == "function" and key ~= "functions" and key ~= "get_all_functions" then
  133. env[key] = value
  134. end
  135. end
  136. return env
  137. end
  138. return M