using System.Collections; using System.Text.RegularExpressions; using Common; using Feign; using LitJson; using Microsoft.CodeAnalysis.CSharp.Scripting; using Microsoft.CodeAnalysis.Scripting; namespace Util { public class Maker { #region 读取模版数据,替换对应的标签内容 public static string StartMake(JsonData jsonObj, string templateString) { foreach (string key in jsonObj.Keys) { JsonData obj = jsonObj[key]; if(obj != null) { if (obj.IsArray) //如果是数组 { //匹配循环带loop循环标记的内容 templateString = makeArrayAsync(obj, templateString, key); } else if (obj.IsObject) //如果是单个对象 { //匹配循环带item单个对象标记的内容 templateString = makeObjectAsync(obj, templateString, key); } else //否则当数值处理 { templateString = templateString.Replace("<>", jsonObj[key].ToString()); } } else { templateString = templateString.Replace("<>", ""); } } templateString = PublicMake(templateString, "-top2"); templateString = PublicMake(templateString, "-top"); templateString = PublicMake(templateString); return templateString; } public static string makeArrayAsync(JsonData jsonObj, string templateString, string key) { MatchCollection mc = Regex.Matches(templateString, "<>[\\s\\S]*?<>"); foreach (Match m in mc) { string listString = ""; string matchValue = m.Value; Match head = Regex.Match(matchValue, "<>"); if (head.Success) { string headValue = head.Value; char[] removeString = new char[] { }; //两头要过滤的符号 char[] removeStartString = new char[] { }; //头部要过滤的符号 char[] removeEndString = new char[] { }; //尾部要过滤的符号 //匹配两头要过滤的符号 Match remove = Regex.Match(headValue, "remove=\".*?\""); if (remove.Success) { removeString = remove.Value.Replace("remove=\"", "").Replace("\"", "").ToCharArray(); } //匹配头部要过滤的符号 Match removeStart = Regex.Match(headValue, "removeStart=\".*?\""); if (removeStart.Success) { removeStartString = removeStart.Value.Replace("removeStart=\"", "").Replace("\"", "").ToCharArray(); } //匹配尾头要过滤的符号 Match removeEnd = Regex.Match(headValue, "removeEnd=\".*?\""); if (removeEnd.Success) { removeEndString = removeEnd.Value.Replace("removeEnd=\"", "").Replace("\"", "").ToCharArray(); } //匹配要循环的模板内容 string itemTemplate = matchValue.Replace(headValue, "").Replace("<>", ""); //循环读取数组数据 for (int i = 0; i < jsonObj.Count; i++) { JsonData item = jsonObj[i]; string itemString = itemTemplate; // IDictionary itemData = item as IDictionary; //循环读取每个字段的数据 foreach (string itemKey in item.Keys) { JsonData itemObj = item[itemKey]; // Console.WriteLine(itemKey + " => " + itemObj); if(itemObj != null) { if (itemObj.IsArray) { itemString = makeArrayAsync(itemObj, itemString, itemKey); } else if (itemObj.IsObject) { itemString = makeObjectAsync(itemObj, itemString, itemKey); } else { itemString = itemString.Replace("<>", itemObj.ToString()); Dictionary replaceDict = new Dictionary(); int index = 0; MatchCollection subLoops = Regex.Matches(itemString, "<>[\\s\\S]*?<>"); foreach (Match subLoop in subLoops) { index += 1; itemString = itemString.Replace(subLoop.Value, "${tag" + index + "}$"); replaceDict.Add("tag" + index, subLoop.Value); } subLoops = Regex.Matches(itemString, "<>[\\s\\S]*?<>"); foreach (Match subLoop in subLoops) { index += 1; itemString = itemString.Replace(subLoop.Value, "${tag" + index + "}$"); replaceDict.Add("tag" + index, subLoop.Value); } itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString()); foreach (var replaceKey in replaceDict.Keys) { itemString = itemString.Replace("${" + replaceKey + "}$", replaceDict[replaceKey]); } } } else { itemString = itemString.Replace("<<" + itemKey + ">>", ""); itemString = Regex.Replace(itemString, "<>[\\s\\S]*?<>", ""); itemString = Regex.Replace(itemString, "<>[\\s\\S]*?<>", ""); } } listString += itemString; } listString = PublicMake(listString); //过滤两头的符号 if (removeString.Length > 0) { listString = listString.Trim(removeString); } //过滤头部的符号 if (removeStartString.Length > 0) { listString = listString.TrimStart(removeStartString); } //过滤尾部的符号 if (removeEndString.Length > 0) { listString = listString.TrimEnd(removeEndString); } } templateString = templateString.Replace(matchValue, listString); } return templateString; } public static string makeObjectAsync(JsonData jsonObj, string templateString, string key) { MatchCollection mc = Regex.Matches(templateString, "<>[\\s\\S]*?<>"); foreach (Match m in mc) { string listString = ""; string matchValue = m.Value; Match head = Regex.Match(matchValue, "<>"); if (head.Success) { string headValue = head.Value; string itemTemplate = matchValue.Replace(headValue, "").Replace("<>", ""); string itemString = itemTemplate; // IDictionary itemData = obj as IDictionary; //循环读取每个字段的数据 foreach (string itemKey in jsonObj.Keys) { JsonData itemObj = jsonObj[itemKey]; if(itemObj != null) { if (itemObj.IsArray) { itemString = makeArrayAsync(itemObj, itemString, itemKey); } else if (itemObj.IsObject) { itemString = makeObjectAsync(itemObj, itemString, itemKey); } else { itemString = itemString.Replace("<>", itemObj.ToString()); Dictionary replaceDict = new Dictionary(); int index = 0; MatchCollection subLoops = Regex.Matches(itemString, "<>[\\s\\S]*?<>"); foreach (Match subLoop in subLoops) { index += 1; itemString = itemString.Replace(subLoop.Value, "${tag" + index + "}$"); replaceDict.Add("tag" + index, subLoop.Value); } subLoops = Regex.Matches(itemString, "<>[\\s\\S]*?<>"); foreach (Match subLoop in subLoops) { index += 1; itemString = itemString.Replace(subLoop.Value, "${tag" + index + "}$"); replaceDict.Add("tag" + index, subLoop.Value); } itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString()); foreach (var replaceKey in replaceDict.Keys) { itemString = itemString.Replace("${" + replaceKey + "}$", replaceDict[replaceKey]); } } } else { itemString = itemString.Replace("<<" + itemKey + ">>", ""); itemString = Regex.Replace(itemString, "<>[\\s\\S]*?<>", ""); itemString = Regex.Replace(itemString, "<>[\\s\\S]*?<>", ""); } } listString += itemString; } templateString = templateString.Replace(matchValue, listString); } mc = Regex.Matches(templateString, "<>"); foreach (Match m in mc) { string matchValue = m.Value; string[] data = matchValue.Replace("<>", "").Split('.'); if(data.Length == 1) { if(jsonObj.ToJson().Contains("\"" + key + "\"")) { templateString = templateString.Replace(matchValue, jsonObj[key][data[0]].ToString()); } else { templateString = templateString.Replace(matchValue, jsonObj[data[0]].ToString()); } } else if(data.Length == 2) { if(jsonObj.ToJson().Contains("\"" + key + "\"")) { templateString = templateString.Replace(matchValue, jsonObj[key][data[0]][data[1]].ToString()); } else { templateString = templateString.Replace(matchValue, jsonObj[data[0]][data[1]].ToString()); } } } return templateString; } #endregion #region 公共标签处理 public static string PublicMake(string content, string level = "") { MatchCollection mc = Regex.Matches(content, "<>[\\s\\S]*?<>"); foreach (Match m in mc) { string matchValue = m.Value; Match head = Regex.Match(matchValue.Replace("<>", ""), "<>"); if (head.Success) { string headValue = head.Value; string resultValue = matchValue.Replace(headValue, "").Replace("<>", ""); string elseValue = ""; if (resultValue.Contains("<>")) { string[] valueData = resultValue.Split(new string[] { "<>" }, StringSplitOptions.None); resultValue = valueData[0]; elseValue = valueData[1]; } string condition = headValue.Substring(8 + level.Length, headValue.Length - 10 - level.Length); condition = condition.Replace("isEmpty", "IsNullOrEmpty"); condition = condition.Replace("start(", "StartsWith("); condition = condition.Replace("True", "true"); condition = condition.Replace("False", "false"); condition = condition.Replace("\"", "'"); Utils.WriteLog(condition, "生成接收"); bool result = Utils.EvaluateBoolean(condition); content = content.Replace(matchValue, result ? resultValue : elseValue); } } return content; } #endregion #region 读取模版数据,替换对应的标签内容 public static string replaceKeyToValue(JsonData jsonObj, string templateString) { MatchCollection mc = Regex.Matches(templateString, "<<.*?>>"); foreach (Match m in mc) { string matchValue = m.Value; string key = matchValue.Replace("<<", "").Replace(">>", ""); string[] keyArr = key.Replace("ym.", "").Split('.'); string jsonObjString = jsonObj.ToJson(); if (keyArr.Length == 1) { if (jsonObjString.Contains("\"" + key + "\"") && jsonObj.Keys.Contains(key)) { templateString = templateString.Replace(matchValue, jsonObj[key].ToString()); } } else if (keyArr.Length == 2) { if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\"")) { templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]].ToString()); } } else if (keyArr.Length == 3) { if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\"") && jsonObjString.Contains("\"" + keyArr[2] + "\"")) { templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]][keyArr[2]].ToString()); } } } return templateString; } #endregion } }