From 1fc786d39cb97318298a71507679346a6f060ffe Mon Sep 17 00:00:00 2001 From: JianWeie Date: Fri, 16 Sep 2022 12:24:16 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=90=8E=E5=8F=B0=E5=95=86=E5=93=81=E6=8C=89=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E6=9F=A5=E8=AF=A2=EF=BC=8C=E6=9C=AA=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E4=B8=8B=E7=BA=A7=E5=88=86=E7=B1=BB=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E5=90=8C=E6=97=B6=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=AB=AF=E5=A2=9E=E5=8A=A0=E5=A4=9A=E7=BA=A7=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CoreCms.Net.Utility/Helper/GoodsHelper.cs | 48 ++++++++++++------- .../Good/CoreCmsGoodsController.cs | 12 ++++- .../Controllers/GoodController.cs | 14 ++---- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/CoreCms.Net.Utility/Helper/GoodsHelper.cs b/CoreCms.Net.Utility/Helper/GoodsHelper.cs index 891155fc..de7045a0 100644 --- a/CoreCms.Net.Utility/Helper/GoodsHelper.cs +++ b/CoreCms.Net.Utility/Helper/GoodsHelper.cs @@ -80,6 +80,37 @@ namespace CoreCms.Net.Utility.Helper #endregion + #region 递归获取下级所有序列 + /// + /// 递归获取下级所有序列 + /// + /// + [Description("递归获取下级所有序列")] + public static List GetChildIds(List categories, int parentId) + { + var ids = new List { parentId }; + ids = GetChildrenIds(categories, parentId, ids); + return ids; + } + + /// + /// 迭代方法 + /// + /// + /// + /// + /// + private static List GetChildrenIds(List oldNavs, int parentId, List ids) + { + var model = oldNavs.Where(p => p.parentId == parentId).ToList(); + foreach (var item in model) + { + ids.Add(item.id); + GetChildrenIds(oldNavs, item.id, ids); + } + return ids; + } + #endregion #region 获取可用库存 /// @@ -120,16 +151,9 @@ namespace CoreCms.Net.Utility.Helper return url; } } - #endregion - - #region 小程序端获取编码后的分类集合 - - - #endregion #region 后端判断提交的商品属性值是否符合规则(判断内容,只允许中文,字母,数字,和-,/) - /// /// 判断内容,只允许中文,字母,数字,和-,/ /// @@ -139,17 +163,7 @@ namespace CoreCms.Net.Utility.Helper public static bool FilterChar(string inputValue) { return Regex.IsMatch(inputValue, "[`.~!@#$^&*()=|\"{}':;',\\[\\]<>?~!@#¥……&*&;|{}。*-+]+"); - //return Regex.IsMatch(inputValue, "[`.~!@#$^&*()=|\"{}':;',\\[\\]<>?~!@#¥……&*&;|{}。*-+]+"); - //if (Regex.IsMatch(inputValue, "[A-Za-z0-9\u4e00-\u9fa5-]+")) - //{ - // return Regex.Match(inputValue, "[A-Za-z0-9\u4e00-\u9fa5-]+").Value; - //} - //return ""; - //return Regex.IsMatch(inputValue, "[~!@#$%^&*()_+|<>,.?:;'\\[\\]{}\"]+"); - - //return Regex.IsMatch(inputValue, "[A-Za-z0-9\u4e00-\u9fa5-/]+"); } - #endregion diff --git a/CoreCms.Net.Web.Admin/Controllers/Good/CoreCmsGoodsController.cs b/CoreCms.Net.Web.Admin/Controllers/Good/CoreCmsGoodsController.cs index fd60f369..ea7cecd7 100644 --- a/CoreCms.Net.Web.Admin/Controllers/Good/CoreCmsGoodsController.cs +++ b/CoreCms.Net.Web.Admin/Controllers/Good/CoreCmsGoodsController.cs @@ -172,8 +172,18 @@ namespace CoreCms.Net.Web.Admin.Controllers var selectTreeSelectNodeId = Request.Form["selectTree_select_nodeId"].FirstOrDefault().ObjectToInt(0); if (selectTreeSelectNodeId > 0) @where = @where.And(p => p.goodsCategoryId == selectTreeSelectNodeId); //商品分类ID 关联category.id int + var goodsCategoryId = Request.Form["goodsCategoryId"].FirstOrDefault().ObjectToInt(0); - if (goodsCategoryId > 0) @where = @where.And(p => p.goodsCategoryId == goodsCategoryId); + if (goodsCategoryId > 0) + { + //获取全部数据缓存 + var categories = await _coreCmsGoodsCategoryServices.QueryAsync(true, true); + var ids = GoodsHelper.GetChildIds(categories, goodsCategoryId); + + @where = @where.And(p => ids.Contains(p.goodsCategoryId)); + } + + //商品类别ID 关联goods_type.id int var goodsTypeId = Request.Form["goodsTypeId"].FirstOrDefault().ObjectToInt(0); if (goodsTypeId > 0) @where = @where.And(p => p.goodsTypeId == goodsTypeId); diff --git a/CoreCms.Net.Web.WebApi/Controllers/GoodController.cs b/CoreCms.Net.Web.WebApi/Controllers/GoodController.cs index 5e827036..205b091e 100644 --- a/CoreCms.Net.Web.WebApi/Controllers/GoodController.cs +++ b/CoreCms.Net.Web.WebApi/Controllers/GoodController.cs @@ -25,6 +25,7 @@ using CoreCms.Net.Model.FromBody; using CoreCms.Net.Model.ViewModels.DTO; using CoreCms.Net.Model.ViewModels.UI; using CoreCms.Net.Utility.Extensions; +using CoreCms.Net.Utility.Helper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -195,16 +196,9 @@ namespace CoreCms.Net.Web.WebApi.Controllers className = category.name; } - var childs = await _goodsCategoryServices.QueryListByClauseAsync(p => p.parentId == catId, p => p.sort, OrderByType.Asc, true, true); - if (childs.Any()) - { - var ids = childs.Select(p => p.id).ToList(); - where = where.And(p => ids.Contains(p.goodsCategoryId) || p.goodsCategoryId == catId); - } - else - { - where = where.And(p => p.goodsCategoryId == catId); - } + var categories = await _goodsCategoryServices.QueryAsync(true, true); + var ids = GoodsHelper.GetChildIds(categories, catId); + where = where.And(p => ids.Contains(p.goodsCategoryId)); } } if (!string.IsNullOrWhiteSpace(obj.brandId))