新野县12345_后端

CommonHelper.cs 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using System;
  2. using System.Web;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using CallCenter.Utility;
  6. using System.Text.RegularExpressions;
  7. using System.Net;
  8. using System.IO;
  9. namespace CallCenterApi.Common
  10. {
  11. public class CommonHelper
  12. {
  13. public static string EncryptAES(string data, string key)
  14. {
  15. return EncryptHelper.EncryptAES(data, key);
  16. }
  17. public static string DecryptAES(string data, string key)
  18. {
  19. return data + "" == "" ? "" : EncryptHelper.DecryptAES(data.Replace(" ", "+"), key);
  20. }
  21. /// <summary>
  22. /// MD5函数
  23. /// </summary>
  24. /// <param name="str">原始字符串</param>
  25. /// <returns>MD5结果</returns>
  26. public static string MD5(string str)
  27. {
  28. byte[] b = Encoding.UTF8.GetBytes(str);
  29. b = new MD5CryptoServiceProvider().ComputeHash(b);
  30. string ret = "";
  31. for (int i = 0; i < b.Length; i++)
  32. ret += b[i].ToString("x").PadLeft(2, '0');
  33. return ret;
  34. }
  35. public static string escape(string value)
  36. {
  37. if (value == null) return "";
  38. return Microsoft.JScript.GlobalObject.escape(value);
  39. }
  40. public static string unescape(string value)
  41. {
  42. if (value == null) return "";
  43. return Microsoft.JScript.GlobalObject.unescape(value);
  44. }
  45. public static string toString(object value)
  46. {
  47. if (value == null)
  48. {
  49. return "";
  50. }
  51. else
  52. {
  53. return value.ToString();
  54. }
  55. }
  56. public static int ToInt32(object value)
  57. {
  58. string str = toString(value);
  59. int result = 0;
  60. if (int.TryParse(str, out result) == false)
  61. {
  62. result = 0;
  63. }
  64. return result;
  65. }
  66. public static long ToInt64(object value)
  67. {
  68. string str = toString(value);
  69. long result = 0;
  70. if (long.TryParse(str, out result) == false)
  71. {
  72. result = 0;
  73. }
  74. return result;
  75. }
  76. /// <summary>
  77. /// 是否登录
  78. /// </summary>
  79. /// <returns></returns>
  80. public static bool IsLogin(string Uid, string PassToken)
  81. {
  82. if (string.IsNullOrEmpty(Uid) || string.IsNullOrEmpty(PassToken) || Uid == "0" || PassToken == "0")
  83. {
  84. return false;
  85. }
  86. if (PassToken.Length <= 37)
  87. {
  88. return false;
  89. }
  90. string salt = PassToken.Substring(32);
  91. if (MD5(Uid + salt + "0fc7b78cfa1cb49f5d265e261dc70675") == PassToken.Substring(0, 32))
  92. {
  93. return true;
  94. }
  95. else
  96. {
  97. return false;
  98. }
  99. }
  100. public static bool IsLogin()
  101. {
  102. var uid = HttpContext.Current.Request["uid"];
  103. var PassToken = HttpContext.Current.Request["PassToken"];
  104. return CommonHelper.IsLogin(uid, PassToken);
  105. }
  106. public static string getSourceIP()
  107. {
  108. string SourceIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; /* 获得用户 IP 地址 */
  109. if (string.IsNullOrEmpty(SourceIP))
  110. {
  111. SourceIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; /* 兼容已有程序 */
  112. }
  113. if (SourceIP != null)
  114. {
  115. if (SourceIP.IndexOf(";") != -1)
  116. {
  117. SourceIP = SourceIP.Split(';')[0];
  118. if (SourceIP.IndexOf(",") != -1)
  119. {
  120. SourceIP = SourceIP.Split(',')[0];
  121. }
  122. }
  123. SourceIP = SourceIP.Trim();
  124. }
  125. return SourceIP;
  126. }
  127. /// <summary>
  128. /// 字符串数组转十进制数组
  129. /// </summary>
  130. /// <param name="strArray">字符串数组</param>
  131. /// <returns>十进制数组</returns>
  132. public static decimal[] ToDecimalArray(string[] strArray)
  133. {
  134. decimal[] decArray = new decimal[strArray.Length];
  135. for (int i = 0; i < strArray.Length; i++)
  136. {
  137. decArray[i] = Convert.ToDecimal(strArray[i]);
  138. }
  139. return decArray;
  140. }
  141. /// <summary>
  142. /// 全局静态随机数生成器
  143. /// </summary>
  144. public static Random ranNumber = new Random();
  145. public static string DirtyFilter(string text)
  146. {
  147. if (text == null) return "";
  148. string dirtyWords = "";
  149. if (HttpContext.Current.Cache["dirtywords"] == null)
  150. {
  151. string path = HttpContext.Current.Server.MapPath("/Txt/dirtywords.txt");
  152. if (System.IO.File.Exists(path)) dirtyWords = System.IO.File.ReadAllText(path, Encoding.UTF8);
  153. HttpContext.Current.Cache.Add("dirtywords", dirtyWords, null, DateTime.Now.AddDays(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
  154. }
  155. dirtyWords = HttpContext.Current.Cache["dirtywords"].ToString();
  156. dirtyWords = dirtyWords.Trim('\r', '\n', ' ');
  157. if (dirtyWords != "")
  158. {
  159. text = System.Text.RegularExpressions.Regex.Replace(text, dirtyWords, "*", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  160. }
  161. return text;
  162. }
  163. public static string DirtyFilterBroadCast(string text)
  164. {
  165. if (text == null) return "";
  166. string dirtyWords = "";
  167. string path = HttpContext.Current.Server.MapPath("/Txt/boradcast.txt");
  168. if (System.IO.File.Exists(path)) dirtyWords = System.IO.File.ReadAllText(path, Encoding.UTF8);
  169. if (dirtyWords != "")
  170. {
  171. text = System.Text.RegularExpressions.Regex.Replace(text, dirtyWords, "**", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  172. }
  173. return text;
  174. }
  175. /// <summary>
  176. /// GB2312转化为UTF-8
  177. /// </summary>
  178. /// <param name="str"></param>
  179. /// <returns></returns>
  180. public static byte[] gb2312toutf8(string str)
  181. {
  182. Encoding utf8 = Encoding.GetEncoding("UTF-8");
  183. Encoding gb2312 = Encoding.GetEncoding("GB2312");
  184. byte[] gb = gb2312.GetBytes(str);
  185. gb = Encoding.Convert(gb2312, utf8, gb);
  186. //return utf8.GetString(gb);
  187. return gb;
  188. }
  189. /// <summary>
  190. /// UTF-8转化为GB2312
  191. /// </summary>
  192. /// <param name="text"></param>
  193. /// <returns></returns>
  194. public static byte[] utf8togb2312(string text)
  195. {
  196. Encoding utf8 = Encoding.GetEncoding("UTF-8");
  197. Encoding gb2312 = Encoding.GetEncoding("GB2312");
  198. byte[] bs = utf8.GetBytes(text);
  199. bs = Encoding.Convert(utf8, gb2312, bs);
  200. //return gb2312.GetString(bs);
  201. return bs;
  202. }
  203. private static string IP_Url = "http://iframe.ip138.com/ic.asp";
  204. private static string IP_Url2 = "http://www.ip.cn";
  205. /// <summary>
  206. /// 获取本机外网ip地址
  207. /// </summary>
  208. /// <returns></returns>
  209. public static string GetOuterNetIP()
  210. {
  211. string ip = "0.0.0.0";
  212. string html = GetSourceTextByUrl(IP_Url);
  213. Regex regex = new Regex(@"((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)");
  214. if (regex.Match(html).Success)
  215. {
  216. ip = regex.Match(html).Value;
  217. }
  218. else
  219. {
  220. html = GetSourceTextByUrl(IP_Url2);
  221. if (regex.Match(html).Success)
  222. {
  223. ip = regex.Match(html).Value;
  224. }
  225. }
  226. return ip;
  227. }
  228. public static string GetSourceTextByUrl(string url)
  229. {
  230. try
  231. {
  232. Uri uri = new Uri(url);
  233. WebRequest webreq = WebRequest.Create(uri);
  234. Stream s = webreq.GetResponse().GetResponseStream();
  235. StreamReader sr = new StreamReader(s, System.Text.Encoding.Default);
  236. string all = sr.ReadToEnd(); //读取网站返回的数据 格式:您的IP地址是:[x.x.x.x]
  237. int i = all.IndexOf("[") + 1;
  238. string tempip = all.Substring(i, 15);
  239. string ip = tempip.Replace("]", "").Replace(" ", "").Replace("<", ""); //去除杂项找出ip
  240. return ip;
  241. //WebRequest request = WebRequest.Create(url);
  242. //request.Timeout = 200000;//20秒超时
  243. //WebResponse response = request.GetResponse();
  244. //Stream resStream = response.GetResponseStream();
  245. //StreamReader sr = new StreamReader(resStream);
  246. //return sr.ReadToEnd();
  247. }
  248. catch (Exception ex)
  249. {
  250. return ex.Message;
  251. }
  252. }
  253. }
  254. }