CommonHandleClass.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Text.RegularExpressions;
  7. namespace LYFZ.WXLibrary
  8. {
  9. /// <summary>
  10. /// 常用处理类
  11. /// </summary>
  12. public class CommonHandleClass
  13. {
  14. public CommonHandleClass()
  15. {
  16. }
  17. /// <summary>
  18. /// 微信接口日志目录路径
  19. /// </summary>
  20. public static string WeiXinlogsPath
  21. {
  22. get { return LYFZ.WinAPI.CustomPublicMethod.BasePath + "\\Logs\\WeiXinlogs\\"; }
  23. }
  24. private static bool isOpenLogs = false;
  25. /// <summary>
  26. /// 是否开启日志功能
  27. /// </summary>
  28. public static bool IsOpenLogs
  29. {
  30. get { return CommonHandleClass.isOpenLogs; }
  31. set { CommonHandleClass.isOpenLogs = value; }
  32. }
  33. /// <summary>
  34. /// 写日志(用于跟踪)
  35. /// </summary>
  36. public static void WriteLog(string strMsg, string ddomaine="-1", List<string> _LogsDongleDomainList = null)
  37. {
  38. try
  39. {
  40. if ((_LogsDongleDomainList != null && IsOpenLogs && _LogsDongleDomainList.Contains(ddomaine)) || ddomaine == "-1" || (ddomaine == "-2" && IsOpenLogs))
  41. {
  42. string filename = LYFZ.WinAPI.CustomPublicMethod.BasePath + "\\Logs\\WeiXinlogs.txt";
  43. if (!Directory.Exists(System.IO.Path.GetDirectoryName(filename)))
  44. Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filename));
  45. StreamWriter sr = null;
  46. try
  47. {
  48. if (!File.Exists(filename))
  49. {
  50. sr = File.CreateText(filename);
  51. }
  52. else
  53. {
  54. sr = File.AppendText(filename);
  55. }
  56. sr.WriteLine("【"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"】"+strMsg);
  57. }
  58. catch
  59. {
  60. }
  61. finally
  62. {
  63. if (sr != null)
  64. sr.Close();
  65. }
  66. }
  67. }
  68. catch { }
  69. }
  70. public static string GetWeiXinlogs()
  71. { string filename = LYFZ.WinAPI.CustomPublicMethod.BasePath + "\\Logs\\WeiXinlogs.txt";
  72. if (System.IO.File.Exists(filename))
  73. {
  74. return System.IO.File.ReadAllText(filename).Replace("\r\n", "<br />").Replace("\n", "<br />");
  75. }
  76. else {
  77. return "暂无日志";
  78. }
  79. }
  80. public static bool DeleteWeiXinlogs()
  81. {
  82. try
  83. {
  84. string filename = LYFZ.WinAPI.CustomPublicMethod.BasePath + "\\Logs\\WeiXinlogs.txt";
  85. if (System.IO.File.Exists(filename))
  86. {
  87. System.IO.File.Delete(filename);
  88. return true;
  89. }
  90. else
  91. {
  92. return false;
  93. }
  94. }
  95. catch { return false; }
  96. }
  97. //URL参数添加/修改
  98. public static string Fun_AddQueryToURL(string url, string key, string value)
  99. {
  100. int fragPos = url.LastIndexOf("#");
  101. string fragment = string.Empty;
  102. if (fragPos > -1)
  103. {
  104. fragment = url.Substring(fragPos);
  105. url = url.Substring(0, fragPos);
  106. }
  107. int querystart = url.IndexOf("?");
  108. if (querystart < 0)
  109. {
  110. url += "?" + key + "=" + value;
  111. }
  112. else
  113. {
  114. Regex reg = new Regex(@"(?<=[&\?])" + key + @"=[^\s&#]*", RegexOptions.Compiled);
  115. if (reg.IsMatch(url))
  116. url = reg.Replace(url, key + "=" + value);
  117. else
  118. url += "&" + key + "=" + value;
  119. }
  120. return url + fragment;
  121. }
  122. //URL参数删除
  123. public static string Fun_RemoveQueryFromURL(string url, string key)
  124. {
  125. Regex reg = new Regex(@"[&\?]" + key + @"=[^\s&#]*&?", RegexOptions.Compiled);
  126. return reg.Replace(url, new MatchEvaluator(PutAwayGarbageFromURL));
  127. }
  128. private static string PutAwayGarbageFromURL(Match match)
  129. {
  130. string value = match.Value;
  131. if (value.EndsWith("&"))
  132. {
  133. return value.Substring(0, 1);
  134. }
  135. else
  136. {
  137. return string.Empty;
  138. }
  139. }
  140. public static string GetNetUrl(string ServerHostOrIP, int PortNumber)
  141. {
  142. if (ServerHostOrIP.Contains(":"))
  143. {
  144. return "http://" + ServerHostOrIP;
  145. }
  146. else
  147. {
  148. return "http://" + ServerHostOrIP + ":" + PortNumber.ToString();
  149. }
  150. }
  151. static List<string> _MicroLetterEventList = new List<string>();
  152. /// <summary>
  153. /// 微信事件列表
  154. /// </summary>
  155. public static List<string> MicroLetterEventList
  156. {
  157. get { return _MicroLetterEventList; }
  158. }
  159. /// <summary>
  160. /// 关键字集合
  161. /// </summary>
  162. static List<KeywordObjects> _KeywordObjectsList = new List<KeywordObjects>();
  163. /// <summary>
  164. /// 关键字集合
  165. /// </summary>
  166. public static List<KeywordObjects> KeywordObjectsList
  167. {
  168. get { return _KeywordObjectsList; }
  169. set { _KeywordObjectsList = value; }
  170. }
  171. public static string GetFunctionCode(string keyword)
  172. {
  173. KeywordObjects key = KeywordObjectsList.Find(k=>k.Keyword.ToLower()==keyword.ToLower());
  174. if (key != null && key.FunctionCode.Trim().Length > 0)
  175. {
  176. return key.FunctionCode;
  177. }
  178. else {
  179. return "null";
  180. }
  181. }
  182. }
  183. }