No Description

CommonHelper.cs 11KB

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