ComplianceServer/oldcode/WeChatServerServices/WeiXin/GenHtml/GenHtmlHelper.cs

249 lines
8.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace WeChatServerServices.WeiXin.GenHtml
{
public class GenHtmlHelper
{
string AppendSign = "<!--$AppendKey:4c751cd8-b7da-4787-b166-818ab328146e-->";
#region
/// <summary>
/// 模板模型类
/// </summary>
public class WxTemplateModel
{
Dictionary<string, string> _WxTempData = new Dictionary<string, string>();
public Dictionary<string, string> WxTempData
{
get { return _WxTempData; }
set { _WxTempData = value; }
}
List<object> _WxForeach = new List<object>();
public List<object> WxForeach
{
get { return _WxForeach; }
set { _WxForeach = value; }
}
}
/// <summary>
/// 模板标签处理
/// </summary>
/// <param name="sb">模板文件</param>
/// <param name="model">生成文件的数据模型</param>
/// <param name="ForeachTemplate">模板中循环的标签代码块</param>
/// <returns></returns>
public StringBuilder ReplaceSign(ref StringBuilder sb, WxTemplateModel model, string ForeachTemplate)
{
StringBuilder s = sb;
try
{
//TempData标签处理
foreach (string key in model.WxTempData.Keys)
{
sb.Replace("$WxTempData[" + key + "]", model.WxTempData[key]);
}
string newHtml = ForeachTemplate;
newHtml = newHtml.Replace("$WxForeachStart", "<!--s-->");
newHtml = newHtml.Replace("$WxForeachEnd", "<!--e-->");
string tempstr = "";
//Foreach标签处理
if (!string.IsNullOrEmpty(ForeachTemplate))
{
foreach (var obj in model.WxForeach)
{
tempstr += newHtml;
var pinfo = obj.GetType().GetProperties();
foreach (var p in pinfo)
{
string v = string.Format("{0}", p.GetValue(obj));
string r = string.Format("$WxForeach.{0}", p.Name);
tempstr = tempstr.Replace(r, v);
}
}
}
sb.Replace(ForeachTemplate, tempstr);
}
catch (Exception ex)
{
throw ex;
}
return sb;
}
/// <summary>
/// 追加记录(扩展方法)
/// </summary>
/// <param name="sb"></param>
/// <param name="model"></param>
/// <param name="ForeachTemplate"></param>
/// <returns></returns>
public StringBuilder AppendExt(ref StringBuilder sb, WxTemplateModel model, string ForeachTemplate)
{
StringBuilder s = sb;
try
{
string newHtml = ForeachTemplate;
newHtml = newHtml.Replace("$WxForeachStart", "<!--s-->");
newHtml = newHtml.Replace("$WxForeachEnd", "<!--e-->");
string tempstr = "";
//Foreach标签处理
if (!string.IsNullOrEmpty(ForeachTemplate))
{
foreach (var obj in model.WxForeach)
{
tempstr += newHtml;
var pinfo = obj.GetType().GetProperties();
foreach (var p in pinfo)
{
string v = string.Format("{0}", p.GetValue(obj));
string r = string.Format("$WxForeach.{0}", p.Name);
tempstr = tempstr.Replace(r, v);
}
}
}
tempstr += AppendSign;
sb.Replace(AppendSign, tempstr);
}
catch (Exception ex)
{
throw ex;
}
return sb;
}
#endregion
#region
/// <summary>
/// 获取模板文件
/// </summary>
/// <param name="ForeachTemplate">模板中循环的标签代码块</param>
/// <param name="path">模板路径</param>
/// <returns></returns>
public StringBuilder GetTemplate(out string ForeachTemplate, string path = "")
{
ForeachTemplate = "";
StringBuilder htmltext = new StringBuilder();
if (string.IsNullOrEmpty(path))
path = System.AppDomain.CurrentDomain.BaseDirectory + "/WeiXin/GenHtml/Template/chatlog.html";
try
{
using (StreamReader sr = new StreamReader(path))
{
//1循环标签开始
int sign = 0;
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
if (line.IndexOf("$WxForeachStart") >= 0)
sign = 1;
if (sign == 1)
ForeachTemplate += line;
if (line.IndexOf("$WxForeachEnd") >= 0)
sign = 0;
}
sr.Close();
}
}
catch (Exception ex)
{
throw ex;
}
return htmltext;
}
/// <summary>
/// 获取已存在的html页面
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public StringBuilder GetExtHtml(string path)
{
StringBuilder htmltext = new StringBuilder();
try
{
using (StreamReader sr = new StreamReader(path))
{
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
}
}
catch (Exception ex)
{
throw ex;
}
return htmltext;
}
/// <summary>
/// 保存html文伯
/// </summary>
/// <param name="htmltext">html文件</param>
/// <param name="savePath">文件保存路径(根目录下的路径)</param>
/// <returns></returns>
public bool SaveHtmlFile(StringBuilder htmltext, string savePath, int tableMaxtCount)
{
bool r = false;
try
{
if (string.IsNullOrEmpty(savePath))
throw new Exception("文件路径不能为空!");
var htmlDocument = new HtmlAgilityPack.HtmlDocument();
string htmlstr = htmltext.ToString();
htmlDocument.LoadHtml(htmlstr);
var baseInfo = htmlDocument.GetElementbyId("TemplateTableSetMaxCount");
var trs = baseInfo.SelectNodes("tbody/tr");
if (trs != null && trs.Count > tableMaxtCount)
{
int c = trs.Count;
for (int i = 0; i < c - tableMaxtCount; i++)
{
var delinfo = trs.FirstOrDefault();
trs.Remove(delinfo);
htmltext.Replace(delinfo.OuterHtml, "");
//WX.CRM.Common.LogHelper.Info(delinfo.OuterHtml);
}
}
//路径如果不存在则创建路径
string tempPath = savePath.Substring(0, savePath.LastIndexOf('/'));
if (!Directory.Exists(tempPath))
{
DirectoryInfo directoryInfo = new DirectoryInfo(tempPath);
directoryInfo.Create();
}
//保存文件
using (StreamWriter sw = new StreamWriter(savePath, false, System.Text.Encoding.GetEncoding("UTF-8")))
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
}
r = true;
}
catch (Exception ex)
{
throw ex;
}
return r;
}
#endregion
}
}