菜单树
//菜单树 public async Task<CommonResponse> GetPermissions() { var result = new CommonResponse(); var permissionsList = await _repository.QueryAsync<PermissionEntity>(d => d.Is_Deleted == false); var rootNode = new JArray(); GetPermission(permissionsList, 0, "", rootNode); result.SetSuccess(rootNode); return result; } public void GetPermission(List<PermissionEntity> permissions, int index, string pid, JArray root) { if (index >= permissions.Count) { return; } var entity = permissions[index]; if (!string.IsNullOrEmpty(pid) && pid != "0") //子菜单 { entity = permissions.Where(d => d.Id == pid).FirstOrDefault(); } else if (index == 0 && entity.Pid != "0") //首次不是一级菜单 { GetPermission(permissions, ++index, "", root); return; } var node = new JObject(); node.Add("id", entity.Id); node.Add("path", entity.Path); node.Add("name", entity.Name); node.Add("component", entity.Component); if (!string.IsNullOrEmpty(entity.Redirect)) { node.Add("redirect", entity.Redirect); } var node2 = new JObject(); node2.Add("title", entity.Title); node2.Add("icon", entity.Icon); node.Add("meta", node2); node.Add("ability", new JArray()); var childeData = new JArray(); var childeIDs = permissions.Where(d => d.Pid == entity.Id).Select(d => d.Id).ToList(); if (childeIDs.Count > 0) { foreach (var childID in childeIDs) { GetPermission(permissions, 0, childID, childeData); } node.Add("children", childeData); } root.Add(node); var currentIndex = permissions.IndexOf(entity); permissions.RemoveAt(currentIndex); //删除已添加节点项 if (!string.IsNullOrEmpty(pid) && pid != "0" && childeIDs.Count == 0) //子菜单下没有子菜单了 return; GetPermission(permissions, currentIndex, "", root); }