Files
KJD/Store.Product/Publish/Steps/Wx/UploadDetailImagesStep.cs
T
2026-09-11 19:11:12 +08:00

182 lines
5.9 KiB
C#

using AllBase;
using AllBase.Extension;
using AllBase.Helper;
using AllBase.Model.Config;
using AllBase.Model.DBModel;
using AllBase.Model.Param.WXParam;
using AllBase.Model.Result;
using AllBase.Model.Result.PDDResult;
using AllBase.Model.Result.WXResult;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Store.Product.Models.PublishParameters.Wx;
using Store.Product.Publish;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Work;
namespace Store.Product.Publish.Steps.Wx;
/// <summary>
/// 由 WXDBProdUploadForm.UploadProduct 的“上传详情图”区段迁移。
/// TODO(迁移): 原区段中的 TEMUKJD/WXUpload/窗体服务引用需按 Store.Product 运行时适配逐项核对。
/// </summary>
public sealed class UploadDetailImagesStep : WxProductPublishStepBase
{
public override string Description => "上传详情图";
protected override Task ExecuteProvider(WXPublishContext context, CancellationToken cancellationToken)
{
if (cell != null)
{
Invoke(new Action(() =>
{
cell.Value = "正在生成商品详情";
}));
}
Thread.Sleep(Randomx.GetId(100, 300));
upProdParam.product.info.detail = new productDetailParam(); // 详情图
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)
{
try
{
if (dbProd.product_get_type_text == "微信店采")
{
upProdParam.product.info.detail.detailImg.Add(gallery_item.url);
continue;
}
// 获取缓存盗链 详情图
if (wxImgMapDic.ContainsKey(BaseClass.GetMd5(gallery_item.url)))
{
upProdParam.product.info.detail.detailImg.Add(wxImgMapDic[BaseClass.GetMd5(gallery_item.url)]);
continue;
}
if (needAddRandomBlock || wantAddRandomBlock || gallery_item.url.Contains("wst.wxapp.tc.qq.com"))
{
// 如果想打码或者需要打码,就走差异化详情图,不走api直传
}
else
{
// 使用接口链接传详情图
var urlImg = BaseClass.BuildWXImgObjString("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 sTime = DateTime.Now;
var upDateRst = GetHtml.PostUtf8(urlImg, upDate);
if (cell != null)
{
Invoke(new Action(() =>
{
cell.Value = "正在生成商品详情图 " + (DateTime.Now - sTime).TotalMilliseconds.ParseToInt();
}));
}
for (int i = 0; i < 20 && 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);
wxImgMapDic[BaseClass.GetMd5(gallery_item.url)] = upImgUrl;
isChangeImgDic = true;
continue;
}
else
{
}
}
}
catch (Exception ex)
{
}
// api上传图片文件 详情图
if (1 == 1)
{
try
{
string errMsg = "";
var imgBase64 = GetImgBase64ForApi(out errMsg, gallery_item.url, needAddRandomBlock, wantAddRandomBlock);
if (!string.IsNullOrEmpty(errMsg))
{
throw new Exception(errMsg);
}
var urlImg = BaseClass.BuildWXImgObjString("product/wx_api_upload_image");
var upDate = UserLogin.GetSignaturex();
upDate += "&token=" + token.accessToken;
//upDate += "&is_file=true";
//if (needAddRandomBlock || wantAddRandomBlock)
// upDate += "&random_code=true";
upDate += "&img_url=" + gallery_item.url;
upDate += "&img_base64=" + imgBase64;
var sTime = DateTime.Now;
var apiUpImgFileRst = GetHtml.PostUtf8(urlImg, upDate);
if (cell != null)
{
Invoke(new Action(() =>
{
cell.Value = "正在上传商品详情图文件 " + (DateTime.Now - sTime).TotalMilliseconds.ParseToInt();
}));
}
for (int i = 0; i < 5 && apiUpImgFileRst.Contains("access_token expired"); i++)
{
token.mdyTime = DateTime.Now.AddDays(-100);
token = new WxApiBLL().GetRandomWxToken();
apiUpImgFileRst = GetHtml.PostUtf8(urlImg, upDate);
}
var upImgUrl = BaseRegex.GetStringJson(ref apiUpImgFileRst, "img_url");
if (apiUpImgFileRst.Contains("\"errcode\":0") && !string.IsNullOrEmpty(upImgUrl))
{
upProdParam.product.info.detail.detailImg.Add(upImgUrl);
wxImgMapDic[BaseClass.GetMd5(gallery_item.url)] = upImgUrl;
isChangeImgDic = true;
continue;
}
else
{
}
}
catch (Exception ex)
{
throw new Exception("修改详情图失败 " + ex.Message);
}
}
// 模拟上传图片
bool randomDrawBlock = false;
if (needAddRandomBlock || wantAddRandomBlock)
randomDrawBlock = true;
var locationStr = UploadImg(shop, gallery_item.url, cell, randomDrawBlock);
if (!locationStr.Contains("\"success\":true"))
throw new Exception(locationStr);
var location = BaseRegex.GetStringJson(ref locationStr, "Location");
upProdParam.product.info.detail.detailImg.Add(location);
//wxImgMapDic[BaseClass.GetMd5(gallery_item.url)] = location;
//isChangeImgDic = true;
Thread.Sleep(Randomx.GetId(10, 50));
}
}
}