Files
daydaytalk-fwutils/target/fwutils/funs.lua
2026-01-30 15:04:00 +08:00

155 lines
4.4 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
local M = {}
M.file_get_contents = function(filepath)
local file, errmsg = io.open(fw.website_dir()..filepath, "r")
if not file then
fw.throw_string(errmsg)
end
local content = file:read("*a")
if content == nil or content == "" then
print("file_get_contents error: ",filepath)
return ""
end
local replaced,c2 = engine.replace(content)
if replaced then
return c2
end
return content
end
M.menu_top = function()
-- 当前请求路径
local request_path = request.filepath() or ""
-- 取配置
local menuData = require("fwutils.develop.function.menu").bytecode(string.format("%d",engine.cfg.role_id()))
if not menuData then
return "<!-- no menu config -->"
end
-- 提取并排序主菜单
local menuItems = {}
for name, item in pairs(menuData) do
table.insert(menuItems, {
name = name,
item = item,
sort = item.sort or 0
})
end
table.sort(menuItems, function(a, b)
return a.sort > b.sort
end)
local html = [[
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
]]
-- 渲染主菜单
for _, entry in ipairs(menuItems) do
local name = entry.name
local item = entry.item
local icon = item.icon or ""
local path = item.path or "#"
local activeClass = ""
if item.children then
-- 有子菜单
local isMegamenu = item.megamenu and " dropdown-megamenu" or ""
-- 检查子菜单是否有激活
local hasActiveChild = false
local children = {}
for childName, childItem in pairs(item.children) do
table.insert(children, {
name = childName,
item = childItem,
sort = childItem.sort or 0
})
if not hasActiveChild and (request_path == (childItem.path or "")) then
hasActiveChild = true
end
end
table.sort(children, function(a, b)
return a.sort > b.sort
end)
-- 父菜单激活当前页面是父菜单path或是任一子菜单path
if request_path == path or hasActiveChild then
activeClass = "active-link"
end
html = html .. [[
<li class="nav-item dropdown ]] .. activeClass .. [[">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="]] .. icon .. [["></i> ]] .. name .. [[
</a>
<ul class="dropdown-menu]] .. isMegamenu .. [[">
]]
-- 渲染子菜单
for _, child in ipairs(children) do
local childName = child.name
local childItem = child.item
local childPath = childItem.path or "#"
-- 子菜单不设置激活样式
html = html .. [[
<li>
<a class="dropdown-item" href="]] .. childPath .. [[">
<span>]] .. childName .. [[</span>
</a>
</li>
]]
end
html = html .. [[
</ul>
</li>
]]
else
-- 没有子菜单
-- 简单的“当前页面是否激活”检查
if request_path == path then
activeClass = "active-link"
end
html = html .. [[
<li class="nav-item ]] .. activeClass .. [[">
<a class="nav-link" href="]] .. path .. [[">
<i class="]] .. icon .. [["></i> ]] .. name .. [[
</a>
</li>
]]
end
end
html = html .. [[
</ul>
]]
return [=[
<nav class="navbar navbar-expand-lg">
<div class="container">
<div class="offcanvas offcanvas-end" id="MobileMenu">
<div class="offcanvas-header">
<h5 class="offcanvas-title semibold">Navigation</h5>
<button type="button" class="btn btn-danger btn-sm ms-auto" data-bs-dismiss="offcanvas">
<i class="icon-clear"></i>
</button>
</div>
]=]..html..[[
</div>
</div>
</nav>
]]
end
M.regist = function(env)
for key, value in pairs(M) do
if type(value) == "function" and key ~= "functions" and key ~= "get_all_functions" then
env[key] = value
end
end
return env
end
return M