245 lines
9.2 KiB
C#
245 lines
9.2 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 的“上传主图”区段迁移。
|
|
/// TODO(迁移): 原区段中的 TEMUKJD/WXUpload/窗体服务引用需按 Store.Product 运行时适配逐项核对。
|
|
/// </summary>
|
|
public sealed class UploadMainImagesStep : WxProductPublishStepBase
|
|
{
|
|
public override string Description => "上传主图";
|
|
|
|
protected override Task ExecuteProvider(WXPublishContext context, CancellationToken cancellationToken)
|
|
{
|
|
if (context.Progress != null)
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
context.Progress.Report("正在生成主图并识别商品");
|
|
}));
|
|
}
|
|
Thread.Sleep(Randomx.GetId(100, 300));
|
|
context.Parameters.MainGalleries = context.Parameters.ProdInfo.detail.galleries.Where(g => g.type == 1).Take(9).ToList();
|
|
foreach (var gallery_item in context.Parameters.MainGalleries) // 9张主图
|
|
{
|
|
try
|
|
{
|
|
if (context.Product.ProductGetTypeText == "微信店采")
|
|
{
|
|
context.Parameters.InitParameter.product.info.headImg.Add(gallery_item.url);
|
|
context.Parameters.ProductParameter.product.info.headImg.Add(gallery_item.url);
|
|
continue;
|
|
}
|
|
|
|
// 获取缓存盗链 主图
|
|
if (context.Parameters.WxImgMapDic.ContainsKey(BaseClass.GetMd5(gallery_item.url)))
|
|
{
|
|
context.Parameters.InitParameter.product.info.headImg.Add(context.Parameters.WxImgMapDic[BaseClass.GetMd5(gallery_item.url)]);
|
|
context.Parameters.ProductParameter.product.info.headImg.Add(context.Parameters.WxImgMapDic[BaseClass.GetMd5(gallery_item.url)]);
|
|
continue;
|
|
}
|
|
|
|
if (context.Parameters.NeedAddRandomBlock || context.Parameters.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=" + context.Parameters.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 (context.Progress != null)
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
context.Progress.Report("正在生成主图并识别商品 " + (DateTime.Now - sTime).TotalMilliseconds.ParseToInt());
|
|
}));
|
|
}
|
|
|
|
for (int i = 0; i < 20 && upDateRst.Contains("access_token expired"); i++)
|
|
{
|
|
context.Parameters.Token.mdyTime = DateTime.Now.AddDays(-100);
|
|
context.Parameters.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))
|
|
{
|
|
context.Parameters.InitParameter.product.info.headImg.Add(upImgUrl);
|
|
context.Parameters.ProductParameter.product.info.headImg.Add(upImgUrl);
|
|
context.Parameters.WxImgMapDic[BaseClass.GetMd5(gallery_item.url)] = upImgUrl;
|
|
context.Parameters.IsChangeImgDic = true;
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
|
|
// api上传图片文件 主图
|
|
if (1 == 1)
|
|
{
|
|
try
|
|
{
|
|
string errMsg = "";
|
|
var imgBase64 = GetImgBase64ForApi(out errMsg, gallery_item.url, context.Parameters.NeedAddRandomBlock, context.Parameters.WantAddRandomBlock, 800, 800);
|
|
if (!string.IsNullOrEmpty(errMsg))
|
|
{
|
|
throw new Exception(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=" + gallery_item.url;
|
|
upDate += "&img_base64=" + imgBase64;
|
|
|
|
var sTime = DateTime.Now;
|
|
var apiUpImgFileRst = GetHtml.PostUtf8(urlImg, upDate);
|
|
if (context.Progress != null)
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
context.Progress.Report("正在上传主图文件并识别商品 " + (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.InitParameter.product.info.headImg.Add(upImgUrl);
|
|
context.Parameters.ProductParameter.product.info.headImg.Add(upImgUrl);
|
|
context.Parameters.WxImgMapDic[BaseClass.GetMd5(gallery_item.url)] = upImgUrl;
|
|
context.Parameters.IsChangeImgDic = true;
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("修改主图失败 " + ex.Message);
|
|
}
|
|
}
|
|
|
|
// 模拟上传图片
|
|
if (1 == 1)
|
|
{
|
|
var upImgAuthRst = WXUpload.getUploadTempImgAuth(context.Parameters.Shop);
|
|
for (int i = 0; i < 10 && upImgAuthRst.Contains("LogicError"); i++)
|
|
{
|
|
Invoke(new Action(() =>
|
|
{
|
|
context.Progress.Report("图片提报失败,正在重试。" + (i + 1));
|
|
}));
|
|
Thread.Sleep(Randomx.GetId(3000, 6000));
|
|
upImgAuthRst = WXUpload.getUploadTempImgAuth(context.Parameters.Shop);
|
|
}
|
|
if (!upImgAuthRst.Contains("\"code\":0"))
|
|
{
|
|
var error = BaseRegex.GetStringJson(ref upImgAuthRst, "msg");
|
|
if (string.IsNullOrEmpty(error))
|
|
error = upImgAuthRst;
|
|
throw new Exception("申请上传主图失败 " + GetHtml.UrlEncodeUtf8(error));
|
|
}
|
|
string disK = BaseRegex.GetStringJson(ref upImgAuthRst, "disK");
|
|
long disT = BaseRegex.GetLongJson(ref upImgAuthRst, "disT");
|
|
string ext = BaseRegex.GetStringJson(ref upImgAuthRst, "ext");
|
|
string imgBase64 = "";
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------
|
|
//生成主图base64字符串
|
|
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)
|
|
{
|
|
throw new Exception("初始化主图上传失败");
|
|
}
|
|
try
|
|
{
|
|
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 (context.Parameters.NeedAddRandomBlock || context.Parameters.WantAddRandomBlock)
|
|
{
|
|
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));
|
|
}
|
|
}
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
{
|
|
imageBg.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
|
|
byte[] imageBytes = memoryStream.ToArray();
|
|
imgBase64 = Convert.ToBase64String(imageBytes);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("修改主图失败 " + ex.Message);
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------------------------------------------------------------
|
|
//上传主图获取路径
|
|
var upImgRst = WXUpload.adtimageupload(context.Parameters.Shop, imgBase64, disK, disT, ext).ParseToString();
|
|
var location = BaseRegex.GetStringJson(ref upImgRst, "Location");
|
|
if (string.IsNullOrEmpty(location))
|
|
{
|
|
var error = BaseRegex.GetStringJson(ref upImgAuthRst, "msg");
|
|
if (string.IsNullOrEmpty(error))
|
|
error = upImgAuthRst;
|
|
throw new Exception("主图上传失败 " + error);
|
|
}
|
|
context.Parameters.InitParameter.product.info.headImg.Add(location);
|
|
context.Parameters.ProductParameter.product.info.headImg.Add(location);
|
|
//wxImgMapDic[BaseClass.GetMd5(gallery_item.url)] = location;
|
|
//isChangeImgDic = true;
|
|
Thread.Sleep(Randomx.GetId(10, 50));
|
|
}
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|