api接口上货

This commit is contained in:
wule_5900x committed 2026-01-31 21:15:42 +08:00
1 parent afaabea1fa
commit 50ae0c3ce4
24 files changed
+511 -200

No files matched your search

@@ -49,6 +49,11 @@ namespace TemuKJD.WXForms.ProductForms
public Dictionary<string, bool> dicCheckedShopId = new Dictionary<string, bool>();
public Dictionary<string, WxShopUploadInfo> dicShopUpload = new Dictionary<string, WxShopUploadInfo>();
public string pdd_product_cates = "";
public List<string> forceFilteKeyWords = new List<string>()
{
"亏本", "冲量", "白菜价", "跳楼价", "xxx", "xx", "最大", "最小", "低价", "降价", "成本", "赔钱", "甩卖",
"真丝", "蚕丝", "香云纱", "羊绒", "羊毛", "羊羔绒", "羊羔毛", "羽绒", "皮草"
};
public WXDBProdUploadForm()
{
InitializeComponent();
@@ -137,6 +142,10 @@ namespace TemuKJD.WXForms.ProductForms
wxUploadRule = NaiBa.Util.Helper.ConfigHelper.LoadConfig<WXUploadRule>("wx_upload_rule");
if (wxUploadRule == null)
wxUploadRule = new WXUploadRule();
var filteWords = wxUploadRule.FilterWords.Split(new string[] { ",", ",", " ", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
filteWords.RemoveAll(w => forceFilteKeyWords.Contains(w));
filteWords.AddRange(forceFilteKeyWords);
wxUploadRule.FilterWords = string.Join(",", filteWords);
this.txtPreDays.TextChanged -= txtPreDays_TextChanged;
this.txtReplaceText.TextChanged -= txtReplaceText_TextChanged;
this.txtMinPrice.TextChanged -= txtMinPrice_TextChanged;
@@ -187,14 +196,6 @@ namespace TemuKJD.WXForms.ProductForms
dgvSelectProduct.Columns["colSkuFakeImg"].Visible = false;
// if (BaseClass.SaLevel == 1)
// {
// dgvSelectProduct.Columns["colSkuFakeImg"].Visible = true;
// }
//#if DEBUG
// dgvSelectProduct.Columns["colSkuFakeImg"].Visible = true;
//#endif
upload_draft_product_lst = NaiBa.Util.Helper.ConfigHelper.LoadConfig<NaiBa.Util.ConcurrentList<TemuDBProductSelect>>("wx_select_upload_draft_products");
if (upload_draft_product_lst == null)
upload_draft_product_lst = new ConcurrentList<TemuDBProductSelect>();
@@ -208,15 +209,15 @@ namespace TemuKJD.WXForms.ProductForms
selectProd.shop_account,
"登录",
"进店",
selectProd.spu_id,
BaseClass.SaLevel > 0 ? selectProd.spu_id : selectProd.spu_id.ParseToString().GetHashCode().ParseToString().Trim('-'),
selectProd.skc_id,
selectProd.main_img,
null,
DataGridViewExtension.defaultBitmap,
"",
null,
selectProd.product_name, // 用户端显示商品原名
selectProd.cat_name,
selectProd.ext_code,
BaseClass.SaLevel > 0 ? selectProd.ext_code : selectProd.ext_code.ParseToString().GetHashCode().ParseToString().Trim('-'),
selectProd.status,
selectProd.draft_id,
selectProd.notes,
@@ -234,6 +235,24 @@ namespace TemuKJD.WXForms.ProductForms
}
}
#if DEBUG
if (BaseClass.SaLevel == 1)
{
txtIdFilter.Visible = true;
txtExtCodeFilter.Visible = true;
cmbSourceType.Visible = true;
cmbGroupFilter.Visible = true;
lblIdFilter.Visible = true;
lblExtCodeFilter.Visible = true;
lblSourceType.Visible = true;
lblGroupFilter.Visible = true;
}
#endif
if (BaseClass.SaLevel == 1)
{
chkImgRandomBlock.Visible = true;
}
IsLoadComplate = true;
}
@@ -661,7 +680,7 @@ namespace TemuKJD.WXForms.ProductForms
}
if (cancellationTokenSource.IsCancellationRequested)
break;
while (currentShopList.Count >= 5) // 最多5店同传
while (currentShopList.Count >= 20) // 最多20店同传
{
Thread.Sleep(100);
}
@@ -729,7 +748,7 @@ namespace TemuKJD.WXForms.ProductForms
var lstTitleReplaceText = new List<string>();
lstTitleReplaceText.Clear();
if (!txtReplaceText.Text.Trim().IsNullOrEmpty())
lstTitleReplaceText.AddRange(txtReplaceText.Text.Trim().Split(new string[] { ",", ",", " " }, StringSplitOptions.RemoveEmptyEntries));
lstTitleReplaceText.AddRange(txtReplaceText.Text.Trim().Split(new string[] { ",", ",", " ", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries));
// 过滤标题内指定词语
if (lstTitleReplaceText.Count > 0)
{
@@ -749,15 +768,15 @@ namespace TemuKJD.WXForms.ProductForms
selectProd.shop_account,
"登录",
"进店",
selectProd.spu_id,
BaseClass.SaLevel > 0 ? selectProd.spu_id : selectProd.spu_id.ParseToString().GetHashCode().ParseToString().Trim('-'),
selectProd.skc_id,
selectProd.main_img,
null,
DataGridViewExtension.defaultBitmap,
"",
null,
selectProd.product_name, // 用户端显示商品原名
selectProd.cat_name,
selectProd.ext_code,
BaseClass.SaLevel > 0 ? selectProd.ext_code : selectProd.ext_code.ParseToString().GetHashCode().ParseToString().Trim('-'),
selectProd.status,
selectProd.draft_id,
selectProd.notes,
@@ -881,8 +900,8 @@ namespace TemuKJD.WXForms.ProductForms
var timeSpan = DateTime.Now - startTime;
duration = timeSpan.TotalSeconds;
// 至少15秒才完成一件商品
while (duration < 15)
// 至少10秒才完成一件商品
while (duration < 10)
{
Thread.Sleep(1000);
timeSpan = DateTime.Now - startTime;
@@ -1051,8 +1070,8 @@ namespace TemuKJD.WXForms.ProductForms
var lstTitleReplaceText = new List<string>();
lstTitleReplaceText.Clear();
if (!txtReplaceText.Text.Trim().IsNullOrEmpty())
lstTitleReplaceText.AddRange(txtReplaceText.Text.Trim().Split(new string[] { ",", ",", " " }, StringSplitOptions.RemoveEmptyEntries));
// 过滤标题内指定词语
lstTitleReplaceText.AddRange(txtReplaceText.Text.Trim().Split(new string[] { ",", ",", " ", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries));
// 过滤标题敏感词
if (lstTitleReplaceText.Count > 0)
{
foreach (var txt in lstTitleReplaceText)
@@ -1094,6 +1113,29 @@ namespace TemuKJD.WXForms.ProductForms
// 替换编辑过的sku
if (!string.IsNullOrEmpty(dbProd.sku_edit_json))
prodJObj["detail"]["sku"] = JArray.Parse(JsonHelper.SerializeObject(JsonHelper.DeserializeJsonToList<sku_item>(dbProd.sku_edit_json)));
// 过滤sku敏感词
if (lstTitleReplaceText.Count> 0)
{
var skuJArr = prodJObj["detail"]["sku"] as JArray;
try
{
foreach (var skuJObj in skuJArr)
{
var specJArr = skuJObj["spec"] as JArray;
for (int i = 0; i < specJArr.Count; i++)
{
foreach (var txt in lstTitleReplaceText)
{
specJArr[i]["spec_name"] = specJArr[i]["spec_name"].ParseToString().Replace(txt, "");
}
}
}
}
catch (Exception ex)
{
return "{\"success\":false,\"draftId\":0,\"status\":\"未上传\",\"errMsg\":\"商品数据初始化失败" + ex.Message + "\"}";
}
}
if (chkSameSpecPrice.Checked)
{
var priceTextLst = dbProd.price_text.Split('-');
@@ -1243,7 +1285,9 @@ namespace TemuKJD.WXForms.ProductForms
var upProdInitParam = new WXUploadProductInitParam();
// 生成上传商品对象
var upProdParam = new WXUploadProductParam();
// 随机获取一个上传图片的accessToken
var token = new WxApiBLL().GetRandomWxToken();
// 设置标题
upProdInitParam.product.info.title = dbProd.product_name.IsNullOrEmpty() ? prodInfo.detail.goods_name : dbProd.product_name;
upProdParam.product.info.title = dbProd.product_name.IsNullOrEmpty() ? prodInfo.detail.goods_name : dbProd.product_name;
@@ -1266,21 +1310,46 @@ namespace TemuKJD.WXForms.ProductForms
}
Thread.Sleep(Randomx.GetId(500, 800));
//-------------------------------------------------------------------------------------------------------------------------------------
// ai识别类目暂存主图位置
int imgId = 0;
string tempImgPath = Path.GetTempPath() + "\\";
string folderLetters = "abcdefghijklmnopqrstuvwxyz1234567890";
for (int i = 0; i < 8; i++)
{
tempImgPath += folderLetters[Randomx.GetId(folderLetters.Length)];
}
List<string> tempImgFullPathLst = new List<string>();
//-------------------------------------------------------------------------------------------------------------------------------------
// 上传主图
//上传主图
foreach (var gallery_item in prodInfo.detail.galleries.Where(g => g.type == 1).Take(9)) // 9张主图
{
try
{
if (chkImgRandomBlock.Checked && (upProdParam.product.info.category.FirstOrDefault()?.name == "图书" || prodInfo.detail.cats.FirstOrDefault() == "书籍/杂志/报纸" ||
dbProd.main_cat_name == "图书" || dbProd.main_cat_name == "书籍/杂志/报纸"))
{
// 图书暂时要加差异化主图,走不了api
}
else
{
// 使用接口链接传图
var urlImg = BaseClass.BuildPDDObjString("product/wx_api_upload_image");
var upDate = UserLogin.GetSignaturex();
upDate += "&token=" + token.accessToken;
upDate += "&post_data=" + GetHtml.UrlEncodeUtf8("{\"img_url\":\"" + gallery_item.url.Replace("https://", "http://") + "\"}");
var upDateRst = GetHtml.PostUtf8(urlImg, upDate);
for (int i = 0; i < 5 && upDateRst.Contains("access_token expired"); i++)
{
token.mdyTime = DateTime.Now.AddDays(-100);
token = new WxApiBLL().GetRandomWxToken();
upDateRst = GetHtml.PostUtf8(urlImg, upDate);
}
var upImgUrl = BaseRegex.GetStringJson(ref upDateRst, "img_url");
if (upDateRst.Contains("\"errcode\":0") && !string.IsNullOrEmpty(upImgUrl))
{
upProdInitParam.product.info.headImg.Add(upImgUrl);
upProdParam.product.info.headImg.Add(upImgUrl);
continue;
}
else
{
}
}
}
catch
{
}
var upImgAuthRst = WXUpload.getUploadTempImgAuth(shop);
for (int i = 0; i < 10 && upImgAuthRst.Contains("LogicError"); i++)
{
@@ -1317,38 +1386,13 @@ namespace TemuKJD.WXForms.ProductForms
using (Graphics g = Graphics.FromImage(imageBg))
{
g.DrawImage(image, new Rectangle(0, 0, imageBg.Width, imageBg.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
if (upProdParam.product.info.category.FirstOrDefault()?.name == "图书" || prodInfo.detail.cats.FirstOrDefault() == "书籍/杂志/报纸")
if (upProdParam.product.info.category.FirstOrDefault()?.name == "图书" || prodInfo.detail.cats.FirstOrDefault() == "书籍/杂志/报纸" ||
dbProd.main_cat_name == "图书" || dbProd.main_cat_name == "书籍/杂志/报纸")
{
g.FillRectangle(new SolidBrush(Color.FromArgb(Randomx.GetId(256), Randomx.GetId(256), Randomx.GetId(256))),
new Rectangle(Randomx.GetId(imageBg.Width - 30), Randomx.GetId(imageBg.Height - 30), 30, 30));
}
}
// 取前三张图片保存到系统temp路径,配合商品标题获取商品类目
if (imgId < 3)
{
imgId++;
if (!Directory.Exists(tempImgPath))
Directory.CreateDirectory(tempImgPath);
string imgFullPath = tempImgPath + "\\图片" + imgId + ".jpg";
tempImgFullPathLst.Add(imgFullPath);
if (imgId == 1)
image.Save(imgFullPath, System.Drawing.Imaging.ImageFormat.Jpeg);
else
{
// 只需要传一张真实图像即可
int fileSize = Randomx.GetId(1024, 10240);
using (BinaryWriter writer = new BinaryWriter(File.Create(imgFullPath)))
{
for (int i = 0; i < fileSize; i++)
{
writer.Write((byte)Randomx.GetId(256)); // 生成0到255之间的随机字节
}
}
}
}
using (MemoryStream memoryStream = new MemoryStream())
{
imageBg.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
@@ -1377,6 +1421,56 @@ namespace TemuKJD.WXForms.ProductForms
Thread.Sleep(Randomx.GetId(10, 50));
}
//-------------------------------------------------------------------------------------------------------------------------------------
// ai识别类目暂存主图位置
int imgId = 0;
string tempImgPath = Path.GetTempPath() + "\\";
string folderLetters = "abcdefghijklmnopqrstuvwxyz1234567890";
for (int i = 0; i < 8; i++)
{
tempImgPath += folderLetters[Randomx.GetId(folderLetters.Length)];
}
List<string> tempImgFullPathLst = new List<string>();
//-------------------------------------------------------------------------------------------------------------------------------------
// 生成ai识别类目所需图片
foreach (var gallery_item in prodInfo.detail.galleries.Where(g => g.type == 1).Take(3)) // 9张主图
{
string getImgError;
var image = GetHtml.GetUrlImg(gallery_item.url.Replace("https://", "http://"), out getImgError);
var imageBg = new Bitmap(800, 800);
if (!string.IsNullOrEmpty(getImgError) || image == null)
{
return "{\"success\":false,\"draftId\":0,\"status\":\"未上传\",\"errMsg\":\"初始化主图上传失败\"}";
}
using (Graphics g = Graphics.FromImage(imageBg))
{
g.DrawImage(image, new Rectangle(0, 0, imageBg.Width, imageBg.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
}
// 取前三张图片保存到系统temp路径,配合商品标题获取商品类目
if (imgId <= 3)
{
if (!Directory.Exists(tempImgPath))
Directory.CreateDirectory(tempImgPath);
string imgFullPath = tempImgPath + "\\图片" + imgId + ".jpg";
tempImgFullPathLst.Add(imgFullPath);
if (imgId == 1)
image.Save(imgFullPath, System.Drawing.Imaging.ImageFormat.Jpeg);
else
{
// 只需要传一张真实图像即可
int fileSize = Randomx.GetId(1024, 10240);
using (BinaryWriter writer = new BinaryWriter(File.Create(imgFullPath)))
{
for (int i = 0; i < fileSize; i++)
{
writer.Write((byte)Randomx.GetId(256)); // 生成0到255之间的随机字节
}
}
}
}
}
if (cell != null)
{
Invoke(new Action(() =>
@@ -1435,8 +1529,15 @@ namespace TemuKJD.WXForms.ProductForms
return "{\"success\":false,\"draftId\":0,\"status\":\"未上传\",\"errMsg\":\"" + "根据图片获取类目失败 " + ex.Message + "\"}";
}
// 检测是否有类目上传权限
// 获取叶子类目id
long leafCatId = (upProdParam.product.info.category.LastOrDefault()?.catId).ParseToLong(); //哲学经典著作
var bound = GlobalConfig.WxCatBoundList.FirstOrDefault(b => b.leaf_id == leafCatId);
if (bound == null)
return "{\"success\":false,\"draftId\":0,\"status\":\"未上传\",\"errMsg\":\"获取类目保证金失败\"}";
if (bound.cat_bound > txtFilterBound.Text.Trim().ParseToInt())
return "{\"success\":false,\"draftId\":0,\"status\":\"未上传\",\"errMsg\":\"类目保证金(" + bound.cat_bound + ")超过过滤限制(" + txtFilterBound.Text.Trim().ParseToInt() + ")\"}";
// 检测是否有类目上传权限
var cateUpCheckRst = WXUpload.batchCheckCategoryRelation(shop, leafCatId);
if (!cateUpCheckRst.Contains("\"code\":0") && !cateUpCheckRst.Contains("\"success\":true"))
{
@@ -1703,8 +1804,39 @@ namespace TemuKJD.WXForms.ProductForms
var detailGalleries = prodInfo.detail.galleries.Where(g => g.type == 2).ToList();
if (detailGalleries.Count == 0)
detailGalleries.Add(new gallery_item() { type = 2, url = prodInfo.detail.galleries.FirstOrDefault(g => g.type == 1)?.url });
foreach (var gallery_item in detailGalleries)
{
if (chkImgRandomBlock.Checked && (upProdParam.product.info.category.FirstOrDefault()?.name == "图书" || prodInfo.detail.cats.FirstOrDefault() == "书籍/杂志/报纸" ||
dbProd.main_cat_name == "图书" || dbProd.main_cat_name == "书籍/杂志/报纸"))
{
// 图书暂时要加差异化详情图,走不了api
}
else
{
// 使用接口链接传图
var urlImg = BaseClass.BuildPDDObjString("product/wx_api_upload_image");
var upDate = UserLogin.GetSignaturex();
upDate += "&token=" + token.accessToken;
upDate += "&post_data=" + GetHtml.UrlEncodeUtf8("{\"img_url\":\"" + gallery_item.url.Replace("https://", "http://") + "\"}");
var upDateRst = GetHtml.PostUtf8(urlImg, upDate);
for (int i = 0; i < 5 && upDateRst.Contains("access_token expired"); i++)
{
token.mdyTime = DateTime.Now.AddDays(-100);
token = new WxApiBLL().GetRandomWxToken();
upDateRst = GetHtml.PostUtf8(urlImg, upDate);
}
var upImgUrl = BaseRegex.GetStringJson(ref upDateRst, "img_url");
if (upDateRst.Contains("\"errcode\":0") && !string.IsNullOrEmpty(upImgUrl))
{
upProdParam.product.info.detail.detailImg.Add(upImgUrl);
continue;
}
else
{
}
}
bool randomDrawBlock = false;
if (upProdParam.product.info.category.FirstOrDefault()?.name == "图书" || prodInfo.detail.cats.FirstOrDefault() == "书籍/杂志/报纸")
randomDrawBlock = true;
@@ -1972,6 +2104,28 @@ namespace TemuKJD.WXForms.ProductForms
var spec = sku.spec[0];
if (!attrParam.items.Any(it => it.value == spec.spec_name))
{
// 使用接口链接传sku图
var urlImg = BaseClass.BuildPDDObjString("product/wx_api_upload_image");
var upDate = UserLogin.GetSignaturex();
upDate += "&token=" + token.accessToken;
upDate += "&post_data=" + GetHtml.UrlEncodeUtf8("{\"img_url\":\"" + sku.thumb_url.Replace("https://", "http://") + "\"}");
var upDateRst = GetHtml.PostUtf8(urlImg, upDate);
for (int i = 0; i < 5 && upDateRst.Contains("access_token expired"); i++)
{
token.mdyTime = DateTime.Now.AddDays(-100);
token = new WxApiBLL().GetRandomWxToken();
upDateRst = GetHtml.PostUtf8(urlImg, upDate);
}
var upImgUrl = BaseRegex.GetStringJson(ref upDateRst, "img_url");
if (upDateRst.Contains("\"errcode\":0") && !string.IsNullOrEmpty(upImgUrl))
{
attrParam.items.Add(new productSaleAttrItemParam() { value = spec.spec_name, imgUrl = upImgUrl, });
continue; // 传图成功跳过模拟上传
}
else
{
}
// 上传sku图
var locationStr = UploadImg(shop, sku.thumb_url, cell);
if (!locationStr.Contains("\"success\":true"))
@@ -3357,7 +3511,7 @@ Content-Type: application/octet-stream
{
public int PreSaleDays { get; set; } = 5;
public string FilterWords { get; set; } = "真丝,蚕丝,香云纱,羊绒,羊毛,羊羔绒,羊羔毛,羽绒,皮草";
public string FilterWords { get; set; } = "亏本,冲量,白菜价,跳楼价,xx,xxx,最大,最小,低价,降价,成本,赔钱,甩卖,真丝,蚕丝,香云纱,羊绒,羊毛,羊羔绒,羊羔毛,羽绒,皮草";
public int SkuMinPrice { get; set; } = 10;