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; /// /// 由 WXDBProdUploadForm.UploadProduct 的“为图片添加随机外框”区段迁移。 /// TODO(迁移): 原区段中的 TEMUKJD/WXUpload/窗体服务引用需按 Store.Product 运行时适配逐项核对。 /// public sealed class AddRandomImageBorderStep : WxProductPublishStepBase { public override string Description => "为图片添加随机外框"; protected override Task ExecuteProvider(WXPublishContext context, CancellationToken cancellationToken) { if (context.Parameters.IsFissionTitle && context.Parameters.IsRandomImageBorder) { // selectProd.isFission int mainCount = 9; int detailCount = 1; Invoke(new Action(() => { mainCount = context.Parameters.MainBorderImageCount; detailCount = context.Parameters.DetailBorderImageCount; })); // api上传图片文件 边框主图 if (1 == 1) { if (context.Parameters.IsRandomMainImageOrder) context.Parameters.ProductParameter.product.info.headImg = context.Parameters.ProductParameter.product.info.headImg.OrderBy(x => Guid.NewGuid()).Take(100).ToList(); for (int n = 0; n < context.Parameters.ProductParameter.product.info.headImg.Count && n < mainCount; n++) { var imgUrl = context.Parameters.ProductParameter.product.info.headImg[n]; try { string errMsg = ""; //var imgBase64 = GetImgBase64ForApi(out errMsg, imgUrl, needAddRandomBlock, wantAddRandomBlock, 800, 800); var imgBase64 = GetImgBase64ForApi(out errMsg, imgUrl, false, false, 800, 800, true); 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=" + 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("正在优化主图文件 " + (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; } else { string upImgErrMsg = BaseRegex.GetStringJsonByRegs(ref apiUpImgFileRst, apiUpImgFileRst, "msg", "errMsg", "errorMsg", "err_msg", "error_msg"); throw new Exception("优化主图失败 " + upImgErrMsg); } } catch (Exception ex) { throw new Exception("优化主图失败 " + ex.Message); } } } // api上传图片文件 边框详情图 if (1 == 1) { if (context.Parameters.IsRandomDetailImageOrder) context.Parameters.ProductParameter.product.info.detail.detailImg = context.Parameters.ProductParameter.product.info.detail.detailImg.OrderBy(x => Guid.NewGuid()).Take(100).ToList(); for (int n = 0; n < context.Parameters.ProductParameter.product.info.detail.detailImg.Count && n < detailCount; n++) { var imgUrl = context.Parameters.ProductParameter.product.info.detail.detailImg[n]; try { string errMsg = ""; //var imgBase64 = GetImgBase64ForApi(out errMsg, imgUrl, needAddRandomBlock, wantAddRandomBlock, 800, 800); var imgBase64 = GetImgBase64ForApi(out errMsg, imgUrl, false, false, 800, 800, true); 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=" + 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("正在优化详情图文件 " + (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.detail.detailImg[n] = upImgUrl; } else { string upImgErrMsg = BaseRegex.GetStringJsonByRegs(ref apiUpImgFileRst, apiUpImgFileRst, "msg", "errMsg", "errorMsg", "err_msg", "error_msg"); throw new Exception("优化详情图失败 " + upImgErrMsg); } } catch (Exception ex) { throw new Exception("优化详情图失败 " + ex.Message); } } } } return Task.CompletedTask; } }