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 UploadDetailImagesStep : 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.ProductParameter.product.info.detail = new productDetailParam(); // 详情图 context.Parameters.DetailGalleries = context.Parameters.ProdInfo.detail.galleries.Where(g => g.type == 2).ToList(); if (context.Parameters.DetailGalleries.Count == 0) context.Parameters.DetailGalleries.Add(new gallery_item() { type = 2, url = context.Parameters.ProdInfo.detail.galleries.FirstOrDefault(g => g.type == 1)?.url }); foreach (var gallery_item in context.Parameters.DetailGalleries) { try { if (context.Product.ProductGetTypeText == "微信店采") { context.Parameters.ProductParameter.product.info.detail.detailImg.Add(gallery_item.url); continue; } // 获取缓存盗链 详情图 if (context.Parameters.WxImgMapDic.ContainsKey(BaseClass.GetMd5(gallery_item.url))) { context.Parameters.ProductParameter.product.info.detail.detailImg.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.ProductParameter.product.info.detail.detailImg.Add(upImgUrl); context.Parameters.WxImgMapDic[BaseClass.GetMd5(gallery_item.url)] = upImgUrl; context.Parameters.IsChangeImgDic = true; continue; } else { } } } catch (Exception ex) { } // api上传图片文件 详情图 if (1 == 1) { try { string errMsg = ""; var imgBase64 = GetImgBase64ForApi(out errMsg, gallery_item.url, context.Parameters.NeedAddRandomBlock, context.Parameters.WantAddRandomBlock); 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.ProductParameter.product.info.detail.detailImg.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); } } // 模拟上传图片 bool randomDrawBlock = false; if (context.Parameters.NeedAddRandomBlock || context.Parameters.WantAddRandomBlock) randomDrawBlock = true; var locationStr = UploadImg(context.Parameters.Shop, gallery_item.url, randomDrawBlock); if (!locationStr.Contains("\"success\":true")) throw new Exception(locationStr); var location = BaseRegex.GetStringJson(ref locationStr, "Location"); context.Parameters.ProductParameter.product.info.detail.detailImg.Add(location); //wxImgMapDic[BaseClass.GetMd5(gallery_item.url)] = location; //isChangeImgDic = true; Thread.Sleep(Randomx.GetId(10, 50)); } return Task.CompletedTask; } }