67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
using Newtonsoft.Json;
|
|
|
|
namespace YLErp.Web
|
|
{
|
|
public static class MenuHelper
|
|
{
|
|
public static List<MenuItem> GetMenus(string fileName)
|
|
{
|
|
try
|
|
{
|
|
var realFilePath = Server.MapPath("/App_Data/" + fileName);
|
|
var menuStr = "";
|
|
if (File.Exists(realFilePath))
|
|
{
|
|
menuStr = File.ReadAllText(realFilePath);
|
|
}
|
|
else
|
|
{
|
|
menuStr = File.ReadAllText(Server.MapPath("~/App_Data/" + fileName));
|
|
}
|
|
var menus = JsonConvert.DeserializeObject<List<MenuItem>>(menuStr);
|
|
var seq = 0;
|
|
PreProcessMenus(menus, 0, ref seq);
|
|
return menus;
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
LogFactory.GetLogger<MenuItem>().Error(exp);
|
|
return new List<MenuItem>();
|
|
}
|
|
}
|
|
|
|
private static void PreProcessMenus(List<MenuItem> items, int level, ref int seq)
|
|
{
|
|
if (items == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var item in items)
|
|
{
|
|
seq++;
|
|
item.Level = level;
|
|
if (string.IsNullOrEmpty(item.Icon))
|
|
{
|
|
item.Icon = "fa fa-caret-right";
|
|
}
|
|
var url = item.Url;
|
|
if (string.IsNullOrWhiteSpace(url) || url.StartsWith("#"))
|
|
{
|
|
url = string.Empty;
|
|
}
|
|
else if (url.StartsWith("http:"))
|
|
{
|
|
|
|
}
|
|
else if (!url.StartsWith("/"))
|
|
{
|
|
url = "/" + item.Url;
|
|
}
|
|
item.Url = url;
|
|
item.Id = seq.ToString();
|
|
PreProcessMenus(item.SubItems, level + 1, ref seq);
|
|
}
|
|
}
|
|
}
|
|
} |