Example #1
0
        /// <summary>
        /// 搜索
        /// </summary>
        public ActionResult AjaxSearch()
        {
            //搜索词
            string keyword = WebHelper.GetQueryString("keyword");

            if (keyword.Length == 0)
            {
                return(Content(""));
            }
            if (!SecureHelper.IsSafeSqlString(keyword))
            {
                return(Content(""));
            }

            //分类id
            int cateId = WebHelper.GetQueryInt("cateId");
            //品牌id
            int brandId = WebHelper.GetQueryInt("brandId");
            //筛选价格
            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");

            //分类信息
            CategoryInfo categoryInfo = Categories.GetCategoryById(cateId);

            //分类价格范围列表
            string[] catePriceRangeList = StringHelper.SplitString(categoryInfo.PriceRange, "\r\n");
            //筛选属性处理
            List <int> attrValueIdList = new List <int>();

            string[] filterAttrValueIdList = StringHelper.SplitString(filterAttr, "-");
            foreach (string attrValueId in filterAttrValueIdList)
            {
                int temp = TypeHelper.StringToInt(attrValueId);
                if (temp > 0)
                {
                    attrValueIdList.Add(temp);
                }
            }

            //分页对象
            PageModel pageModel = new PageModel(20, page, Searches.GetSearchMallProductCount(keyword, cateId, brandId, filterPrice, catePriceRangeList, attrValueIdList, onlyStock));
            //视图对象
            AjaxMallSearchModel model = new AjaxMallSearchModel()
            {
                PageModel   = pageModel,
                ProductList = Searches.SearchMallProducts(pageModel.PageSize, pageModel.PageNumber, keyword, cateId, brandId, filterPrice, catePriceRangeList, attrValueIdList, onlyStock, sortColumn, sortDirection)
            };

            return(Json(model, JsonRequestBehavior.AllowGet));
        }
Example #2
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.GetSearchMallProductCount(keyword, cateId, brandId, filterPrice, catePriceRangeList, attrValueIdList, onlyStock));
            //视图对象
            MallSearchModel model = new MallSearchModel()
            {
                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.SearchMallProducts(pageModel.PageSize, pageModel.PageNumber, keyword, cateId, brandId, filterPrice, catePriceRangeList, attrValueIdList, onlyStock, sortColumn, sortDirection)
            };

            return(View(model));
        }
Example #3
0
        public ActionResult CategorySearch()
        {
            //搜索词
            string keyword = GetRouteString("keyword");

            if (string.IsNullOrEmpty(keyword))
            {
                keyword = WebHelper.GetQueryString("keyword");
            }
            //搜索关联匹配类型
            int searchKeyType = GetRouteInt("searchKeyType");

            if (searchKeyType == 0)
            {
                searchKeyType = WebHelper.GetQueryInt("searchKeyType");
            }
            //搜索关联匹配对对象id
            int searchKeyId = GetRouteInt("searchKeyId");

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

            //分类id
            int cateId = GetRouteInt("cateId");

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

            if (brandId == 0)
            {
                brandId = WebHelper.GetQueryInt("brandId");
            }
            ////专场id
            //int specialId = GetRouteInt("specialId");
            //if (specialId == 0)
            //    specialId = WebHelper.GetQueryInt("specialId");
            //筛选属性
            string filterAttr = GetRouteString("filterAttr");

            if (filterAttr.Length == 0)
            {
                filterAttr = WebHelper.GetQueryString("filterAttr");
            }
            //是否只显示有货
            int onlyStock = GetRouteInt("onlyStock");

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


            if (searchKeyType <= 0 || searchKeyId <= 0)
            {
                return(PromptView(WorkContext.UrlReferrer, "您搜索的商品不存在"));
            }


            //可选商品id列表
            List <int> productIdList = new List <int>();
            //可选分类列表
            List <CategoryInfo> categoryList = new List <CategoryInfo>();
            //选中的分类信息
            List <CategoryInfo> categoryInfo = new List <CategoryInfo>();
            //可选品牌列表
            List <BrandInfo> brandList = new List <BrandInfo>();
            //可选属性列表
            List <KeyValuePair <AttributeInfo, List <AttributeValueInfo> > > cateAAndVList = new List <KeyValuePair <AttributeInfo, List <AttributeValueInfo> > >();

            bool isValid = false;

            //分类跳转
            if (searchKeyType == (int)ProductKeyEnum.Category)
            {
                isValid = GetProductFliterListForCate(ref productIdList, ref categoryList, ref categoryInfo, ref brandList, ref cateAAndVList,
                                                      searchKeyType, searchKeyId, cateId, brandId, filterAttr, onlyStock);
            }
            else
            {
                isValid = GetProductFliterList(ref productIdList, ref categoryList, ref categoryInfo, ref brandList, ref cateAAndVList,
                                               searchKeyType, searchKeyId, cateId, brandId, filterAttr, onlyStock);
            }

            if (!isValid)
            {
                return(PromptView(WorkContext.UrlReferrer, "您搜索的商品不存在"));
            }
            if (categoryInfo == null || categoryInfo.Count == 0)
            {
                return(PromptView("/", "此分类不存在"));
            }

            //排序列
            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");
            }



            ////筛选价格
            //int filterPrice = GetRouteInt("filterPrice");
            //if (filterPrice == 0)
            //    filterPrice = WebHelper.GetQueryInt("filterPrice");


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


            //分页对象
            PageModel pageModel = new PageModel(20, page, (productIdList == null || productIdList.Count == 0) ? 0 : Searches.GetSearchMallProductCount(productIdList));
            //视图对象
            MallSearchModel model = new MallSearchModel()
            {
                Word          = keyword,
                SearchKeyType = searchKeyType,
                SearchKeyId   = searchKeyId,
                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 = (productIdList == null || productIdList.Count == 0) ? new List <StoreProductInfo>() : Searches.SearchMallProducts(productIdList, pageModel.PageSize, pageModel.PageNumber, sortColumn, sortDirection)
            };

            return(View("search", model));
        }