优化微信上传代码

This commit is contained in:
wule committed 2026-01-15 22:24:00 +08:00
1 parent 9e1a354ee5
commit f71cb1bba8
13 files changed
+749 -159

No files matched your search

+142 -100
View File
@@ -40,6 +40,8 @@ namespace TemuKJD.WXForms.ProductForms
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.ucGroupBoxEx2 = new TemuKJD.UserComponent.UcGroupBoxEx(this.components);
this.chkUpDraft = new TemuKJD.UserComponent.UcCheckBox(this.components);
this.并发上传 = new DevComponents.DotNetBar.PanelEx();
this.chkSkipUploaded = new TemuKJD.UserComponent.UcCheckBox(this.components);
this.chkSameSpecPrice = new TemuKJD.UserComponent.UcCheckBox(this.components);
this.label5 = new System.Windows.Forms.Label();
@@ -94,6 +96,17 @@ namespace TemuKJD.WXForms.ProductForms
this.splitContainer3 = new System.Windows.Forms.SplitContainer();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
this.dgvShop = new TemuKJD.UserComponent.DataGridViewEx();
this.colSelect = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.colShopLoginName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colShopId = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colShopName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colShopNotes = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colSelectProduct = new System.Windows.Forms.DataGridViewLinkColumn();
this.colLoginStatus = new System.Windows.Forms.DataGridViewLinkColumn();
this.colCanUse = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.colUploadCount = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colRemaining = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colMessage = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.txtTest20981 = new System.Windows.Forms.TextBox();
this.btnTest20981 = new DevComponents.DotNetBar.PanelEx();
this.pbLoading = new System.Windows.Forms.PictureBox();
@@ -127,17 +140,6 @@ namespace TemuKJD.WXForms.ProductForms
this.btnClear = new DevComponents.DotNetBar.PanelEx();
this.panelEx1 = new DevComponents.DotNetBar.PanelEx();
this.panel1 = new System.Windows.Forms.Panel();
this.colSelect = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.colShopLoginName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colShopId = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colShopName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colShopNotes = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colSelectProduct = new System.Windows.Forms.DataGridViewLinkColumn();
this.colLoginStatus = new System.Windows.Forms.DataGridViewLinkColumn();
this.colCanUse = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.colUploadCount = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colRemaining = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colMessage = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
@@ -192,6 +194,7 @@ namespace TemuKJD.WXForms.ProductForms
| System.Windows.Forms.AnchorStyles.Right)));
this.ucGroupBoxEx2.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(63)))), ((int)(((byte)(126)))), ((int)(((byte)(255)))));
this.ucGroupBoxEx2.BorderStyle = TemuKJD.UserComponent.UcGroupBoxEx.GroupBoxBorderStyle.ROUND;
this.ucGroupBoxEx2.Controls.Add(this.并发上传);
this.ucGroupBoxEx2.Controls.Add(this.chkSkipUploaded);
this.ucGroupBoxEx2.Controls.Add(this.chkSameSpecPrice);
this.ucGroupBoxEx2.Controls.Add(this.label5);
@@ -218,6 +221,7 @@ namespace TemuKJD.WXForms.ProductForms
this.ucGroupBoxEx2.Controls.Add(this.label4);
this.ucGroupBoxEx2.Controls.Add(this.label9);
this.ucGroupBoxEx2.Controls.Add(this.chkStepAddPrice);
this.ucGroupBoxEx2.Controls.Add(this.chkUpDraft);
this.ucGroupBoxEx2.Location = new System.Drawing.Point(459, 2);
this.ucGroupBoxEx2.Name = "ucGroupBoxEx2";
this.ucGroupBoxEx2.Size = new System.Drawing.Size(1062, 140);
@@ -225,6 +229,42 @@ namespace TemuKJD.WXForms.ProductForms
this.ucGroupBoxEx2.TabStop = false;
this.ucGroupBoxEx2.Text = "商品上传";
//
// chkUpDraft
//
this.chkUpDraft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.chkUpDraft.AutoSize = true;
this.chkUpDraft.Location = new System.Drawing.Point(770, 105);
this.chkUpDraft.Name = "chkUpDraft";
this.chkUpDraft.Size = new System.Drawing.Size(99, 21);
this.chkUpDraft.TabIndex = 769;
this.chkUpDraft.Text = "上传到草稿箱";
this.chkUpDraft.UseVisualStyleBackColor = true;
//
// 并发上传
//
this.并发上传.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.并发上传.ColorSchemeStyle = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled;
this.并发上传.Cursor = System.Windows.Forms.Cursors.Hand;
this.并发上传.DisabledBackColor = System.Drawing.Color.Empty;
this.并发上传.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.并发上传.Location = new System.Drawing.Point(865, 104);
this.并发上传.Name = "并发上传";
this.并发上传.Size = new System.Drawing.Size(84, 23);
this.并发上传.Style.Alignment = System.Drawing.StringAlignment.Center;
this.并发上传.Style.BackColor1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(63)))), ((int)(((byte)(126)))), ((int)(((byte)(255)))));
this.并发上传.Style.BackColor2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(63)))), ((int)(((byte)(126)))), ((int)(((byte)(255)))));
this.并发上传.Style.BorderColor.ColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBorder;
this.并发上传.Style.BorderDashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
this.并发上传.Style.BorderSide = DevComponents.DotNetBar.eBorderSide.None;
this.并发上传.Style.ForeColor.Color = System.Drawing.Color.White;
this.并发上传.Style.GradientAngle = 90;
this.并发上传.StyleMouseOver.Alignment = System.Drawing.StringAlignment.Center;
this.并发上传.StyleMouseOver.BackColor1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(25)))), ((int)(((byte)(102)))), ((int)(((byte)(255)))));
this.并发上传.StyleMouseOver.BackColor2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(25)))), ((int)(((byte)(102)))), ((int)(((byte)(255)))));
this.并发上传.TabIndex = 768;
this.并发上传.Text = "并发上传";
this.并发上传.Click += new System.EventHandler(this.并发上传_Click);
//
// chkSkipUploaded
//
this.chkSkipUploaded.AutoSize = true;
@@ -1031,6 +1071,95 @@ namespace TemuKJD.WXForms.ProductForms
this.dgvShop.TabIndex = 6;
this.dgvShop.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvShop_CellContentClick);
//
// colSelect
//
this.colSelect.HeaderText = "全选";
this.colSelect.Name = "colSelect";
this.colSelect.ReadOnly = true;
this.colSelect.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.colSelect.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.colSelect.Width = 28;
//
// colShopLoginName
//
this.colShopLoginName.HeaderText = "店铺登录名";
this.colShopLoginName.Name = "colShopLoginName";
this.colShopLoginName.ReadOnly = true;
this.colShopLoginName.Visible = false;
//
// colShopId
//
this.colShopId.HeaderText = "店铺Id";
this.colShopId.Name = "colShopId";
this.colShopId.ReadOnly = true;
this.colShopId.Visible = false;
this.colShopId.Width = 55;
//
// colShopName
//
this.colShopName.FillWeight = 150F;
this.colShopName.HeaderText = "店铺名称";
this.colShopName.Name = "colShopName";
this.colShopName.ReadOnly = true;
this.colShopName.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.colShopName.Width = 66;
//
// colShopNotes
//
this.colShopNotes.HeaderText = "分组";
this.colShopNotes.Name = "colShopNotes";
this.colShopNotes.ReadOnly = true;
this.colShopNotes.Width = 66;
//
// colSelectProduct
//
this.colSelectProduct.HeaderText = "选择商品";
this.colSelectProduct.Name = "colSelectProduct";
this.colSelectProduct.ReadOnly = true;
this.colSelectProduct.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.colSelectProduct.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.colSelectProduct.Visible = false;
this.colSelectProduct.Width = 62;
//
// colLoginStatus
//
this.colLoginStatus.HeaderText = "状态";
this.colLoginStatus.Name = "colLoginStatus";
this.colLoginStatus.ReadOnly = true;
this.colLoginStatus.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.colLoginStatus.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.colLoginStatus.Width = 38;
//
// colCanUse
//
this.colCanUse.HeaderText = "可用";
this.colCanUse.Name = "colCanUse";
this.colCanUse.ReadOnly = true;
this.colCanUse.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.colCanUse.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.colCanUse.Visible = false;
this.colCanUse.Width = 28;
//
// colUploadCount
//
this.colUploadCount.HeaderText = "已传";
this.colUploadCount.Name = "colUploadCount";
this.colUploadCount.ReadOnly = true;
this.colUploadCount.Width = 38;
//
// colRemaining
//
this.colRemaining.HeaderText = "月限额";
this.colRemaining.Name = "colRemaining";
this.colRemaining.ReadOnly = true;
this.colRemaining.Visible = false;
//
// colMessage
//
this.colMessage.HeaderText = "消息";
this.colMessage.Name = "colMessage";
this.colMessage.ReadOnly = true;
//
// txtTest20981
//
this.txtTest20981.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
@@ -1422,95 +1551,6 @@ namespace TemuKJD.WXForms.ProductForms
this.panel1.TabIndex = 189;
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
//
// colSelect
//
this.colSelect.HeaderText = "全选";
this.colSelect.Name = "colSelect";
this.colSelect.ReadOnly = true;
this.colSelect.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.colSelect.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.colSelect.Width = 28;
//
// colShopLoginName
//
this.colShopLoginName.HeaderText = "店铺登录名";
this.colShopLoginName.Name = "colShopLoginName";
this.colShopLoginName.ReadOnly = true;
this.colShopLoginName.Visible = false;
//
// colShopId
//
this.colShopId.HeaderText = "店铺Id";
this.colShopId.Name = "colShopId";
this.colShopId.ReadOnly = true;
this.colShopId.Visible = false;
this.colShopId.Width = 55;
//
// colShopName
//
this.colShopName.FillWeight = 150F;
this.colShopName.HeaderText = "店铺名称";
this.colShopName.Name = "colShopName";
this.colShopName.ReadOnly = true;
this.colShopName.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.colShopName.Width = 66;
//
// colShopNotes
//
this.colShopNotes.HeaderText = "分组";
this.colShopNotes.Name = "colShopNotes";
this.colShopNotes.ReadOnly = true;
this.colShopNotes.Width = 66;
//
// colSelectProduct
//
this.colSelectProduct.HeaderText = "选择商品";
this.colSelectProduct.Name = "colSelectProduct";
this.colSelectProduct.ReadOnly = true;
this.colSelectProduct.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.colSelectProduct.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.colSelectProduct.Visible = false;
this.colSelectProduct.Width = 62;
//
// colLoginStatus
//
this.colLoginStatus.HeaderText = "状态";
this.colLoginStatus.Name = "colLoginStatus";
this.colLoginStatus.ReadOnly = true;
this.colLoginStatus.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.colLoginStatus.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.colLoginStatus.Width = 38;
//
// colCanUse
//
this.colCanUse.HeaderText = "可用";
this.colCanUse.Name = "colCanUse";
this.colCanUse.ReadOnly = true;
this.colCanUse.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.colCanUse.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.colCanUse.Visible = false;
this.colCanUse.Width = 28;
//
// colUploadCount
//
this.colUploadCount.HeaderText = "已传";
this.colUploadCount.Name = "colUploadCount";
this.colUploadCount.ReadOnly = true;
this.colUploadCount.Width = 38;
//
// colRemaining
//
this.colRemaining.HeaderText = "月限额";
this.colRemaining.Name = "colRemaining";
this.colRemaining.ReadOnly = true;
this.colRemaining.Visible = false;
//
// colMessage
//
this.colMessage.HeaderText = "消息";
this.colMessage.Name = "colMessage";
this.colMessage.ReadOnly = true;
//
// WXDBProdUploadForm
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
@@ -1659,5 +1699,7 @@ namespace TemuKJD.WXForms.ProductForms
private System.Windows.Forms.DataGridViewTextBoxColumn colUploadCount;
private System.Windows.Forms.DataGridViewTextBoxColumn colRemaining;
private System.Windows.Forms.DataGridViewTextBoxColumn colMessage;
private DevComponents.DotNetBar.PanelEx 并发上传;
private UserComponent.UcCheckBox chkUpDraft;
}
}
@@ -27,6 +27,7 @@ using AllBase.Model.Result.PDDResult;
using AllBase.Model.Param.WXParam;
using AllBase.Model.Result.WXResult;
using Newtonsoft.Json;
using NaiBa.Util;
namespace TemuKJD.WXForms.ProductForms
{
@@ -403,6 +404,7 @@ namespace TemuKJD.WXForms.ProductForms
}
}
private void btnUpload_Click(object sender, EventArgs e)
{
var shopIdLst = dgvShop.Rows.Cast<DataGridViewRow>().Where(row
@@ -1679,13 +1681,18 @@ namespace TemuKJD.WXForms.ProductForms
}
upProdParam.product.productKey.productId = BaseRegex.GetLongJson(ref uploadDraftRst, "productId").ParseToString();
upload_draft_products.Add(upProdParam);
if (chkUpDraft.Checked)
return "{\"success\":true,\"draftId\":" + upProdParam.product.productKey.productId + ",\"status\":\"草稿箱\",\"errMsg\":\"上架到草稿箱\"}";
// 提交商品上架
var uploadProdRst = WXUpload.upProductV2(shop, upProdParam);
if (!uploadProdRst.Contains("\"code\":0") && !uploadProdRst.Contains("\"success\":true"))
{
var error = BaseRegex.GetStringJson(ref uploadProdRst, "msg");
if (string.IsNullOrEmpty(error))
error = uploadProdRst;
return "{\"success\":false,\"draftId\":0,\"status\":\"草稿箱\",\"errMsg\":\"上架失败 " + error + "\"}";
return "{\"success\":false,\"draftId\":" + upProdParam.product.productKey.productId + ",\"status\":\"草稿箱\",\"errMsg\":\"上架失败 " + error + "\"}";
}
if (cell != null)
{
@@ -2038,6 +2045,92 @@ namespace TemuKJD.WXForms.ProductForms
GetRandomProductList(out prodCount, 1, 1);
lblGoodsCount.Text = "产品库 " + prodCount + " 件";
}
private void 并发上传_Click(object sender, EventArgs e)
{
var draftIdLst = dgvSelectProduct.Rows.Cast<DataGridViewRow>().Where(row
=> row.Cells["colCheck"]?.Value.ParseToBool() == true).Select(r => r.Cells["colDraftId"]?.Value.ToString()).ToList();
// var lst = NaiBa.Util.Helper.ConfigHelper.LoadConfig<NaiBa.Util.ConcurrentList<WXUploadProductParam>>("wx_upload_draft_products");
// if (lst == null)
// lst = new ConcurrentList<WXUploadProductParam>();
var lst = upload_draft_products;
var upLst = lst.Where(l => draftIdLst.Contains(l.product.productKey.productId)).ToList();
DateTime startTime = DateTime.Now;
Task.Run(() =>
{
List<Task> tLst = new List<Task>();
foreach (var prodParam in upLst)
{
var row = this.dgvSelectProduct.Rows.Cast<DataGridViewRow>().FirstOrDefault(r => r.Cells["colDraftId"].Value.ParseToString() == prodParam.product.productKey.productId);
if (row == null)
continue;
var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == row.Cells["colSupplierId"].Value.ParseToString());
if (shop == null)
continue;
var t =Task.Run(() =>
{
UploadSameTime(shop, prodParam, row.Cells["colOpResult"]);
});
tLst.Add(t);
}
Task.WaitAll(tLst.ToArray());
var duration = DateTime.Now - startTime;
Invoke(new Action(() => { BaseMsgForm.ShowInfo("并发上传完成,耗时:" + duration.TotalSeconds + " 秒"); }));
});
}
public ConcurrentList<WXUploadProductParam> upload_draft_products = new ConcurrentList<WXUploadProductParam>();
void UploadSameTime(ShopLoginInfo shop, WXUploadProductParam upProdParam, DataGridViewCell cell)
{
var uploadProdRst = WXUpload.upProductV2(shop, upProdParam);
int retryTimes = 0;
while (!uploadProdRst.Contains("\"code\":0") && !uploadProdRst.Contains("\"success\":true"))
{
retryTimes++;
uploadProdRst = WXUpload.upProductV2(shop, upProdParam);
if (retryTimes >= 5)
break;
}
if (!uploadProdRst.Contains("\"code\":0") && !uploadProdRst.Contains("\"success\":true"))
{
var error = BaseRegex.GetStringJson(ref uploadProdRst, "msg");
if (string.IsNullOrEmpty(error))
error = uploadProdRst;
if (cell != null)
{
Invoke(new Action(() =>
{
cell.Value = "商品上传失败 " + error;
}));
}
}
else
{
if (cell != null)
{
upload_draft_products.Remove(upProdParam);
Invoke(new Action(() =>
{
cell.Value = "商品已上传,重试次数 " + retryTimes;
}));
}
var dgvShopRow = dgvShop.Rows.Cast<DataGridViewRow>().FirstOrDefault(row => row.Cells["colShopId"]?.Value.ParseToString() == shop.ShopId);
if (dgvShopRow != null)
{
Invoke(new Action(() =>
{
dgvShopRow.Cells["colUploadCount"].Value = dgvShopRow.Cells["colUploadCount"].Value.ParseToInt() + 1;
}));
}
}
}
}
public class WXUpload