Пример #1
0
        private async Task<bool> SyncProductList(string where, string url)
        {
            var productList = new List<ProductItem>();
            int page = 1;
            IHtmlDocument xdoc = null;
            ApiPagedResult<List<ProductItem>> pagedList = new ApiPagedResult<List<ProductItem>>();
            while (pagedList.Success && pagedList.HasMore)
            {

                var nextUrl = UrlHelper.SetValue(url, "page", page.ToString());
                using (var helper = new WBHelper(false))
                {
                    try
                    {
                        var result = await this.InvokeTask(helper.SynchronousLoadDocument, nextUrl);
                        while (result.LoginRequired)
                        {
                            this.InvokeAction(ShowLoginWindow, helper.WB);
                            result = await this.InvokeTask(helper.SynchronousLoadDocument, nextUrl);
                        }
                        xdoc = HtmlParser.Parse(result.Data.Body.InnerHtml);
                        var table = xdoc.GetElementById("main").FindFirst(".stock-table");
                        pagedList = ProductItemHelper.GetProductItemList(table, page);
                        tbcpCrumbs = xdoc.FindSingle("#tbcpCrumbs").Attribute("value").AttributeValue;
                        xdoc = null;
                        result = null;
                        SetTaskName("同步{0}商品第{1}页({2}个)", where, page, pagedList.Data.Count);
                        productList.AddRange(pagedList.Data);
                        page++;
                    }
                    catch (Exception ex)
                    {
                        pagedList.Success = false;
                        helper.Dispose();
                        //AppendException(ex);
                    }
                }
                if (!pagedList.Success)
                {
                    AppendText("同步{0}商品第{1}页出错,{2}", where, page, pagedList.Message);
                    if (MessageBox.Show(string.Format("同步{0}商品第{1}页出错,是否重试?", where, page), Text, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        pagedList.Success = true;
                        continue;
                    }
                }

            }
            foreach (var item in productList)
            {
                item.UpdateAt = ProductSyncTime;
                item.Where = where;
                item.原利润 = item.利润 = item.一口价 - item.进价;
            }
            AppDatabase.UpsertProductList(productList);
            BindDGViewProduct();
            SetTaskName("就绪");
            AppendText("同步{0}商品完成!({1}个)", where, productList.Count);
            return true;
        }