mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2026-06-10 16:57:50 +08:00
【调整】获取商品分类功能,从原先的只获取二级分类,改成递归获取无限级分类。
This commit is contained in:
@@ -35,29 +35,6 @@ namespace CoreCms.Net.Model.ViewModels.DTO
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string imageUrl { get; set; }
|
public string imageUrl { get; set; }
|
||||||
|
|
||||||
public List<WxGoodCategoryChild> child { get; set; }
|
public List<WxGoodCategoryDto>? child { get; set; }
|
||||||
}
|
|
||||||
|
|
||||||
public class WxGoodCategoryChild
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 序列
|
|
||||||
/// </summary>
|
|
||||||
public int id { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 标题
|
|
||||||
/// </summary>
|
|
||||||
public string name { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 排序
|
|
||||||
/// </summary>
|
|
||||||
public int sort { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 图片地址
|
|
||||||
/// </summary>
|
|
||||||
public string imageUrl { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -109,46 +109,39 @@ namespace CoreCms.Net.Web.WebApi.Controllers
|
|||||||
var jm = new WebApiCallBack() { status = true };
|
var jm = new WebApiCallBack() { status = true };
|
||||||
|
|
||||||
var data = await _goodsCategoryServices.QueryListByClauseAsync(p => p.isShow == true, p => p.sort, OrderByType.Asc, true, true);
|
var data = await _goodsCategoryServices.QueryListByClauseAsync(p => p.isShow == true, p => p.sort, OrderByType.Asc, true, true);
|
||||||
var wxGoodCategoryDto = new List<WxGoodCategoryDto>();
|
|
||||||
|
|
||||||
var parents = data.Where(p => p.parentId == 0).ToList();
|
|
||||||
if (parents.Any())
|
|
||||||
{
|
|
||||||
parents.ForEach(p =>
|
|
||||||
{
|
|
||||||
var model = new WxGoodCategoryDto
|
|
||||||
{
|
|
||||||
id = p.id,
|
|
||||||
name = p.name,
|
|
||||||
imageUrl = !string.IsNullOrEmpty(p.imageUrl) ? p.imageUrl : "/static/images/common/empty.png",
|
|
||||||
sort = p.sort
|
|
||||||
};
|
|
||||||
|
|
||||||
var childs = data.Where(p => p.parentId == model.id).ToList();
|
|
||||||
if (childs.Any())
|
|
||||||
{
|
|
||||||
var childsList = new List<WxGoodCategoryChild>();
|
|
||||||
childs.ForEach(o =>
|
|
||||||
{
|
|
||||||
childsList.Add(new WxGoodCategoryChild()
|
|
||||||
{
|
|
||||||
id = o.id,
|
|
||||||
imageUrl = !string.IsNullOrEmpty(o.imageUrl) ? o.imageUrl : "/static/images/common/empty.png",
|
|
||||||
name = o.name,
|
|
||||||
sort = o.sort
|
|
||||||
});
|
|
||||||
});
|
|
||||||
model.child = childsList;
|
|
||||||
}
|
|
||||||
wxGoodCategoryDto.Add(model);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
jm.status = true;
|
jm.status = true;
|
||||||
jm.data = wxGoodCategoryDto;
|
jm.data = GetCategories(data, 0);
|
||||||
|
|
||||||
return jm;
|
return jm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 迭代方法
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="oldCategories"></param>
|
||||||
|
/// <param name="parentId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static List<WxGoodCategoryDto> GetCategories(List<CoreCmsGoodsCategory> oldCategories, int parentId)
|
||||||
|
{
|
||||||
|
List<WxGoodCategoryDto> childTree = new List<WxGoodCategoryDto>();
|
||||||
|
var model = oldCategories.Where(p => p.parentId == parentId).ToList();
|
||||||
|
foreach (var item in model)
|
||||||
|
{
|
||||||
|
var parentTree = new WxGoodCategoryDto();
|
||||||
|
parentTree.id = item.id;
|
||||||
|
parentTree.imageUrl = !string.IsNullOrEmpty(item.imageUrl) ? item.imageUrl : "/static/images/common/empty.png";
|
||||||
|
parentTree.name = item.name;
|
||||||
|
parentTree.sort = item.sort;
|
||||||
|
|
||||||
|
childTree.Add(parentTree);
|
||||||
|
|
||||||
|
parentTree.child = GetCategories(oldCategories, item.id);
|
||||||
|
}
|
||||||
|
return childTree;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region 根据查询条件获取分页数据============================================================
|
#region 根据查询条件获取分页数据============================================================
|
||||||
|
|||||||
Reference in New Issue
Block a user