优化微信上传代码
This commit is contained in:
1 parent
9e1a354ee5
commit
f71cb1bba8
13 files changed
+749
-159
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user