Beispiel #1
0
        /// <summary>
        /// 解析搜索地址
        /// </summary>
        /// <param name="webArgs"></param>
        /// <returns></returns>
        public override ResolvedSearchUrlWithParas ResolveSearchUrl(BaseFetchWebPageArgument webArgs)
        {
            ResolvedSearchUrlWithParas resultUrl = new ResolvedSearchUrlWithParas();

            resultUrl.ParasPost = new Dictionary <string, object> ();
            var urlParaContainer = resultUrl.ParasPost;

            //1 查询品牌
            var brandParaModel = new VipSearchParaBrand(webArgs.KeyWord);

            urlParaContainer.Add("para_brand", JsonConvert.SerializeObject(brandParaModel));
            // 2 查询分类
            var categoryTreeParaModel = new VipSearchParaCategoryTree(webArgs.KeyWord);

            urlParaContainer.Add("para_categoryTree", JsonConvert.SerializeObject(categoryTreeParaModel));

            //3检索内容
            var searchListParaModel = new VipSearchParaSearchList(webArgs.KeyWord);

            //分页
            searchListParaModel.paramsDetails.np = webArgs.PageIndex + 1;
            //排序
            int tempSort = 0;

            int.TryParse(webArgs.OrderFiled.FieldValue, out tempSort);
            searchListParaModel.paramsDetails.sort = tempSort;
            //品牌
            if (null != webArgs.Brands && webArgs.Brands.Any())
            {
                searchListParaModel.paramsDetails.brand_store_sn = string.Join(",", webArgs.Brands.Select(x => x.BrandId));
            }
            //分类+规格
            if (null != webArgs.TagGroup)
            {
                //分类
                var category_id_1_5_show = webArgs.TagGroup.Tags.Where(x => x.FilterFiled == "category_id_1_5_show");
                searchListParaModel.paramsDetails.category_id_1_5_show = string.Join(",", category_id_1_5_show.Select(x => x.Value));
                var category_id_1_show = webArgs.TagGroup.Tags.Where(x => x.FilterFiled == "category_id_1_show");
                searchListParaModel.paramsDetails.category_id_1_show = string.Join(",", category_id_1_show.Select(x => x.Value));
                var category_id_2_show = webArgs.TagGroup.Tags.Where(x => x.FilterFiled == "category_id_2_show");
                searchListParaModel.paramsDetails.category_id_2_show = string.Join(",", category_id_2_show.Select(x => x.Value));
                var category_id_3_show = webArgs.TagGroup.Tags.Where(x => x.FilterFiled == "category_id_3_show");
                searchListParaModel.paramsDetails.category_id_3_show = string.Join(",", category_id_3_show.Select(x => x.Value));
                //规格
                var props = webArgs.TagGroup.Tags.Where(x => x.FilterFiled == "props");
                searchListParaModel.paramsDetails.props = string.Join(";", props.Select(x => x.Value));
            }

            urlParaContainer.Add("para_searchList", JsonConvert.SerializeObject(searchListParaModel));

            return(resultUrl);
        }
            public override string LoadUrlGetSearchApiContent(IFetchWebPageArgument queryParas)
            {
                VipSearchResultBag resultBag = new VipSearchResultBag();

                string keyWord = queryParas.KeyWord;

                if (string.IsNullOrEmpty(keyWord))
                {
                    return(string.Empty);
                }

                try
                {
                    //加载Cookie
                    var ckVisitor = new LazyCookieVistor();
                    var cks       = ckVisitor.LoadCookies(VipSiteUrl);



                    string searchUrl = string.Format(templateOfSearchUrl, keyWord);

                    var client = VipHttpClient;
                    client.Client.DefaultRequestHeaders.Referrer = new Uri(string.Format("https://m.vip.com/searchlist.html?q={0}&channel_id=", keyWord));
                    ////加载cookies
                    ////获取当前站点的Cookie
                    client.ChangeGlobleCookies(cks, VipSiteUrl);

                    // 4 发送请求
                    var clientProxy = new HttpServerProxy()
                    {
                        Client = client.Client, KeepAlive = true
                    };

                    //注意:对于响应的内容 不要使用内置的文本 工具打开,这个工具有bug.看到的文本不全面
                    //使用json格式打开即可看到里面所有的字符串
                    Task <HttpResponseMessage> brandTask;
                    Task <HttpResponseMessage> categoryTreeTask;
                    Task <HttpResponseMessage> searchListTask;
                    string para_brandJson        = "";
                    string para_categoryTreeJson = "";
                    string para_searchListJson   = "";
                    if (null != queryParas.ResolvedUrl && null != queryParas.ResolvedUrl.ParasPost)
                    {
                        para_brandJson        = queryParas.ResolvedUrl.ParasPost["para_brand"].ToString();
                        para_categoryTreeJson = queryParas.ResolvedUrl.ParasPost["para_categoryTree"].ToString();
                        para_searchListJson   = queryParas.ResolvedUrl.ParasPost["para_searchList"].ToString();
                    }
                    else
                    {
                        para_brandJson        = new VipSearchParaBrand(keyWord).ToJson();
                        para_categoryTreeJson = new VipSearchParaCategoryTree(keyWord).ToJson();

                        //插件不解析的话,那么使用最简单的基本关键词过滤分页,不进行复杂过滤,复杂过滤筛选应该在插件实现
                        var paraDetais = new VipSearchParaSearchList(keyWord);
                        //分页
                        paraDetais.paramsDetails.np = queryParas.PageIndex + 1;

                        para_searchListJson = paraDetais.ToJson();
                    }
                    if (queryParas.IsNeedResolveHeaderTags == true)
                    {
                        //1 查询品牌
                        var brandPara = new Dictionary <string, string>();
                        brandPara.Add("para_brand", para_brandJson);
                        brandTask = clientProxy.PostRequestTransferAsync(queryBrandUrl, PostDataContentType.Json, brandPara, null);
                        // 2 查询分类
                        var categoryTreePara = new Dictionary <string, string>();
                        categoryTreePara.Add("para_categoryTree", para_categoryTreeJson);
                        categoryTreeTask = clientProxy.PostRequestTransferAsync(queryCategoryUrl, PostDataContentType.Json, categoryTreePara, null);
                    }
                    else
                    {
                        brandTask        = Task.FromResult <HttpResponseMessage>(null);
                        categoryTreeTask = Task.FromResult <HttpResponseMessage>(null);
                    }

                    //3检索内容
                    var searchListPara = new Dictionary <string, string>();
                    searchListPara.Add("para_searchList", para_searchListJson);
                    searchListTask = clientProxy.PostRequestTransferAsync(templateOfSearchUrl, PostDataContentType.Json, searchListPara, null);

                    //等待任务完毕
                    Task.WaitAll(new Task[] { brandTask, categoryTreeTask, searchListTask });
                    if (brandTask.Result != null)
                    {
                        resultBag.BrandStoreList = brandTask.Result.Content.ReadAsStringAsync().Result;
                    }
                    if (categoryTreeTask.Result != null)
                    {
                        resultBag.CategoryTree = categoryTreeTask.Result.Content.ReadAsStringAsync().Result;
                    }
                    if (searchListTask.Result != null)
                    {
                        resultBag.SearchList = searchListTask.Result.Content.ReadAsStringAsync().Result;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return(resultBag.ToJson());
            }