using AllBase; using AllBase.Extension; using AllBase.Helper; using AllBase.Model.Result; using TemuKJD.Extension; using TemuKJD.KJDForms.BaseForms; using NaiBa.Util.Extension; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using Work.BLL.Product; namespace TemuKJD.KJDForms.SalesCenterForms { public partial class GroupBuyShopForm : Form { public List lstProductId { get { return this.dgvProduct.Rows.Cast().Where(r => r.Cells["colSelect"].Value.ParseToBool() == true && r.Visible == true) .Select(r => r.Cells["colProductId"].Value.ToString()).ToList(); } } private CheckBox chkSelectAll = null; DouShopLoginInfo shop = null; public GroupBuyForm groupBuyForm = null; public bool isGetProductSuccess = false; public bool isGetSalesSuccess = false; string _signature = ""; string _timeSpan = ""; public Dictionary dicProductSales = new Dictionary(); public List productList = new List(); public GroupBuyShopForm(DouShopLoginInfo _shop = null) { InitializeComponent(); shop = _shop; } private void GroupBuyShopForm_Load(object sender, EventArgs e) { chkSelectAll = dgvProduct.Columns["colSelect"].AddHeaderCheckBox(HorizontalAlignment.Center, 0, 0, null, null, false); dgvProduct.Columns["colSelect"].HeaderText = ""; chkSelectAll.Width = chkSelectAll.Height = 13; chkSelectAll.CheckedChanged += chkSelectAll_CheckedChanged; } private void chkSelectAll_CheckedChanged(object sender, EventArgs e) { if (!chkSelectAll.Checked) { foreach (DataGridViewRow row in dgvProduct.Rows) { row.Cells["colSelect"].Value = false; } } else { foreach (DataGridViewRow row in dgvProduct.Rows) { row.Cells["colSelect"].Value = true; } } } public void InitGetProduct() { if (shop == null) return; ShowVisitorMessage(""); foreach (DataGridViewRow r in this.dgvProduct.Rows) { r.Cells["colSales"].Value = "获取中"; r.Cells["colStock"].Value = "获取中"; r.Cells["colMessage"].Value = ""; } foreach (var p in productList) { p.sell_num = null; } } public void GetProductSim(string listUrl, string referrerUrl, Dictionary headers, string firstPageData = "", string campaign_start_time = "", string campaign_end_time = "") { ShowVisitorMessage(""); var salesUrl = listUrl; foreach (DataGridViewRow r in this.dgvProduct.Rows) { Invoke(new Action(() => { r.Cells["colSales"].Value = "获取中"; r.Cells["colStock"].Value = "获取中"; r.Cells["colMessage"].Value = ""; })); } foreach (var p in productList) { p.sell_num = null; } bool isGetSalesOver = false; var t1 = Task.Run(() => { int totalx = -1; int totalPagex = 0; int numberx = 0; string resultx = ""; if (!string.IsNullOrEmpty(firstPageData)) resultx = firstPageData; ShowVisitorMessage("正在初始化商品获取"); // 获取有销量的商品 for (int i = 0; i < 10000; i++) { numberx += 20; for (int j = 0; j < 3 && !resultx.Contains("\"st\":0"); j++) { CommonSimRequest simRequest = new CommonSimRequest(); simRequest.Accept = "application/json, text/plain, */*"; if (headers.ContainsKey("Accept")) simRequest.Accept = headers["Accept"]; if (headers.ContainsKey("UserAgent")) simRequest.UserAgent = headers["UserAgent"]; var cookie = shop.Cookies; ShowVisitorMessage("商品销量获取中,第 " + (i + 1) + " 页"); var rUrl = salesUrl.Replace("page=0", "page=" + i).Replace("&pay_type=", "&pay_type=&sell_num_min=1"); // 获取所有有销量的商品,Replace不可注释!! resultx = simRequest.Request_Get(rUrl, referrerUrl, new Dictionary(), cookie); if (j > 0) { Thread.Sleep(Randomx.GetId(250, 750)); Thread.Sleep(Randomx.GetId(250, 750)); } } if (resultx.Contains("\"st\":0") && !groupBuyForm.cancellationTokenSource.IsCancellationRequested) { this.isGetSalesSuccess = true; if (totalx == -1) { totalx = BaseRegex.GetIntJson(ref resultx, "total"); } var jObj = JObject.Parse(resultx); var jArr = jObj["data"] as JArray; foreach (var productObj in jArr) { var prod = JsonHelper.SerializeObject(productObj); var product_id = BaseRegex.GetLongJson(ref prod, "product_id"); var sell_num = BaseRegex.GetIntJson(ref prod, "sell_num"); this.dicProductSales[product_id] = sell_num; } Thread.Sleep(Randomx.GetId(250, 750)); Thread.Sleep(Randomx.GetId(250, 750)); resultx = ""; } else { isGetSalesSuccess = false; ShowVisitorMessage("获取商品销量完成"); isGetSalesOver = true; break; } if (numberx >= totalx) { ShowVisitorMessage("获取商品销量完成"); isGetSalesOver = true; break; } } }); var total = -1; var totalPage = 1; var number = 0; var result = ""; referrerUrl = "https://fxg.jinritemai.com/market/center/group/create?type=1"; DateTime date = DateTime.Now.Date; if (string.IsNullOrEmpty(campaign_start_time)) campaign_start_time = date.ToString("yyyy-MM-dd") + "+00:00:00"; if (string.IsNullOrEmpty(campaign_end_time)) campaign_end_time = date.ToString("yyyy-MM-dd") + "+23:59:59"; _timeSpan = BaseRegex.GetMatchRegexString(ref listUrl, "&_=([\\s\\S]+?)&"); if (string.IsNullOrEmpty(_timeSpan)) _timeSpan = BaseRegex.GetMatchRegexString(ref listUrl, "&_=([\\s\\S]+?)"); if (string.IsNullOrEmpty(_timeSpan)) _timeSpan = BaseClass.DateTimeToTimestampMilliseconds(DateTime.Now).ToString(); _signature = BaseRegex.GetMatchRegexString(ref listUrl, "&_signature=([\\s\\S]+?)&"); if (string.IsNullOrEmpty(_signature)) { var listUrl2 = listUrl + "&"; _signature = BaseRegex.GetMatchRegexString(ref listUrl2, "&_signature=([\\s\\S]+?)&"); } //https://fxg.jinritemai.com/byteshop/promotion/queryAvailableGoods?pageSize=15&page=2&page_size=15&campaign_start_time=2022-06-21+19:14:51&campaign_end_time=2022-06-22+19:14:51&_bid=ecom_market_shop&appid=1&_=1655887384070&_signature=_02B4Z6wo001018eSLdwAAIDCpJjttu0f6uPHlilAAJNfj1ao1TeKBkwmWorJ02UXEJ65nOUW80KDl7BCnks4QRQn4NSiCgbDAn0lDv-DZLnjG2jU-ZN-lFYs9ZV8k8yacuCAeWC7HN1lMk3576"); listUrl = "https://fxg.jinritemai.com/byteshop/promotion/queryAvailableGoods?pageSize=15&page=0&page_size=15&campaign_start_time=" + campaign_start_time + "&campaign_end_time=" + campaign_end_time + "&_bid=ecom_market_shop&appid=1" + "&_=" + _timeSpan + "&_signature=" + _signature; for (int i = 1; i <= groupBuyForm.getPage; i++) { if (i > totalPage) break; result = ""; number += 15; for (int j = 0; j < 3 && !result.Contains("\"st\":0") || result.Contains("\"msg\":\"账号未登录\""); j++) { CommonSimRequest simRequest = new CommonSimRequest(); simRequest.Accept = "application/json, text/plain, */*"; if (headers.ContainsKey("Accept")) simRequest.Accept = headers["Accept"]; if (headers.ContainsKey("UserAgent")) simRequest.UserAgent = headers["UserAgent"]; var cookie = shop.Cookies; var rUrl = listUrl.Replace("page=0", "page=" + i); result = simRequest.Request_Get(rUrl, referrerUrl, new Dictionary(), cookie); if (j > 0) { Thread.Sleep(Randomx.GetId(250, 500)); } ShowVisitorMessage("正在获取商品第" + i + "页"); } if (result.Contains("\"st\":0") && !groupBuyForm.cancellationTokenSource.IsCancellationRequested) { this.isGetProductSuccess = true; if (total == -1) { total = BaseRegex.GetIntJson(ref result, "total"); } totalPage = Math.Ceiling(total / 15M).ParseToInt(); if (totalPage > groupBuyForm.getPage) totalPage = groupBuyForm.getPage; string message = "正在获取商品第 " + i + " 页,共获取 " + totalPage + " 页"; ShowVisitorMessage(""); Invoke(new Action(() => { this.statusBar.lblTaskStatus.Text = message; this.statusBar.pgbTaskProgress.Value = i; this.statusBar.pgbTaskProgress.Text = i + "/" + totalPage; this.statusBar.pgbTaskProgress.Maximum = totalPage; })); try { var jObj = JObject.Parse(result); var pList = JsonHelper.DeserializeJsonToList(jObj["data"]?.ToString()); foreach (var product in pList) { if (product.allow == false) continue; if (dicProductSales.ContainsKey(product.product_id.ParseToLong())) product.sell_num = dicProductSales[product.product_id.ParseToLong()]; else if (isGetSalesOver && isGetSalesSuccess) product.sell_num = 0; else if (isGetSalesOver) product.sell_num = -1; if (this.productList.Any(p => p.product_id == product.product_id)) this.productList.RemoveAll(p => p.product_id == product.product_id); this.productList.Add(product); Invoke(new Action(() => { var rowId = -1; var rowExist = this.dgvProduct.Rows.Cast().FirstOrDefault(r => r.Cells["colProductId"].Value.ToString() == product.product_id); if (rowExist != null) { rowId = rowExist.Index; dgvProduct.Rows.Insert(rowExist.Index, false, product.product_id, product.name, product.stock_num, product.sell_num, product.min_price / 100M + " - " + product.max_price / 100M); dgvProduct.Rows.Remove(rowExist); } else rowId = dgvProduct.Rows.Add(false, product.product_id, product.name, product.stock_num, product.sell_num, product.min_price / 100M + " - " + product.max_price / 100M); // 过滤数据 if (groupBuyForm.keywords?.Count > 0 && !groupBuyForm.keywords.Any(k => product.name.Contains(k) || product.product_id.Contains(k))) dgvProduct.Rows[rowId].Visible = false; if (groupBuyForm.filterWords?.Count > 0 && groupBuyForm.filterWords.Any(k => product.name.Contains(k) || product.product_id.Contains(k))) dgvProduct.Rows[rowId].Visible = false; if (groupBuyForm.priceMin > product.max_price || groupBuyForm.priceMax < product.min_price) dgvProduct.Rows[rowId].Visible = false; if (groupBuyForm.saleMin > product.sell_num || groupBuyForm.saleMax < product.sell_num) dgvProduct.Rows[rowId].Visible = false; })); } Thread.Sleep(Randomx.GetId(250, 500)); } catch (Exception) { isGetProductSuccess = false; break; } } else { isGetProductSuccess = false; break; } if (number >= total) break; } t1.Wait(); // 最后填写商品销量 foreach (var product in this.productList) { if (dicProductSales.ContainsKey(product.product_id.ParseToLong())) product.sell_num = dicProductSales[product.product_id.ParseToLong()]; else if (isGetSalesSuccess) product.sell_num = 0; else product.sell_num = -1; var row = this.dgvProduct.Rows.Cast().FirstOrDefault(r => r.Cells["colProductId"].Value.ToString() == product.product_id); if (row != null && row.Cells["colSales"].Value?.ToString() == "获取中") { if (product.sell_num >= 0) row.Cells["colSales"].Value = product.sell_num; if (product.sell_num >= 0) row.Cells["colSales"].Value = product.sell_num; } } Invoke(new Action(() => { if (groupBuyForm.cancellationTokenSource.IsCancellationRequested) { this.statusBar.lblTaskStatus.Text = "获取中断"; } else if (!isGetSalesSuccess) { this.statusBar.lblTaskStatus.Text = "获取完成,但获取销量信息失败"; } else if (!isGetProductSuccess) { this.statusBar.lblTaskStatus.Text = "获取商品信息失败"; } else { this.statusBar.lblTaskStatus.Text = "获取完成"; } //this.statusBar.pgbTaskProgress.Value = 0; //this.statusBar.pgbTaskProgress.Text = 0 + "/" + 0; //this.statusBar.pgbTaskProgress.Maximum = 0; })); } public void SetSalesCount() { foreach (var key in dicProductSales.Keys) { var row = this.dgvProduct.Rows.Cast().FirstOrDefault(r => r.Cells["colProductId"].Value.ToString() == key.ToString()); if (row != null) { Invoke(new Action(() => { row.Cells["colSales"].Value = dicProductSales[key]; })); } var product = this.productList.Find(p => p.product_id.ToString() == key.ToString()); if (product != null) product.sell_num = dicProductSales[key]; } foreach (DataGridViewRow row in dgvProduct.Rows) { if (row.Cells["colSales"].Value.ToString() == "获取中") { Invoke(new Action(() => { if (isGetSalesSuccess) { row.Cells["colSales"].Value = 0; } else { row.Cells["colSales"].Value = "获取失败"; } })); } } if (isGetSalesSuccess) { foreach (var prod in this.productList) { if (prod.sell_num == null) prod.sell_num = 0; } } } public void ShowVisitorMessage(string msg, string ids = "") { Invoke(new Action(() => { this.label1.Text = msg; if (!string.IsNullOrEmpty(ids)) { var idList = ids.Split(new string[] { "\",\"", "\"" }, StringSplitOptions.RemoveEmptyEntries); var rows = this.dgvProduct.Rows.Cast().Where(r => idList.Contains(r.Cells["colProductId"].Value?.ToString())); foreach (var row in rows) { row.Cells["colMessage"].Value = msg; } } })); } private void dgvProduct_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex < 0 || e.RowIndex < 0) return; string headerText = dgvProduct.Columns[e.ColumnIndex].HeaderText; string columnName = dgvProduct.Columns[e.ColumnIndex].Name; var row = dgvProduct.Rows[e.RowIndex]; switch (columnName) { case "colSelect": break; } } private void label1_Click(object sender, EventArgs e) { } private void splitContainer2_SplitterMoved(object sender, SplitterEventArgs e) { } private void statusBar_Load(object sender, EventArgs e) { } bool isSelecting = false; bool isCheck = true; private void dgvProduct_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { if (e.ColumnIndex < 0 || e.RowIndex < 0) return; var columnName = dgvProduct.Columns[e.ColumnIndex].Name; if (MouseButtons == MouseButtons.Left) { if (columnName == "colSelect") { dgvProduct.Rows[e.RowIndex].Cells["colSelect"].Value = !dgvProduct.Rows[e.RowIndex].Cells["colSelect"].Value.ParseToBool(); isSelecting = true; isCheck = dgvProduct.Rows[e.RowIndex].Cells["colSelect"].Value.ParseToBool(); chkSelectAll.CheckedChanged -= chkSelectAll_CheckedChanged; if (dgvProduct.Rows.Cast().All(r => r.Cells["colSelect"].Value.ParseToBool())) chkSelectAll.Checked = true; else chkSelectAll.Checked = false; chkSelectAll.CheckedChanged += chkSelectAll_CheckedChanged; } } } private void dgvProduct_CellMouseEnter(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex < 0 || e.RowIndex < 0) return; var row = dgvProduct.Rows[e.RowIndex]; if (MouseButtons == MouseButtons.Left) { var columnName = dgvProduct.Columns[e.ColumnIndex].Name; if (isSelecting) { dgvProduct.Rows[e.RowIndex].Cells["colSelect"].Value = isCheck; chkSelectAll.CheckedChanged -= chkSelectAll_CheckedChanged; if (dgvProduct.Rows.Cast().All(r => r.Cells["colSelect"].Value.ParseToBool())) chkSelectAll.Checked = true; else chkSelectAll.Checked = false; chkSelectAll.CheckedChanged += chkSelectAll_CheckedChanged; } } } private void dgvProduct_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) { isSelecting = false; } //根据id获取sku public string GetSkusByProductIds(string postData, string cookies) { CommonSimRequest request = new CommonSimRequest(); request.ContentType = "application/json"; Dictionary dicts = new Dictionary(); string reqUrl = "https://fxg.jinritemai.com/byteshop/promotion/batchGetSkuByProductIds?_bid=ecom_market_shop&appid=1&_=" + _timeSpan + "&_signature=" + _signature; string referer = "https://fxg.jinritemai.com/market/center/group/create?type=1"; string result = request.Request_PostByBigLength(reqUrl, referer, dicts, cookies, postData); return result; } public string checkGroupBuyGoods(string postData, string cookies) { CommonSimRequest request = new CommonSimRequest(); request.ContentType = "application/json"; Dictionary dicts = new Dictionary(); string reqUrl = "https://fxg.jinritemai.com/byteshop/promotion/checkGroupBuy?_bid=ecom_market_shop&appid=1&_=" + _timeSpan + "&_signature=" + _signature; string referer = "https://fxg.jinritemai.com/market/center/group/create?type=1"; string result = request.Request_PostByBigLength(reqUrl, referer, dicts, cookies, postData); return result; } public string createGroupBuyGoods(string postData, string cookies) { CommonSimRequest request = new CommonSimRequest(); request.ContentType = "application/json"; Dictionary dicts = new Dictionary(); string reqUrl = "https://fxg.jinritemai.com/byteshop/promotion/createGroupBuy?_bid=ecom_market_shop&appid=1&_=" + _timeSpan + "&_signature=" + _signature; string result = request.Request_PostByBigLength(reqUrl, "https://fxg.jinritemai.com/market/center/tools/limitsales/detail?refer=new", dicts, cookies, postData); return result; } public void ClearData() { this.dgvProduct.Rows.Clear(); this.productList.Clear(); } } }