102 lines
3.0 KiB
C#
102 lines
3.0 KiB
C#
using Store.Product.Models.Product.Image;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Store.Product.Models.Product
|
|
{
|
|
/// <summary>
|
|
/// 商品详情
|
|
/// </summary>
|
|
public class ProductContent
|
|
{
|
|
/// <summary>
|
|
/// 备份数据
|
|
/// </summary>
|
|
private string _backcontent = "";
|
|
public ProductContent(string content)
|
|
{
|
|
_backcontent = content;
|
|
_content = content;
|
|
ConvertContent();
|
|
}
|
|
|
|
|
|
private string _content = "";
|
|
public override string ToString()
|
|
{
|
|
return GetContent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 详情图片集合
|
|
/// </summary>
|
|
public List<ContentImage> Images { get; } = new List<ContentImage>();
|
|
|
|
/// <summary>
|
|
/// 删除详情图片
|
|
/// </summary>
|
|
/// <param name="image"></param>
|
|
public void DelImage(ContentImage image)
|
|
{
|
|
var item = Images.FirstOrDefault(a => a.Gid == image.Gid);
|
|
if (item != null)
|
|
{
|
|
string value = GetImgTag(item);
|
|
_content.Replace(value, "");
|
|
Images.Remove(item);
|
|
}
|
|
}
|
|
|
|
private void ConvertContent()
|
|
{
|
|
Regex regImg = new Regex(@"<img\b[^>]*?\bsrc[\s]*=[\s]*[""']?[\s]*(?<imgUrl>[^""'>]*)[^>]*?/?[\s]*>");
|
|
if (regImg.IsMatch(_backcontent))
|
|
{
|
|
MatchCollection matchs = regImg.Matches(_backcontent);
|
|
int MatchMax = matchs.Count;
|
|
for (int i = MatchMax - 1; i >=0; i--)
|
|
{
|
|
Match item = matchs[i];
|
|
if (item.Success)
|
|
{
|
|
Group urlgroup = item.Groups["imgUrl"];
|
|
if (urlgroup.Success)
|
|
{
|
|
var cimg = new ContentImage(urlgroup.Value);
|
|
Images.Add(cimg);
|
|
|
|
_content = _content.Remove(item.Index, item.Length);
|
|
_content = _content.Insert(item.Index, GetImgTag(cimg));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private string GetContent()
|
|
{
|
|
var tempContent = _content;
|
|
foreach (var item in Images)
|
|
{
|
|
string value = GetImgTag(item);
|
|
string imgString = $"<img src=\"{item.UploadImgUrl}\" width=\"100%\" />";
|
|
tempContent = tempContent.Replace(value, imgString);
|
|
}
|
|
return tempContent;
|
|
}
|
|
/// <summary>
|
|
/// 图片标记
|
|
/// </summary>
|
|
/// <param name="img"></param>
|
|
/// <returns></returns>
|
|
private string GetImgTag(ContentImage img)
|
|
{
|
|
return "${" + img.Gid + "}";
|
|
}
|
|
}
|
|
}
|