Files
KJD/TEMUKJD/KJDForms/SalesCenterForms/FullMinusShopForm.cs
T

308 lines
13 KiB
C#

using AllBase;
using AllBase.Helper;
using AllBase.Model.Result;
using TemuKJD.Extension;
using NaiBa.Util.Extension;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TemuKJD.KJDForms.SalesCenterForms
{
public partial class FullMinusShopForm : Form
{
private DouShopLoginInfo shop = null;
private CheckBox chkSelectAll = null;
public List<string> lstProductId = new List<string>();
public FullMinusForm fullMinusForm = null;
public List<DouProductPromotionListInfo> productList = new List<DouProductPromotionListInfo>();
private string _signature = "";
private string _underline = "";
public bool isGetProductSuccess = false;
public FullMinusShopForm(DouShopLoginInfo _shop = null)
{
InitializeComponent();
shop = _shop;
//设置全选按钮
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)
{
lstProductId.Clear();
foreach (DataGridViewRow row in dgvProduct.Rows)
{
row.Cells["colSelect"].Value = false;
}
}
else
{
lstProductId.Clear(); //先清空,然后把id加入list
foreach (DataGridViewRow row in dgvProduct.Rows)
{
row.Cells["colSelect"].Value = true;
long ProductId = row.Cells["colProductId"].Value.ParseToLong();
lstProductId.Add(ProductId.ToString());
}
}
}
public void ShowVisitorMessage(string msg)
{
Invoke(new Action(() => { this.label1.Text = msg; }));
}
public void GetProduct()
{
if (shop == null)
return;
ShowVisitorMessage("");
foreach (DataGridViewRow r in dgvProduct.Rows)
{
r.Cells["colStock"].Value = "获取中";
r.Cells["colMessage"].Value = "";
}
Task.Run(() =>
{
if (shop.LoginStatus == "登录成功")
fullMinusForm.GetProductByCef(shop);
var rowsTemp = dgvProduct.Rows.Cast<DataGridViewRow>().ToList();
rowsTemp.Reverse();
}).Wait();
}
public void GetProductSim(string listUrl, string referrerUrl, Dictionary<string, string> headers, string firstPageData = "", string campaign_start_time = "", string campaign_end_time = "")
{
ShowVisitorMessage("");
foreach (DataGridViewRow r in dgvProduct.Rows)
{
Invoke(new Action(() =>
{
r.Cells["colStock"].Value = "获取中";
r.Cells["colMessage"].Value = "";
}));
}
ShowVisitorMessage("正在准备获取商品");
int total = -1;
int number = 0;
string result = "";
if (!string.IsNullOrEmpty(firstPageData))
result = firstPageData;
referrerUrl = "https://fxg.jinritemai.com/market/center/tools/moneyoff";
_underline = BaseRegex.GetMatchRegexString(ref listUrl, "&_=([\\s\\S]+?)&");
if (string.IsNullOrEmpty(_underline))
_underline = BaseRegex.GetMatchRegexString(ref listUrl, "&_=([\\s\\S]+?)");
if (string.IsNullOrEmpty(_underline))
_underline = 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]+?)&");
}
listUrl = "https://fxg.jinritemai.com/marketing/activity/v1/query_product?name=&page=1&pageSize=15&start_time=" + BaseClass.DateTimeToTimestampSeconds(campaign_start_time.ParseToDateTime()) + "&end_time=" + BaseClass.DateTimeToTimestampSeconds(campaign_end_time.ParseToDateTime()) + "&_bid=ecom_market_shop&appid=1&_=" + _underline + "&_signature=" + _signature;
for (int i = 1; i < 9999; i++)
{
if (i == 666)
{
}
result = "";
number += 15;
for (int j = 0; j < 3 && !result.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;
var rUrl = listUrl.Replace("page=1", "page=" + i);
int pageSize = 15;
if (number > 10000)
{
pageSize = 15 - (number - 10000);
if (pageSize < 0)
pageSize = 0;
rUrl = rUrl.Replace("pageSize=15", "pageSize=" + pageSize);
}
result = simRequest.Request_Get(rUrl, referrerUrl, new Dictionary<string, string>(), cookie);
if (j > 0)
{
Thread.Sleep(Randomx.GetId(250, 500));
}
ShowVisitorMessage("正在获取商品第" + i + "页");
}
if (result.Contains("\"st\":0") && !fullMinusForm.cancellationTokenSource.IsCancellationRequested)
{
this.isGetProductSuccess = true;
if (total == -1)
total = BaseRegex.GetIntJson(ref result, "total");
int totalPage = Math.Ceiling(total / 15M).ParseToInt();
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<DouProductPromotionListInfo>(jObj["data"]["data"]?.ToString());
foreach (var product in pList)
{
if (product.allow == false)
continue;
if (productList.Any(p => p.product_id == product.product_id))
productList.RemoveAll(p => p.product_id == product.product_id);
productList.Add(product);
Invoke(new Action(() =>
{
if (this.dgvProduct.Rows.Count >= productList.Count)
{
DataGridViewRow row = dgvProduct.Rows.Cast<DataGridViewRow>().FirstOrDefault(x => x.Cells["colProductId"].Value.ToString() == product.product_id);
if (row != null)
{
row.Cells["colStock"].Value = product.stock_num;
row.Cells["colMessage"].Value = "";
}
else
dgvProduct.Rows.Add(false, product.product_id, product.name, product.stock_num,
product.min_price / 100M + " - " + product.max_price / 100M);
}
else
dgvProduct.Rows.Add(false, product.product_id, product.name, product.stock_num,
product.min_price / 100M + " - " + product.max_price / 100M);
}));
}
Thread.Sleep(Randomx.GetId(250, 500));
}
catch (Exception)
{
isGetProductSuccess = false;
break;
}
}
else
{
isGetProductSuccess = false;
break;
}
if (number >= total)
break;
}
Invoke(new Action(() =>
{
if (fullMinusForm.cancellationTokenSource.IsCancellationRequested)
{
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;
}));
}
private void dgvProduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0)
return;
string columnName = dgvProduct.Columns[e.ColumnIndex].Name;
long ProductId = dgvProduct.Rows[e.RowIndex].Cells["colProductId"].Value.ParseToLong();
var row = dgvProduct.Rows[e.RowIndex];
switch (columnName)
{
case "colSelect":
row.Cells["colSelect"].Value = !row.Cells["colSelect"].Value.ParseToBool();
if (dgvProduct.Rows[e.RowIndex].Cells["colSelect"].Value.ParseToBool())
lstProductId.Add(ProductId.ToString());
else
lstProductId.Remove(ProductId.ToString());
break;
}
}
public void ClearData()
{
this.productList.Clear();
this.lstProductId.Clear();
this.dgvProduct.Rows.Clear();
}
public string checkFullDiscountActivity(string postData, string cookies)
{
CommonSimRequest request = new CommonSimRequest();
request.ContentType = "application/json";
Dictionary<string, string> dicts = new Dictionary<string, string>();
string reqUrl = "https://fxg.jinritemai.com/marketing/activity/v1/check_full_discount_activity?_bid=ecom_market_shop&appid=1&_=" + _underline + "&_signature=" + _signature;
string result = request.Request_PostByBigLength(reqUrl, "https://fxg.jinritemai.com/market/center/tools/moneyoffCreate?refer=1&newFeature=true", dicts, cookies, postData);
return result;
}
public string createFullDiscountActivity(string postData, string cookies)
{
CommonSimRequest request = new CommonSimRequest();
request.ContentType = "application/json";
Dictionary<string, string> dicts = new Dictionary<string, string>();
string reqUrl = "https://fxg.jinritemai.com/marketing/activity/v1/create_full_discount_activity?_bid=ecom_market_shop&appid=1&_=" + _underline + "&_signature=" + _signature;
string result = request.Request_PostByBigLength(reqUrl, "https://fxg.jinritemai.com/market/center/tools/moneyoffCreate?refer=1&newFeature=true", dicts, cookies, postData);
return result;
}
public void RefreshdgvProduct(List<string> strList)
{
if (strList != null && strList.Count > 0)
{
Invoke(new Action(() =>
{
for (int i = 0; i < dgvProduct.Rows.Count; i++)
{
DataGridViewRow row = dgvProduct.Rows[i];
long ActiveId = row.Cells["colProductId"].Value.ParseToLong();
if (strList.Any(p => p == ActiveId.ToString()))
{
dgvProduct.Rows.Remove(row);
lstProductId.Remove(ActiveId.ToString());
}
}
}));
}
}
}
}