160 lines
6.4 KiB
C#
160 lines
6.4 KiB
C#
using AllBase;
|
|
using AllBase.Extension;
|
|
using NaiBa.Util.Extension;
|
|
using AllBase.Helper;
|
|
using AllBase.Model.DBModel;
|
|
using AllBase.Model.Param.WXParam;
|
|
using AllBase.Model.Result;
|
|
using AllBase.Model.Result.PDDResult;
|
|
using AllBase.Model.Result.WXResult;
|
|
using MAX.Core.DependencyInjection;
|
|
using Newtonsoft.Json.Linq;
|
|
using Store.Management.Framework.Apis;
|
|
using Store.Product.Models.PublishParameters.Wx;
|
|
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 Work;
|
|
|
|
namespace Store.Product.Publish.Steps.Wx;
|
|
|
|
/// <summary>
|
|
/// 微信商品发布步骤基类。所有步骤均不依赖 WinForms 控件。
|
|
/// </summary>
|
|
public abstract class WxProductPublishStepBase : PublishStep<WXPublishContext>
|
|
{
|
|
protected WXApi WXApi => LazyServiceProvider.Instance.LazyGetService<WXApi>();
|
|
|
|
protected static void Invoke(Action action) => action?.Invoke();
|
|
|
|
protected static string GetEC1ExtCode(string spuId)
|
|
{
|
|
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("货号指定失败,值不能为空!");
|
|
return !spuId.StartsWith("P1-") ? "P1-" + spuId : spuId;
|
|
}
|
|
|
|
protected static WXGetEditProductInfoLite GetProductWXComplate(string secretSpuId, out string errMsg)
|
|
{
|
|
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; }
|
|
}
|
|
|
|
protected string GetAiImg(string imgUrl, string imgWord)
|
|
{
|
|
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 static string GetImgBase64ForApi(out string errorMsg, string imgUrl, bool needAddRandomBlock, bool wantAddRandomBlock, int imgWidth = 0, int imgHeight = 0, bool wantAddRandomBorder = false)
|
|
{
|
|
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; }
|
|
}
|