This commit is contained in:
chfei authored and chfei committed 2026-09-14 09:16:59 +08:00
1 parent 0a3e5af6a9
commit c2dcedd238
59 files changed
+2053 -1274

No files matched your search

@@ -1,5 +1,6 @@
using AllBase;
using AllBase.Extension;
using NaiBa.Util.Extension;
using AllBase.Helper;
using AllBase.Model.DBModel;
using AllBase.Model.Param.WXParam;
@@ -14,209 +15,145 @@ using Store.Product.Publish;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Work;
namespace Store.Product.Publish.Steps.Wx;
/// <summary>
/// 微信商品发布步骤基类。
/// 该基类把原窗体方法的可变状态、控件快照和窗体能力适配集中到一个入口,
/// 让各 Step 尽量保持与原 UploadProduct 区段一致的代码,便于逐段核对。
/// 微信商品发布步骤基类。所有步骤均不依赖 WinForms 控件。
/// </summary>
public abstract class WxProductPublishStepBase : PublishStep<WXPublishContext>
{
protected WXApi WXApi => LazyServiceProvider.Instance.LazyGetService<WXApi>();
protected static void Invoke(Action action) => action?.Invoke();
private PublishContextBase _context;
protected WxProductUploadParam Upload => _context.WxUpload;
protected WxProductPublishState State => Upload.State;
protected WxProductUploadOptions Options => Upload.Options;
protected WxProductUploadFormState Form => Options.Form;
protected dynamic RuntimeHost => Upload.Runtime.Host;
protected ShopLoginInfo shop => Upload.Source.Shop;
protected KJDDBProduct dbProd => Upload.Source.DbProduct;
protected TemuDBProductSelect selectProd => Upload.Source.SelectProduct;
protected DateTime startTime => Upload.Source.StartTime;
protected WXUploadProductInitParam upProdInitParam => Upload.InitParameter;
protected WXUploadProductParam upProdParam => Upload.ProductParameter;
// TODO(迁移): cell/cancellationTokenSource 原为 WinForms 对象,这里通过代理回调保持步骤代码形状。
protected dynamic cell => Upload.Runtime.Cell;
protected dynamic cancellationTokenSource => Upload.Runtime.CancellationTokenSource;
protected dynamic chkFissionGoodsName => Form.chkFissionGoodsName;
protected dynamic txtFissionTitle => Form.txtFissionTitle;
protected dynamic chkSkipUploaded => Form.chkSkipUploaded;
protected dynamic chkCommission => Form.chkCommission;
protected dynamic cmbCommissionAddOrReduce => Form.cmbCommissionAddOrReduce;
protected dynamic txtCommissionChangeValue => Form.txtCommissionChangeValue;
protected dynamic txtMinUpCommission => Form.txtMinUpCommission;
protected dynamic txtMaxUpCommission => Form.txtMaxUpCommission;
protected dynamic chkImgRandomBlock => Form.chkImgRandomBlock;
protected dynamic chkRandomMainImgOrder => Form.chkRandomMainImgOrder;
protected dynamic chkRandomDetailImgOrder => Form.chkRandomDetailImgOrder;
protected dynamic chkFissionRandomPriceChange => Form.chkFissionRandomPriceChange;
protected dynamic txtFissionRandomPriceChange => Form.txtFissionRandomPriceChange;
protected dynamic chkHighestPriceSku => Form.chkHighestPriceSku;
protected dynamic chkLowestPriceSku => Form.chkLowestPriceSku;
protected dynamic chkSameSpecPriceUp => Form.chkSameSpecPriceUp;
protected dynamic chkSameSpecPriceDown => Form.chkSameSpecPriceDown;
protected dynamic txtMinPrice => Form.txtMinPrice;
protected dynamic txtMaxPrice => Form.txtMaxPrice;
protected dynamic txtReplaceText => Form.txtReplaceText;
protected dynamic chkSkipReplaceText => Form.chkSkipReplaceText;
protected dynamic chkRemoveSpecialChar => Form.chkRemoveSpecialChar;
protected dynamic chkUseWXComplateProd => Form.chkUseWXComplateProd;
protected dynamic chkUseRecommand => Form.chkUseRecommand;
protected dynamic cmbSpBrandName => Form.cmbSpBrandName;
protected dynamic txtFilterBoundMax => Form.txtFilterBoundMax;
protected dynamic txtFilterBoundMin => Form.txtFilterBoundMin;
protected dynamic txtMainBorderImgCount => Form.txtMainBorderImgCount;
protected dynamic txtDetailBorderImgCount => Form.txtDetailBorderImgCount;
protected dynamic chkRandomImgBorder => Form.chkRandomImgBorder;
protected dynamic chkAiImage => Form.chkAiImage;
protected dynamic txtAiImgCount => Form.txtAiImgCount;
protected dynamic chkFreightInsurance => Form.chkFreightInsurance;
protected dynamic chkUseShanghaiReturnOrderAddress => Form.chkUseShanghaiReturnOrderAddress;
protected dynamic chkPreSale => Form.chkPreSale;
protected dynamic chkDefaultBestBefore => Form.chkDefaultBestBefore;
protected dynamic txtDefaultBestBefore => Form.txtDefaultBestBefore;
protected dynamic chkTestNoAuditUpload => Form.chkTestNoAuditUpload;
protected dynamic chkUpDraft => Form.chkUpDraft;
protected dynamic chkIsSubmitSuccess => Form.chkIsSubmitSuccess;
protected dynamic dgvShop => Form.dgvShop;
protected int commissionRate { get => State.CommissionRate; set => State.CommissionRate = value; }
protected int minCommissionRate { get => State.MinCommissionRate; set => State.MinCommissionRate = value; }
protected int maxCommissionRate { get => State.MaxCommissionRate; set => State.MaxCommissionRate = value; }
protected int commissionRateChange { get => State.CommissionRateChange; set => State.CommissionRateChange = value; }
protected int? fromCommissionRate { get => State.FromCommissionRate; set => State.FromCommissionRate = value; }
protected WXGetEditProductInfoLite wxEditProdInfo { get => State.WxEditProdInfo; set => State.WxEditProdInfo = value; }
protected JObject prodJObj { get => State.ProdJObject; set => State.ProdJObject = value; }
protected bool isChangeImgDic { get => State.IsChangeImgDic; set => State.IsChangeImgDic = value; }
protected Dictionary<string, string> wxImgMapDic { get => State.WxImgMapDic; set => State.WxImgMapDic = value; }
protected PDDProductInfo prodInfo { get => State.ProdInfo; set => State.ProdInfo = value; }
protected int mainImgCount { get => State.MainImgCount; set => State.MainImgCount = value; }
protected bool needAddRandomBlock { get => State.NeedAddRandomBlock; set => State.NeedAddRandomBlock = value; }
protected bool wantAddRandomBlock { get => State.WantAddRandomBlock; set => State.WantAddRandomBlock = value; }
protected WXShopTokenInfo token { get => State.Token; set => State.Token = value; }
protected string prodTitle { get => State.ProdTitle; set => State.ProdTitle = value; }
protected string extCode { get => State.ExtCode; set => State.ExtCode = value; }
protected List<gallery_item> mainGalleries { get => State.MainGalleries; set => State.MainGalleries = value; }
protected List<long> aiCateIdLst { get => State.AiCateIdLst; set => State.AiCateIdLst = value; }
protected string getCateRst { get => State.GetCateRst; set => State.GetCateRst = value; }
protected JObject jCateRst { get => State.JCateRst; set => State.JCateRst = value; }
protected long leafCatId { get => State.LeafCatId; set => State.LeafCatId = value; }
protected WxCatBoundInfo bound { get => State.Bound; set => State.Bound = value; }
protected int maxBount { get => State.MaxBound; set => State.MaxBound = value; }
protected int minBount { get => State.MinBound; set => State.MinBound = value; }
protected string spBrandId { get => State.SpBrandId; set => State.SpBrandId = value; }
protected string spBrandName { get => State.SpBrandName; set => State.SpBrandName = value; }
protected string shippingTemplateJson { get => State.ShippingTemplateJson; set => State.ShippingTemplateJson = value; }
protected int shipPrice { get => State.ShipPrice; set => State.ShipPrice = value; }
protected string expressRst { get => State.ExpressRst; set => State.ExpressRst = value; }
protected List<gallery_item> detailGalleries { get => State.DetailGalleries; set => State.DetailGalleries = value; }
protected WXUploadProductInfoRecommendParam recommendParamRst { get => State.RecommendParamRst; set => State.RecommendParamRst = value; }
protected allAttrParam productAttr { get => State.ProductAttr; set => State.ProductAttr = value; }
protected int maxSkuSpecCnt { get => State.MaxSkuSpecCnt; set => State.MaxSkuSpecCnt = value; }
protected int skuIndex { get => State.SkuIndex; set => State.SkuIndex = value; }
protected int id { get => State.Id; set => State.Id = value; }
protected long catId { get => State.CatId; set => State.CatId = value; }
protected long fCatId { get => State.FCatId; set => State.FCatId = value; }
protected long valueCatId { get => State.ValueCatId; set => State.ValueCatId = value; }
protected Dictionary<string, long> dicSpecParentNameId { get => State.DicSpecParentNameId; set => State.DicSpecParentNameId = value; }
protected Dictionary<string, long> dicSpecParentNameCatId { get => State.DicSpecParentNameCatId; set => State.DicSpecParentNameCatId = value; }
protected Dictionary<string, long> dicSpecNamefCatId { get => State.DicSpecNameFCatId; set => State.DicSpecNameFCatId = value; }
protected Dictionary<string, long> dicSpecNameCatId { get => State.DicSpecNameCatId; set => State.DicSpecNameCatId = value; }
protected string uploadDraftRst { get => State.UploadDraftRst; set => State.UploadDraftRst = value; }
protected string commissionRstStr { get => State.CommissionRstStr; set => State.CommissionRstStr = value; }
protected string DefaultAdultBrandId { get => State.DefaultAdultBrandId; set => State.DefaultAdultBrandId = value; }
protected bool IsWxNotUseSoftwareDefaultAddressOnce { get => State.IsWxNotUseSoftwareDefaultAddressOnce; set => State.IsWxNotUseSoftwareDefaultAddressOnce = value; }
protected List<WXShopTokenInfo> lstWXShopTokenInfo { get => State.WxShopTokenInfoList; set => State.WxShopTokenInfoList = value; }
protected List<wx_qualification_image_info> lstQulificationImage { get => State.QualificationImages; set => State.QualificationImages = value; }
protected List<string> forceFilteKeyWords { get => State.ForceFilterKeywords; set => State.ForceFilterKeywords = value; }
protected dynamic lstAddPriceRule => State.AddPriceRules;
protected dynamic wxUploadRule => State.WxUploadRule;
protected string pdd_product_cates { get => State.PddProductCates; set => State.PddProductCates = value; }
protected dynamic upload_draft_product_lst => State.UploadDraftProductList;
/// <summary>
/// 正常结束发布流程(例如提交成功、指定上传到草稿箱)时写入最终结果并停止后续步骤。
/// 失败场景请直接抛出异常。
/// </summary>
protected void StopWithResult(string result)
protected static string GetEC1ExtCode(string spuId)
{
Upload.Result = result;
Upload.IsCompleted = true;
}
protected void Invoke(Action action)
{
action?.Invoke();
}
protected string UploadImg(ShopLoginInfo shopInfo, string imgUrl, object cellState = null, bool randomBlock = false)
{
return Upload.Runtime.UploadImgByUrl?.Invoke(shopInfo, imgUrl, cellState ?? cell, randomBlock) ?? "";
}
protected string UploadImg(ShopLoginInfo shopInfo, Bitmap image, object cellState = null)
{
return Upload.Runtime.UploadImgByBitmap?.Invoke(shopInfo, image, cellState ?? cell) ?? "";
}
protected string GetImgBase64ForApi(out string errorMsg, string imgUrl, bool needAddRandomBlock, bool wantAddRandomBlock,
int imgWidth = 0, int imgHeight = 0, bool wantAddRandomBorder = false)
{
if (Upload.Runtime.GetImgBase64ForApi == null)
{
errorMsg = "GetImgBase64ForApi 尚未从窗体映射到新上下文";
return "";
}
return Upload.Runtime.GetImgBase64ForApi(out errorMsg, imgUrl, needAddRandomBlock, wantAddRandomBlock, imgWidth, imgHeight, wantAddRandomBorder);
}
protected string GetAiImg(string imgUrl, string imgWord)
{
return Upload.Runtime.GetAiImg?.Invoke(imgUrl, imgWord) ?? "";
if (spuId.IsNullOrEmpty()) throw new Exception("货号指定失败,值不能为空!");
return !spuId.StartsWith("EC1") && !spuId.EndsWith("A") ? "EC1" + spuId + "A" : spuId;
}
protected static string GetNewExtCode(string spuId)
{
if (spuId.IsNullOrEmpty())
throw new Exception("货号指定失败,值不能为空!");
if (!spuId.StartsWith("P1-"))
return "P1-" + spuId;
return spuId;
if (spuId.IsNullOrEmpty()) throw new Exception("货号指定失败,值不能为空!");
return !spuId.StartsWith("P1-") ? "P1-" + spuId : spuId;
}
protected static string GetEC1ExtCode(string spuId)
protected static WXGetEditProductInfoLite GetProductWXComplate(string secretSpuId, out string errMsg)
{
if (spuId.IsNullOrEmpty())
throw new Exception("货号指定失败,值不能为空!");
if (!spuId.StartsWith("EC1") && !spuId.EndsWith("A"))
return "EC1" + spuId + "A";
return spuId;
errMsg = "";
if (string.IsNullOrEmpty(secretSpuId)) { errMsg = "产品编码为空"; return null; }
var url = BaseClass.BuildObjString("product/get_pdd_prod_wx_complate_list");
var data = UserLogin.GetSignaturex() + "&search_secret_spu_ids=" + secretSpuId;
var html = GetHtml.PostUtf8(url, data);
if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) html = GetHtml.PostUtf8(url, data);
if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")) html = GetHtml.PostUtf8(url, data);
if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200"))
{
errMsg = BaseRegex.GetStringJson(ref html, "msg");
if (errMsg.IsNullOrEmpty()) errMsg = html;
errMsg = "取回采数据失败 " + errMsg;
return null;
}
var array = JObject.Parse(html)["data"];
if (array == null || !array.Any()) { errMsg = "产品不存在"; return null; }
var item = array.FirstOrDefault()?["wx_edit_prod_json"];
if (item == null) return null;
try { return JsonHelper.DeserializeJsonToObject<WXGetEditProductInfoLite>(item.ParseToString()); }
catch (Exception ex) { errMsg = "回采数据转换失败 " + ex.Message; return null; }
}
// TODO(迁移): 以下两个窗体私有方法的真实实现仍留在 WXDBProdUploadForm,后续应迁到对应服务。
protected virtual void btnLoginAssistant_Click(object sender, EventArgs e)
protected string GetAiImg(string imgUrl, string imgWord)
{
throw new NotImplementedException("请把 WXDBProdUploadForm.btnLoginAssistant_Click 的实现迁入微信发布运行时适配。");
try
{
var time = BaseClass.DateTimeToTimestampSeconds(DateTime.Now).ToString();
var code = BaseClass.GetMd5("sadhjufhe15c4fbvFBvcvds+5sdfds@#%$xsc" + time).ToLower();
var url = "http://svn.jiaotian.vip:" + Randomx.GetId(20010, 20021) + "/index.php?obj=public/image";
var data = "time=" + time + "&code=" + code + "&image_url=" + imgUrl + "&word=" + imgWord;
var html = GetHtml.PostUtf8(url, data, "", 120);
return !string.IsNullOrEmpty(html) && !html.Contains("error") ? html : "生成Ai图片错误 " + html;
}
catch (Exception ex) { return "生成Ai图片错误 " + ex.Message; }
}
protected virtual WXGetEditProductInfoLite GetProductWXComplate(string secretSpuId, out string errMsg)
protected static string GetImgBase64ForApi(out string errorMsg, string imgUrl, bool needAddRandomBlock, bool wantAddRandomBlock, int imgWidth = 0, int imgHeight = 0, bool wantAddRandomBorder = false)
{
errMsg = "GetProductWXComplate 尚未迁移";
return null;
errorMsg = "";
try
{
var source = GetHtml.GetUrlImg(imgUrl, out var getImgError);
if (source == null || !string.IsNullOrEmpty(getImgError))
{
errorMsg = "初始化图片上传失败 " + getImgError;
return "";
}
var width = imgWidth > 0 ? imgWidth : source.Width;
var height = imgHeight > 0 ? imgHeight : source.Height;
using var target = new Bitmap(width, height);
using (var graphics = Graphics.FromImage(target))
{
graphics.DrawImage(source, new Rectangle(0, 0, width, height), 0, 0, source.Width, source.Height, GraphicsUnit.Pixel);
if (needAddRandomBlock || wantAddRandomBlock)
{
using var brush = new SolidBrush(Color.FromArgb(Randomx.GetId(256), Randomx.GetId(256), Randomx.GetId(256)));
graphics.FillRectangle(brush, new Rectangle(Randomx.GetId(Math.Max(1, width - 30)), Randomx.GetId(Math.Max(1, height - 30)), 30, 30));
}
if (wantAddRandomBorder)
{
using var brush = new SolidBrush(Color.FromArgb(Randomx.GetId(256), Randomx.GetId(256), Randomx.GetId(256)));
graphics.FillRectangle(brush, new Rectangle(0, 0, width, 20));
graphics.FillRectangle(brush, new Rectangle(0, height - 20, width, 20));
graphics.FillRectangle(brush, new Rectangle(0, 0, 20, height));
graphics.FillRectangle(brush, new Rectangle(width - 20, 0, 20, height));
}
}
using var stream = new MemoryStream();
target.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
return Convert.ToBase64String(stream.ToArray());
}
catch (Exception ex) { errorMsg = "修改图片失败 " + ex.Message; return ""; }
}
}
protected string UploadImg(ShopLoginInfo shop, string imgUrl, bool needAddRandomBlock = false)
{
var imgBase64 = GetImgBase64ForApi(out var errMsg, imgUrl, needAddRandomBlock, needAddRandomBlock, 800, 800);
if (!string.IsNullOrEmpty(errMsg)) throw new Exception(errMsg);
return UploadImgBase64(shop, imgBase64);
}
protected string UploadImg(ShopLoginInfo shop, Bitmap image)
{
using var stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
return UploadImgBase64(shop, Convert.ToBase64String(stream.ToArray()));
}
private static string UploadImgBase64(ShopLoginInfo shop, string imgBase64)
{
var authRst = WXUpload.getUploadTempImgAuth(shop);
if (!authRst.Contains("\"code\":0")) throw new Exception(authRst);
var disK = BaseRegex.GetStringJson(ref authRst, "disK");
var disT = BaseRegex.GetLongJson(ref authRst, "disT");
var ext = BaseRegex.GetStringJson(ref authRst, "ext");
return WXUpload.adtimageupload(shop, imgBase64, disK, disT, ext);
}
protected static void StopWithResult(string result)
{
throw new PublishCompletedException(result);
}
}
public sealed class PublishCompletedException : Exception
{
public string Result { get; }
public PublishCompletedException(string result) : base("发布步骤已正常结束") { Result = result; }
}