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 { /// /// 商品详情 /// public class ProductContent { /// /// 备份数据 /// private string _backcontent = ""; public ProductContent(string content) { _backcontent = content; _content = content; ConvertContent(); } private string _content = ""; public override string ToString() { return GetContent(); } /// /// 详情图片集合 /// public List Images { get; } = new List(); /// /// 删除详情图片 /// /// 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(@"]*?\bsrc[\s]*=[\s]*[""']?[\s]*(?[^""'>]*)[^>]*?/?[\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 = $""; tempContent = tempContent.Replace(value, imgString); } return tempContent; } /// /// 图片标记 /// /// /// private string GetImgTag(ContentImage img) { return "${" + img.Gid + "}"; } } }