Exemplo n.º 1
0
        /// <summary>
        /// 搜索
        /// </summary>
        public ActionResult Search()
        {
            //搜索词
            string keyword = WebHelper.GetQueryString("keyword");

            WorkContext.SearchWord = WebHelper.HtmlEncode(keyword);
            if (keyword.Length == 0)
            {
                return(PromptView(WorkContext.UrlReferrer, "请输入搜索词"));
            }
            if (!SecureHelper.IsSafeSqlString(keyword))
            {
                return(PromptView(WorkContext.UrlReferrer, "您搜索的商品不存在"));
            }

            //异步保存搜索历史
            Asyn.UpdateSearchHistory(WorkContext.Uid, keyword);

            //判断搜索词是否为分类名称,如果是则重定向到分类页面
            int cateId = Categories.GetCateIdByName(keyword);

            if (cateId > 0)
            {
                return(Redirect(Url.Action("category", new RouteValueDictionary {
                    { "cateId", cateId }
                })));
            }
            else
            {
                cateId = WebHelper.GetQueryInt("cateId");
            }

            //分类列表
            List <CategoryInfo> categoryList = null;
            //分类信息
            CategoryInfo categoryInfo = null;
            //品牌列表
            List <BrandInfo> brandList = null;

            //品牌id
            int brandId = Brands.GetBrandIdByName(keyword);

            if (brandId > 0)//当搜索词为品牌名称时
            {
                //获取品牌相关的分类
                categoryList = Brands.GetBrandCategoryList(brandId);

                //由于搜索结果的展示是以分类为基础的,所以当分类不存在时直接将搜索结果设为“搜索的商品不存在”
                if (categoryList.Count == 0)
                {
                    return(PromptView(WorkContext.UrlReferrer, "您搜索的商品不存在"));
                }

                if (cateId > 0)
                {
                    categoryInfo = Categories.GetCategoryById(cateId);
                }
                else
                {
                    //当没有进行分类的筛选时,将分类列表中的首项设为当前选中的分类
                    categoryInfo = categoryList[0];
                    cateId       = categoryInfo.CateId;
                }
                brandList = new List <BrandInfo>();
                brandList.Add(Brands.GetBrandById(brandId));
            }
            else//当搜索词为商品关键词时
            {
                //获取商品关键词相关的分类
                categoryList = Searches.GetCategoryListByKeyword(keyword);

                //由于搜索结果的展示是以分类为基础的,所以当分类不存在时直接将搜索结果设为“搜索的商品不存在”
                if (categoryList.Count == 0)
                {
                    return(PromptView(WorkContext.UrlReferrer, "您搜索的商品不存在"));
                }

                if (cateId > 0)
                {
                    categoryInfo = Categories.GetCategoryById(cateId);
                }
                else
                {
                    categoryInfo = categoryList[0];
                    cateId       = categoryInfo.CateId;
                }
                //根据商品关键词获取分类相关的品牌
                brandList = Searches.GetCategoryBrandListByKeyword(cateId, keyword);
                if (brandList.Count == 0)
                {
                    return(PromptView(WorkContext.UrlReferrer, "您搜索的商品不存在"));
                }
                brandId = WebHelper.GetQueryInt("brandId");
            }
            //最后再检查一遍分类是否存在
            if (categoryInfo == null)
            {
                return(PromptView(WorkContext.UrlReferrer, "您搜索的商品不存在"));
            }

            //筛选价格
            int filterPrice = WebHelper.GetQueryInt("filterPrice");
            //筛选属性
            string filterAttr = WebHelper.GetQueryString("filterAttr");
            //是否只显示有货
            int onlyStock = WebHelper.GetQueryInt("onlyStock");
            //排序列
            int sortColumn = WebHelper.GetQueryInt("sortColumn");
            //排序方向
            int sortDirection = WebHelper.GetQueryInt("sortDirection");
            //当前页数
            int page = WebHelper.GetQueryInt("page");

            //分类筛选属性及其值列表
            List <KeyValuePair <AttributeInfo, List <AttributeValueInfo> > > cateAAndVList = Categories.GetCategoryFilterAAndVList(cateId);

            //分类价格范围列表
            string[] catePriceRangeList = StringHelper.SplitString(categoryInfo.PriceRange, "\r\n");

            //筛选属性处理
            List <int> attrValueIdList = new List <int>();

            string[] filterAttrValueIdList = StringHelper.SplitString(filterAttr, "-");
            if (filterAttrValueIdList.Length != cateAAndVList.Count)//当筛选属性和分类的筛选属性数目不对应时,重置筛选属性
            {
                if (cateAAndVList.Count == 0)
                {
                    filterAttr = "0";
                }
                else
                {
                    int           count = cateAAndVList.Count;
                    StringBuilder sb    = new StringBuilder();
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append("0-");
                    }
                    filterAttr = sb.Remove(sb.Length - 1, 1).ToString();
                }
            }
            else
            {
                foreach (string attrValueId in filterAttrValueIdList)
                {
                    int temp = TypeHelper.StringToInt(attrValueId);
                    if (temp > 0)
                    {
                        attrValueIdList.Add(temp);
                    }
                }
            }


            //分页对象
            PageModel pageModel = new PageModel(20, page, Searches.GetSearchProductCount(keyword, cateId, brandId, filterPrice, catePriceRangeList, attrValueIdList, onlyStock));
            //视图对象
            SearchModel model = new SearchModel()
            {
                Word               = keyword,
                CateId             = cateId,
                BrandId            = brandId,
                FilterPrice        = filterPrice,
                FilterAttr         = filterAttr,
                OnlyStock          = onlyStock,
                SortColumn         = sortColumn,
                SortDirection      = sortDirection,
                CategoryList       = categoryList,
                CategoryInfo       = categoryInfo,
                BrandList          = brandList,
                CatePriceRangeList = catePriceRangeList,
                AAndVList          = cateAAndVList,
                PageModel          = pageModel,
                ProductList        = Searches.SearchProducts(pageModel.PageSize, pageModel.PageNumber, keyword, cateId, brandId, filterPrice, catePriceRangeList, attrValueIdList, onlyStock, sortColumn, sortDirection)
            };

            return(View(model));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 品牌
        /// </summary>
        /// <returns></returns>
        public ActionResult Brand()
        {
            //分类id
            int cateId = GetRouteInt("cateId");

            if (cateId == 0)
            {
                cateId = WebHelper.GetQueryInt("cateId");
            }
            //品牌id
            int brandId = GetRouteInt("brandId");

            if (brandId == 0)
            {
                brandId = WebHelper.GetQueryInt("brandId");
            }

            //筛选价格
            int filterPrice = GetRouteInt("filterPrice");

            if (filterPrice == 0)
            {
                filterPrice = WebHelper.GetQueryInt("filterPrice");
            }

            //筛选属性
            string filterAttr = GetRouteString("filterAttr");

            if (filterAttr.Length == 0)
            {
                filterAttr = WebHelper.GetQueryString("filterAttr");
            }

            //是否只显示有货
            int onlyStock = GetRouteInt("onlyStock");

            if (onlyStock == 0)
            {
                onlyStock = WebHelper.GetQueryInt("onlyStock");
            }

            //排序列
            int sortColumn = GetRouteInt("sortColumn");

            if (sortColumn == 0)
            {
                sortColumn = WebHelper.GetQueryInt("sortColumn");
            }
            //排序方向
            int sortDirection = GetRouteInt("sortDirection");

            if (sortDirection == 0)
            {
                sortDirection = WebHelper.GetQueryInt("sortDirection");
            }
            //当前页数
            int page = GetRouteInt("page");

            if (page == 0)
            {
                page = WebHelper.GetQueryInt("page");
            }

            //品牌信息
            BrandInfo brandInfo = Brands.GetBrandById(brandId);

            if (brandInfo == null)
            {
                return(PromptView("/", "此品牌不存在"));
            }

            //品牌关联分类列表
            List <CategoryInfo> categoryList = Brands.GetBrandCategoryList(brandId);

            //分类信息
            CategoryInfo categoryInfo = null;
            //分类筛选属性及其值列表
            List <KeyValuePair <AttributeInfo, List <AttributeValueInfo> > > cateAAndVList = new List <KeyValuePair <AttributeInfo, List <AttributeValueInfo> > >();

            //分类价格范围列表
            string[] catePriceRangeList = new string[0];
            //筛选属性处理
            List <int> attrValueIdList = new List <int>();

            if (cateId > 0)
            {
                categoryInfo = Categories.GetCategoryById(cateId);
                //分类筛选属性及其值列表
                cateAAndVList = Categories.GetCategoryFilterAAndVList(cateId);
                //分类价格范围列表
                catePriceRangeList = StringHelper.SplitString(categoryInfo.PriceRange, "\r\n");

                //筛选属性处理
                string[] filterAttrValueIdList = StringHelper.SplitString(filterAttr, "-");
                if (filterAttrValueIdList.Length != cateAAndVList.Count)//当筛选属性和分类的筛选属性数目不对应时,重置筛选属性
                {
                    if (cateAAndVList.Count == 0)
                    {
                        filterAttr = "0";
                    }
                    else
                    {
                        int           count = cateAAndVList.Count;
                        StringBuilder sb    = new StringBuilder();
                        for (int i = 0; i < count; i++)
                        {
                            sb.Append("0-");
                        }
                        filterAttr = sb.Remove(sb.Length - 1, 1).ToString();
                    }
                }
                else
                {
                    foreach (string attrValueId in filterAttrValueIdList)
                    {
                        int temp = TypeHelper.StringToInt(attrValueId);
                        if (temp > 0)
                        {
                            attrValueIdList.Add(temp);
                        }
                    }
                }
            }

            //分页对象
            PageModel pageModel = new PageModel(15, page, Products.GetBrandProductCount(cateId, brandId, filterPrice, catePriceRangeList, attrValueIdList, onlyStock))
            {
                Anchor = "Cdt"
            };
            //视图对象
            BrandModel model = new BrandModel()
            {
                BrandId            = brandId,
                CateId             = cateId,
                FilterPrice        = filterPrice,
                FilterAttr         = filterAttr,
                OnlyStock          = onlyStock,
                SortColumn         = sortColumn,
                SortDirection      = sortDirection,
                BrandInfo          = brandInfo,
                CategoryInfo       = categoryInfo,
                CategoryList       = categoryList,
                CatePriceRangeList = catePriceRangeList,
                AAndVList          = cateAAndVList,
                PageModel          = pageModel,
                ProductList        = Products.GetBrandProductList(pageModel.PageSize, pageModel.PageNumber, cateId, brandId, filterPrice, catePriceRangeList, attrValueIdList, onlyStock, sortColumn, sortDirection)
            };

            return(View(model));
        }