/// <summary> /// 得到用户一级菜单 /// </summary> /// <param name="userId">用户ID</param> /// <returns></returns> public string GetUserMenuHtml(Guid userId) { DataTable appDt = menuData.GetMenuAppDataTable(); if (appDt.Rows.Count == 0) { return(""); } List <Model.MenuUser> menuUsers = new MenuUser().GetAll(); string sourceMember = string.Empty; StringBuilder menuSb = new StringBuilder(); //加载一级菜单 var root = appDt.Select(string.Format("ParentId='{0}'", Guid.Empty.ToString())); if (root.Length == 0) { return(menuSb.ToString()); } //多语言从不同的字段读取标题 string language = Tools.GetCurrentLanguage(); string titleKey = language.Equals("en-US") ? "Title_en" : language.Equals("zh") ? "Title_zh" : "Title"; #region 得到用户快捷菜单 var shortcuts = new UserShortcut().GetListByUserId(userId); if (shortcuts.Count > 0) { foreach (var shortcut in shortcuts) { string params1 = string.Empty; if (!HasUse(shortcut.MenuId, userId, menuUsers, out sourceMember, out params1)) { continue; } var menudts = appDt.Select(string.Format("Id='{0}'", shortcut.MenuId.ToString())); if (menudts.Length > 0) { DataRow dr = menudts[0]; string icoColor = dr["IcoColor"].ToString(); var childs = appDt.Select("ParentID='" + shortcut.MenuId.ToString() + "'"); bool hasChilds = false; foreach (var child in childs) { if (HasUse(child["Id"].ToString().ToGuid(), userId, menuUsers, out sourceMember, out params1)) { hasChilds = true; break; } } menuSb.Append("<div class=\"menulistdiv\" onclick=\"menuClick(this);\" data-id=\"" + dr["Id"].ToString().ToUpper() + "\" data-title=\"" + dr[titleKey].ToString().Trim() + "\" data-model=\"" + dr["OpenMode"].ToString() + "\" data-width=\"" + dr["Width"].ToString() + "\" data-height=\"" + dr["Height"].ToString() + "\" data-childs=\"" + (hasChilds ? "1" : "0") + "\" data-url=\"" + GetAddress(dr, params1) + "\" data-parent=\"" + Guid.Empty.ToString() + "\" style=\"" + (icoColor.IsNullOrEmpty() ? "" : "color:" + icoColor.Trim1() + ";") + "\">"); menuSb.Append("<div class=\"menulistdiv1\">"); string appIco = dr["Ico"].ToString(); if (appIco.IsNullOrEmpty()) { menuSb.Append("<i class=\"fa fa-th-list\"></i>"); } else if (appIco.IsFontIco()) { menuSb.Append("<i class=\"fa " + appIco + "\"></i>"); } else { menuSb.Append("<img src=\"" + appIco + "\" style=\"vertical-align:middle\" alt=\"\"/>"); } menuSb.Append("<span style=\"vertical-align:middle\">" + dr[titleKey].ToString().Trim1() + "</span>"); menuSb.Append("</div>"); if (hasChilds) { menuSb.Append("<div class=\"menulistdiv2\"><i class=\"fa fa-angle-left\"></i></div>"); } menuSb.Append("</div>"); } } } #endregion var apps = appDt.Select(string.Format("ParentId='{0}'", root[0]["Id"].ToString())); for (int i = 0; i < apps.Length; i++) { string params1 = string.Empty; DataRow dr = apps[i]; if (!HasUse(dr["Id"].ToString().ToGuid(), userId, menuUsers, out sourceMember, out params1)) { continue; } var childs = appDt.Select("ParentId='" + dr["Id"].ToString() + "'"); bool hasChilds = false; foreach (var child in childs) { if (HasUse(child["Id"].ToString().ToGuid(), userId, menuUsers, out sourceMember, out params1)) { hasChilds = true; break; } } string icoColor = dr["IcoColor"].ToString(); menuSb.Append("<div class=\"menulistdiv\" onclick=\"menuClick(this);\" data-id=\"" + dr["Id"].ToString().ToUpper() + "\" data-title=\"" + dr[titleKey].ToString().Trim() + "\" data-model=\"" + dr["OpenMode"].ToString() + "\" data-width=\"" + dr["Width"].ToString() + "\" data-height=\"" + dr["Height"].ToString() + "\" data-childs=\"" + (hasChilds ? "1" : "0") + "\" data-url=\"" + GetAddress(dr, params1) + "\" data-parent=\"" + Guid.Empty.ToString() + "\" style=\"" + (icoColor.IsNullOrEmpty() ? "" : "color:" + icoColor.Trim1() + ";") + "\">"); menuSb.Append("<div class=\"menulistdiv1\">"); string appIco = dr["Ico"].ToString(); if (appIco.IsNullOrEmpty()) { menuSb.Append("<i class=\"fa fa-th-list\"></i>"); } else if (appIco.IsFontIco()) { menuSb.Append("<i class=\"fa " + appIco + "\"></i>"); } else { menuSb.Append("<img src=\"" + appIco + "\" style=\"vertical-align:middle\" alt=\"\"/>"); } menuSb.Append("<span style=\"vertical-align:middle\">" + dr[titleKey].ToString().Trim1() + "</span>"); menuSb.Append("</div>"); if (hasChilds) { menuSb.Append("<div class=\"menulistdiv2\"><i class=\"fa fa-angle-left\"></i></div>"); } menuSb.Append("</div>"); } return(menuSb.ToString()); }
/// <summary> /// 得到菜单JSON(树型) /// </summary> /// <param name="userID"></param> /// <param name="rootDir"></param> /// <param name="showSource">是否显示菜单来源(在查看人员菜单设置时用到)</param> /// <returns></returns> public string GetUserMenuJsonString(Guid userID, string rootDir = "", bool showSource = false) { Menu Menu = new Menu(); AppLibrary Applibary = new AppLibrary(); DataTable appDt = menuData.GetMenuAppDataTable(); if (appDt.Rows.Count == 0) { return("[]"); } var root = appDt.Select(string.Format("ParentId='{0}'", Guid.Empty.ToString())); if (root.Length == 0) { return("[]"); } List <Model.MenuUser> menuusers = new MenuUser().GetAll(); var apps = appDt.Select(string.Format("ParentId='{0}'", root[0]["Id"].ToString())); StringBuilder json = new StringBuilder("", 1000); DataRow rootDr = root[0]; string params0 = string.Empty; //var menu0 = menuusers.Find(p => p.MenuID == rootDr["ID"].ToString().ToGuid() && p.SubPageID == Guid.Empty && p.Users.Contains(userID.ToString(), StringComparison.CurrentCultureIgnoreCase)); //if (menu0 != null) //{ // params0 = menu0.Params; //} //多语言从不同的字段读取标题 string language = Tools.GetCurrentLanguage(); string titleKey = language.Equals("en-US") ? "Title_en" : language.Equals("zh") ? "Title_zh" : "Title"; json.Append("{"); json.AppendFormat("\"id\":\"{0}\",", rootDr["Id"].ToString()); json.AppendFormat("\"title\":\"{0}\",", rootDr[titleKey].ToString().Trim()); json.AppendFormat("\"ico\":\"{0}\",", rootDr["Ico"].ToString()); json.AppendFormat("\"color\":\"{0}\",", rootDr["IcoColor"].ToString()); json.AppendFormat("\"link\":\"{0}\",", GetAddress(rootDr).ToString(), params0); json.AppendFormat("\"model\":\"{0}\",", rootDr["OpenMode"].ToString()); json.AppendFormat("\"width\":\"{0}\",", rootDr["Width"].ToString()); json.AppendFormat("\"height\":\"{0}\",", rootDr["Height"].ToString()); json.AppendFormat("\"hasChilds\":\"{0}\",", apps.Length > 0 ? "1" : "0"); json.AppendFormat("\"childs\":["); StringBuilder json1 = new StringBuilder(apps.Length * 100); string sourceMember = string.Empty; #region 加载个人快捷方式 if (!showSource) { var shortcuts = new UserShortcut().GetListByUserId(userID); if (shortcuts.Count > 0) { json1.Append("{"); json1.AppendFormat("\"id\":\"{0}\",", Guid.NewGuid()); json1.AppendFormat("\"title\":\"{0}\",", "快捷菜单"); json1.AppendFormat("\"ico\":\"{0}\",", ""); json1.AppendFormat("\"color\":\"{0}\",", ""); json1.AppendFormat("\"link\":\"{0}\",", ""); json1.AppendFormat("\"model\":\"{0}\",", ""); json1.AppendFormat("\"width\":\"{0}\",", ""); json1.AppendFormat("\"height\":\"{0}\",", ""); json1.AppendFormat("\"hasChilds\":\"1\","); json1.AppendFormat("\"childs\":["); StringBuilder jsonShortcut = new StringBuilder(); foreach (var shortcut in shortcuts) { string params1 = string.Empty; var menu = menuusers.FindAll(p => p.MenuId == shortcut.MenuId && p.Users.ContainsIgnoreCase(userID.ToString())); if (menu.Count > 0) { StringBuilder psb = new StringBuilder(); foreach (var m in menu.FindAll(p => !p.Params.IsNullOrEmpty()).GroupBy(p => p.Params)) { psb.Append(m.Key.Trim1()); psb.Append("&"); } params1 = psb.ToString().TrimEnd('&'); } if (!HasUse(shortcut.MenuId, userID, menuusers, out sourceMember, out params1, showSource)) { continue; } var menudts = appDt.Select(string.Format("ID='{0}'", shortcut.MenuId.ToString())); if (menudts.Length > 0) { DataRow dr = menudts[0]; var childs = appDt.Select("ParentID='" + dr["ID"].ToString() + "'"); jsonShortcut.Append("{"); jsonShortcut.AppendFormat("\"id\":\"{0}\",", dr["ID"].ToString()); jsonShortcut.AppendFormat("\"title\":\"{0}\",", dr[titleKey].ToString().Trim1()); jsonShortcut.AppendFormat("\"ico\":\"{0}\",", dr["Ico"].ToString()); jsonShortcut.AppendFormat("\"color\":\"{0}\",", dr["IcoColor"].ToString()); jsonShortcut.AppendFormat("\"link\":\"{0}\",", GetAddress(dr, params1)); jsonShortcut.AppendFormat("\"model\":\"{0}\",", dr["OpenMode"].ToString()); jsonShortcut.AppendFormat("\"width\":\"{0}\",", dr["Width"].ToString()); jsonShortcut.AppendFormat("\"height\":\"{0}\",", dr["Height"].ToString()); jsonShortcut.AppendFormat("\"hasChilds\":\"{0}\",", childs.Length > 0 ? "1" : "0"); jsonShortcut.AppendFormat("\"childs\":[]"); jsonShortcut.Append("},"); } } json1.Append(jsonShortcut.Length > 0 ? jsonShortcut.ToString().TrimEnd(',') : ""); json1.Append("]"); json1.Append("}"); if (apps.Length > 0) { json1.Append(","); } } } #endregion for (int i = 0; i < apps.Length; i++) { string params1 = string.Empty; DataRow dr = apps[i]; if (!HasUse(dr["Id"].ToString().ToGuid(), userID, menuusers, out sourceMember, out params1, showSource)) { continue; } var childs = appDt.Select("ParentId='" + dr["Id"].ToString() + "'"); bool hasChilds = false; foreach (var child in childs) { if (HasUse(child["ID"].ToString().ToGuid(), userID, menuusers, out sourceMember, out params1, showSource)) { hasChilds = true; break; } } json1.Append("{"); json1.AppendFormat("\"id\":\"{0}\",", dr["ID"].ToString()); json1.AppendFormat("\"title\":\"{0}{1}\",", dr[titleKey].ToString().Trim1(), showSource ? "<span style='margin-left:4px;color:#666;'>[" + sourceMember + "]</span>" : ""); json1.AppendFormat("\"ico\":\"{0}\",", dr["Ico"].ToString()); json1.AppendFormat("\"color\":\"{0}\",", dr["IcoColor"].ToString()); json1.AppendFormat("\"link\":\"{0}\",", GetAddress(dr, params1)); json1.AppendFormat("\"model\":\"{0}\",", dr["OpenMode"].ToString()); json1.AppendFormat("\"width\":\"{0}\",", dr["Width"].ToString()); json1.AppendFormat("\"height\":\"{0}\",", dr["Height"].ToString()); json1.AppendFormat("\"hasChilds\":\"{0}\",", hasChilds ? "1" : "0"); json1.AppendFormat("\"childs\":["); json1.Append("]"); json1.Append("}"); json1.Append(","); } json.Append(json1.ToString().TrimEnd(',')); json.Append("]"); json.Append("}"); return(json.ToString()); }