县级监管平台

CommonHelper.cs 10KB

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