Maker.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using System.Collections;
  2. using System.Text.RegularExpressions;
  3. using Common;
  4. using Feign;
  5. using LitJson;
  6. using Microsoft.CodeAnalysis.CSharp.Scripting;
  7. using Microsoft.CodeAnalysis.Scripting;
  8. namespace Util
  9. {
  10. public class Maker
  11. {
  12. #region 读取模版数据,替换对应的标签内容
  13. public static string StartMake(JsonData jsonObj, string templateString)
  14. {
  15. foreach (string key in jsonObj.Keys)
  16. {
  17. JsonData obj = jsonObj[key];
  18. if(obj != null)
  19. {
  20. if (obj.IsArray) //如果是数组
  21. {
  22. //匹配循环带loop循环标记的内容
  23. templateString = makeArrayAsync(obj, templateString, key);
  24. }
  25. else if (obj.IsObject) //如果是单个对象
  26. {
  27. //匹配循环带item单个对象标记的内容
  28. templateString = makeObjectAsync(obj, templateString, key);
  29. }
  30. else //否则当数值处理
  31. {
  32. templateString = templateString.Replace("<<ym:" + key + ">>", jsonObj[key].ToString());
  33. }
  34. }
  35. else
  36. {
  37. templateString = templateString.Replace("<<ym:" + key + ">>", "");
  38. }
  39. }
  40. templateString = PublicMake(templateString, "-top2");
  41. templateString = PublicMake(templateString, "-top");
  42. templateString = PublicMake(templateString);
  43. return templateString;
  44. }
  45. public static string makeArrayAsync(JsonData jsonObj, string templateString, string key)
  46. {
  47. MatchCollection mc = Regex.Matches(templateString, "<<ym-loop:" + key + ".*?>>[\\s\\S]*?<</ym-loop:" + key + ">>");
  48. foreach (Match m in mc)
  49. {
  50. string listString = "";
  51. string matchValue = m.Value;
  52. Match head = Regex.Match(matchValue, "<<ym-loop:" + key + ".*?>>");
  53. if (head.Success)
  54. {
  55. string headValue = head.Value;
  56. char[] removeString = new char[] { }; //两头要过滤的符号
  57. char[] removeStartString = new char[] { }; //头部要过滤的符号
  58. char[] removeEndString = new char[] { }; //尾部要过滤的符号
  59. //匹配两头要过滤的符号
  60. Match remove = Regex.Match(headValue, "remove=\".*?\"");
  61. if (remove.Success)
  62. {
  63. removeString = remove.Value.Replace("remove=\"", "").Replace("\"", "").ToCharArray();
  64. }
  65. //匹配头部要过滤的符号
  66. Match removeStart = Regex.Match(headValue, "removeStart=\".*?\"");
  67. if (removeStart.Success)
  68. {
  69. removeStartString = removeStart.Value.Replace("removeStart=\"", "").Replace("\"", "").ToCharArray();
  70. }
  71. //匹配尾头要过滤的符号
  72. Match removeEnd = Regex.Match(headValue, "removeEnd=\".*?\"");
  73. if (removeEnd.Success)
  74. {
  75. removeEndString = removeEnd.Value.Replace("removeEnd=\"", "").Replace("\"", "").ToCharArray();
  76. }
  77. //匹配要循环的模板内容
  78. string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-loop:" + key + ">>", "");
  79. //循环读取数组数据
  80. for (int i = 0; i < jsonObj.Count; i++)
  81. {
  82. JsonData item = jsonObj[i];
  83. string itemString = itemTemplate;
  84. // IDictionary<string, JsonData> itemData = item as IDictionary<string, JsonData>;
  85. //循环读取每个字段的数据
  86. foreach (string itemKey in item.Keys)
  87. {
  88. JsonData itemObj = item[itemKey];
  89. // Console.WriteLine(itemKey + " => " + itemObj);
  90. if(itemObj != null)
  91. {
  92. if (itemObj.IsArray)
  93. {
  94. itemString = makeArrayAsync(itemObj, itemString, itemKey);
  95. }
  96. else if (itemObj.IsObject)
  97. {
  98. itemString = makeObjectAsync(itemObj, itemString, itemKey);
  99. }
  100. else
  101. {
  102. itemString = itemString.Replace("<<p." + itemKey + ">>", itemObj.ToString());
  103. Dictionary<string, string> replaceDict = new Dictionary<string, string>();
  104. int index = 0;
  105. MatchCollection subLoops = Regex.Matches(itemString, "<<ym-loop:.*?>>[\\s\\S]*?<</ym-loop:.*?>>");
  106. foreach (Match subLoop in subLoops)
  107. {
  108. index += 1;
  109. itemString = itemString.Replace(subLoop.Value, "${tag" + index + "}$");
  110. replaceDict.Add("tag" + index, subLoop.Value);
  111. }
  112. subLoops = Regex.Matches(itemString, "<<ym-item:.*?>>[\\s\\S]*?<</ym-item:.*?>>");
  113. foreach (Match subLoop in subLoops)
  114. {
  115. index += 1;
  116. itemString = itemString.Replace(subLoop.Value, "${tag" + index + "}$");
  117. replaceDict.Add("tag" + index, subLoop.Value);
  118. }
  119. itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
  120. foreach (var replaceKey in replaceDict.Keys)
  121. {
  122. itemString = itemString.Replace("${" + replaceKey + "}$", replaceDict[replaceKey]);
  123. }
  124. }
  125. }
  126. else
  127. {
  128. itemString = itemString.Replace("<<" + itemKey + ">>", "");
  129. itemString = Regex.Replace(itemString, "<<ym-loop:" + itemKey + ".*?>>[\\s\\S]*?<</ym-loop:" + itemKey + ">>", "");
  130. itemString = Regex.Replace(itemString, "<<ym-item:" + itemKey + ".*?>>[\\s\\S]*?<</ym-item:" + itemKey + ">>", "");
  131. }
  132. }
  133. listString += itemString;
  134. }
  135. listString = PublicMake(listString);
  136. //过滤两头的符号
  137. if (removeString.Length > 0)
  138. {
  139. listString = listString.Trim(removeString);
  140. }
  141. //过滤头部的符号
  142. if (removeStartString.Length > 0)
  143. {
  144. listString = listString.TrimStart(removeStartString);
  145. }
  146. //过滤尾部的符号
  147. if (removeEndString.Length > 0)
  148. {
  149. listString = listString.TrimEnd(removeEndString);
  150. }
  151. }
  152. templateString = templateString.Replace(matchValue, listString);
  153. }
  154. return templateString;
  155. }
  156. public static string makeObjectAsync(JsonData jsonObj, string templateString, string key)
  157. {
  158. MatchCollection mc = Regex.Matches(templateString, "<<ym-item:" + key + ".*?>>[\\s\\S]*?<</ym-item:" + key + ">>");
  159. foreach (Match m in mc)
  160. {
  161. string listString = "";
  162. string matchValue = m.Value;
  163. Match head = Regex.Match(matchValue, "<<ym-item:" + key + ".*?>>");
  164. if (head.Success)
  165. {
  166. string headValue = head.Value;
  167. string itemTemplate = matchValue.Replace(headValue, "").Replace("<</ym-item:" + key + ">>", "");
  168. string itemString = itemTemplate;
  169. // IDictionary<string, JsonData> itemData = obj as IDictionary<string, JsonData>;
  170. //循环读取每个字段的数据
  171. foreach (string itemKey in jsonObj.Keys)
  172. {
  173. JsonData itemObj = jsonObj[itemKey];
  174. if(itemObj != null)
  175. {
  176. if (itemObj.IsArray)
  177. {
  178. itemString = makeArrayAsync(itemObj, itemString, itemKey);
  179. }
  180. else if (itemObj.IsObject)
  181. {
  182. itemString = makeObjectAsync(itemObj, itemString, itemKey);
  183. }
  184. else
  185. {
  186. itemString = itemString.Replace("<<p." + itemKey + ">>", itemObj.ToString());
  187. Dictionary<string, string> replaceDict = new Dictionary<string, string>();
  188. int index = 0;
  189. MatchCollection subLoops = Regex.Matches(itemString, "<<ym-loop:.*?>>[\\s\\S]*?<</ym-loop:.*?>>");
  190. foreach (Match subLoop in subLoops)
  191. {
  192. index += 1;
  193. itemString = itemString.Replace(subLoop.Value, "${tag" + index + "}$");
  194. replaceDict.Add("tag" + index, subLoop.Value);
  195. }
  196. subLoops = Regex.Matches(itemString, "<<ym-item:.*?>>[\\s\\S]*?<</ym-item:.*?>>");
  197. foreach (Match subLoop in subLoops)
  198. {
  199. index += 1;
  200. itemString = itemString.Replace(subLoop.Value, "${tag" + index + "}$");
  201. replaceDict.Add("tag" + index, subLoop.Value);
  202. }
  203. itemString = itemString.Replace("<<" + itemKey + ">>", itemObj.ToString());
  204. foreach (var replaceKey in replaceDict.Keys)
  205. {
  206. itemString = itemString.Replace("${" + replaceKey + "}$", replaceDict[replaceKey]);
  207. }
  208. }
  209. }
  210. else
  211. {
  212. itemString = itemString.Replace("<<" + itemKey + ">>", "");
  213. itemString = Regex.Replace(itemString, "<<ym-loop:" + itemKey + ".*?>>[\\s\\S]*?<</ym-loop:" + itemKey + ">>", "");
  214. itemString = Regex.Replace(itemString, "<<ym-item:" + itemKey + ".*?>>[\\s\\S]*?<</ym-item:" + itemKey + ">>", "");
  215. }
  216. }
  217. listString += itemString;
  218. }
  219. templateString = templateString.Replace(matchValue, listString);
  220. }
  221. mc = Regex.Matches(templateString, "<<ym:" + key + ".*?>>");
  222. foreach (Match m in mc)
  223. {
  224. string matchValue = m.Value;
  225. string[] data = matchValue.Replace("<<ym:" + key + ".", "").Replace(">>", "").Split('.');
  226. if(data.Length == 1)
  227. {
  228. if(jsonObj.ToJson().Contains("\"" + key + "\""))
  229. {
  230. templateString = templateString.Replace(matchValue, jsonObj[key][data[0]].ToString());
  231. }
  232. else
  233. {
  234. templateString = templateString.Replace(matchValue, jsonObj[data[0]].ToString());
  235. }
  236. }
  237. else if(data.Length == 2)
  238. {
  239. if(jsonObj.ToJson().Contains("\"" + key + "\""))
  240. {
  241. templateString = templateString.Replace(matchValue, jsonObj[key][data[0]][data[1]].ToString());
  242. }
  243. else
  244. {
  245. templateString = templateString.Replace(matchValue, jsonObj[data[0]][data[1]].ToString());
  246. }
  247. }
  248. }
  249. return templateString;
  250. }
  251. #endregion
  252. #region 公共标签处理
  253. public static string PublicMake(string content, string level = "")
  254. {
  255. MatchCollection mc = Regex.Matches(content, "<<ym-if" + level + ":.*?>>[\\s\\S]*?<</ym-if" + level + ">>");
  256. foreach (Match m in mc)
  257. {
  258. string matchValue = m.Value;
  259. Match head = Regex.Match(matchValue.Replace("<</ym-if" + level + ">>", ""), "<<ym-if" + level + ":.*?>>");
  260. if (head.Success)
  261. {
  262. string headValue = head.Value;
  263. string resultValue = matchValue.Replace(headValue, "").Replace("<</ym-if" + level + ">>", "");
  264. string elseValue = "";
  265. if (resultValue.Contains("<<ym-else" + level + ">>"))
  266. {
  267. string[] valueData = resultValue.Split(new string[] { "<<ym-else" + level + ">>" }, StringSplitOptions.None);
  268. resultValue = valueData[0];
  269. elseValue = valueData[1];
  270. }
  271. string condition = headValue.Substring(8 + level.Length, headValue.Length - 10 - level.Length);
  272. condition = condition.Replace("isEmpty", "IsNullOrEmpty");
  273. condition = condition.Replace("start(", "StartsWith(");
  274. condition = condition.Replace("True", "true");
  275. condition = condition.Replace("False", "false");
  276. condition = condition.Replace("\"", "'");
  277. Utils.WriteLog(condition, "生成接收");
  278. bool result = Utils.EvaluateBoolean(condition);
  279. content = content.Replace(matchValue, result ? resultValue : elseValue);
  280. }
  281. }
  282. return content;
  283. }
  284. #endregion
  285. #region 读取模版数据,替换对应的标签内容
  286. public static string replaceKeyToValue(JsonData jsonObj, string templateString)
  287. {
  288. MatchCollection mc = Regex.Matches(templateString, "<<.*?>>");
  289. foreach (Match m in mc)
  290. {
  291. string matchValue = m.Value;
  292. string key = matchValue.Replace("<<", "").Replace(">>", "");
  293. string[] keyArr = key.Replace("ym.", "").Split('.');
  294. string jsonObjString = jsonObj.ToJson();
  295. if (keyArr.Length == 1)
  296. {
  297. if (jsonObjString.Contains("\"" + key + "\"") && jsonObj.Keys.Contains(key))
  298. {
  299. templateString = templateString.Replace(matchValue, jsonObj[key].ToString());
  300. }
  301. }
  302. else if (keyArr.Length == 2)
  303. {
  304. if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\""))
  305. {
  306. templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]].ToString());
  307. }
  308. }
  309. else if (keyArr.Length == 3)
  310. {
  311. if (jsonObjString.Contains("\"" + keyArr[0] + "\"") && jsonObjString.Contains("\"" + keyArr[1] + "\"") && jsonObjString.Contains("\"" + keyArr[2] + "\""))
  312. {
  313. templateString = templateString.Replace(matchValue, jsonObj[keyArr[0]][keyArr[1]][keyArr[2]].ToString());
  314. }
  315. }
  316. }
  317. return templateString;
  318. }
  319. #endregion
  320. }
  321. }