Files
KJD/Store.Product/Publish/Steps/Wx/GenerateAiProductImageStep.cs
T
2026-09-14 09:16:59 +08:00

147 lines
4.9 KiB
C#

using AllBase;
using AllBase.Extension;
using NaiBa.Util.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 Work;
namespace Store.Product.Publish.Steps.Wx;
/// <summary>
/// 由 WXDBProdUploadForm.UploadProduct 的“AI生成商品裂变图片”区段迁移。
/// TODO(迁移): 原区段中的 TEMUKJD/WXUpload/窗体服务引用需按 Store.Product 运行时适配逐项核对。
/// </summary>
public sealed class GenerateAiProductImageStep : WxProductPublishStepBase
{
public override string Description => "AI生成商品裂变图片";
protected override Task ExecuteProvider(WXPublishContext context, CancellationToken cancellationToken)
{
if (context.Parameters.IsAiImage)
{
int imgCount = 1;
Invoke(new Action(() =>
{
if (context.Progress != null)
context.Progress.Report("正在Ai生图");
imgCount = context.Parameters.AiImageCount;
if (imgCount == 0)
imgCount = 1;
}));
List<Task> tList = new List<Task>();
List<string> errStrLst = new List<string>();
for (int n = 0; n < context.Parameters.ProductParameter.product.info.headImg.Count && n < imgCount; n++)
{
while (tList.Count >= 10)
{
Thread.Sleep(500);
tList.RemoveAll(t => t.IsCanceled || t.IsFaulted || t.IsCompleted);
}
var tt = Task.Run(async () =>
{
var imgUrl = GetAiImg(context.Parameters.ProductParameter.product.info.headImg[n], context.Parameters.ProductParameter.product.info.shortTitle);
if (imgUrl.StartsWith("生成Ai图片错误"))
{
errStrLst.Add(imgUrl);
return;
//throw new Exception("" + imgUrl);
}
// api上传图片文件 Ai主图
if (1 == 1)
{
try
{
string errMsg = "";
//var imgBase64 = GetImgBase64ForApi(out errMsg, imgUrl, needAddRandomBlock, wantAddRandomBlock, 800, 800);
var imgBase64 = GetImgBase64ForApi(out errMsg, imgUrl, false, false, 800, 800);
if (!string.IsNullOrEmpty(errMsg))
{
errStrLst.Add(errMsg);
return;
//return errMsg;
}
var urlImg = BaseClass.BuildWXImgObjString("product/wx_api_upload_image");
var upDate = UserLogin.GetSignaturex();
upDate += "&token=" + context.Parameters.Token.accessToken;
//upDate += "&is_file=true";
//if (needAddRandomBlock || wantAddRandomBlock)
// upDate += "&random_code=true";
upDate += "&img_url=" + imgUrl;
upDate += "&img_base64=" + imgBase64;
var sTime = DateTime.Now;
var apiUpImgFileRst = GetHtml.PostUtf8(urlImg, upDate);
if (context.Progress != null)
{
Invoke(new Action(() =>
{
context.Progress.Report("正在上传Ai主图文件 " + (DateTime.Now - sTime).TotalMilliseconds.ParseToInt());
}));
}
for (int i = 0; i < 5 && apiUpImgFileRst.Contains("access_token expired"); i++)
{
context.Parameters.Token.mdyTime = DateTime.Now.AddDays(-100);
context.Parameters.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))
{
context.Parameters.ProductParameter.product.info.headImg[n] = upImgUrl;
//if (n == 0)
// upProdParam.product.info.detail.detailImg.Insert(0, upImgUrl);
}
else
{
string upImgErrMsg = BaseRegex.GetStringJsonByRegs(ref apiUpImgFileRst, apiUpImgFileRst, "msg", "errMsg", "errorMsg", "err_msg", "error_msg");
errStrLst.Add("上传Ai图失败 " + upImgErrMsg);
return;
//throw new Exception("上传Ai图失败 " + upImgErrMsg);
}
}
catch (Exception ex)
{
errStrLst.Add("上传Ai图失败 " + ex.Message);
return;
//throw new Exception("上传Ai图失败 " + ex.Message);
}
}
});
tList.Add(tt);
}
while (tList.Count > 0)
{
Thread.Sleep(500);
tList.RemoveAll(t => t.IsCanceled || t.IsFaulted || t.IsCompleted);
}
// 错误超过一定数量直接报错返回
if (errStrLst.Count > 0 && errStrLst.Count >= imgCount / 3)
{
throw new Exception(string.Join(",", errStrLst));
}
}
return Task.CompletedTask;
}
}