Açıklama Yok

CommonHelper.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Web;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using CallCenter.Utility;
  6. namespace CallCenterApi.Common
  7. {
  8. public class CommonHelper
  9. {
  10. public static string EncryptAES(string data, string key)
  11. {
  12. return EncryptHelper.EncryptAES(data, key);
  13. }
  14. public static string DecryptAES(string data, string key)
  15. {
  16. return data + "" == "" ? "" : EncryptHelper.DecryptAES(data.Replace(" ", "+"), key);
  17. }
  18. /// <summary>
  19. /// MD5函数
  20. /// </summary>
  21. /// <param name="str">原始字符串</param>
  22. /// <returns>MD5结果</returns>
  23. public static string MD5(string str)
  24. {
  25. byte[] b = Encoding.UTF8.GetBytes(str);
  26. b = new MD5CryptoServiceProvider().ComputeHash(b);
  27. string ret = "";
  28. for (int i = 0; i < b.Length; i++)
  29. ret += b[i].ToString("x").PadLeft(2, '0');
  30. return ret;
  31. }
  32. public static string escape(string value)
  33. {
  34. if (value == null) return "";
  35. return Microsoft.JScript.GlobalObject.escape(value);
  36. }
  37. public static string unescape(string value)
  38. {
  39. if (value == null) return "";
  40. return Microsoft.JScript.GlobalObject.unescape(value);
  41. }
  42. public static string toString(object value)
  43. {
  44. if (value == null)
  45. {
  46. return "";
  47. }
  48. else
  49. {
  50. return value.ToString();
  51. }
  52. }
  53. public static int ToInt32(object value)
  54. {
  55. string str = toString(value);
  56. int result = 0;
  57. if (int.TryParse(str, out result) == false)
  58. {
  59. result = 0;
  60. }
  61. return result;
  62. }
  63. public static long ToInt64(object value)
  64. {
  65. string str = toString(value);
  66. long result = 0;
  67. if (long.TryParse(str, out result) == false)
  68. {
  69. result = 0;
  70. }
  71. return result;
  72. }
  73. /// <summary>
  74. /// 是否登录
  75. /// </summary>
  76. /// <returns></returns>
  77. public static bool IsLogin(string Uid, string PassToken)
  78. {
  79. if (string.IsNullOrEmpty(Uid) || string.IsNullOrEmpty(PassToken) || Uid == "0" || PassToken == "0")
  80. {
  81. return false;
  82. }
  83. if (PassToken.Length <= 37)
  84. {
  85. return false;
  86. }
  87. string salt = PassToken.Substring(32);
  88. if (MD5(Uid + salt + "0fc7b78cfa1cb49f5d265e261dc70675") == PassToken.Substring(0, 32))
  89. {
  90. return true;
  91. }
  92. else
  93. {
  94. return false;
  95. }
  96. }
  97. public static bool IsLogin()
  98. {
  99. var uid = HttpContext.Current.Request["uid"];
  100. var PassToken = HttpContext.Current.Request["PassToken"];
  101. return CommonHelper.IsLogin(uid, PassToken);
  102. }
  103. public static string getSourceIP()
  104. {
  105. string SourceIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; /* 获得用户 IP 地址 */
  106. if (string.IsNullOrEmpty(SourceIP))
  107. {
  108. SourceIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; /* 兼容已有程序 */
  109. }
  110. if (SourceIP != null)
  111. {
  112. if (SourceIP.IndexOf(";") != -1)
  113. {
  114. SourceIP = SourceIP.Split(';')[0];
  115. if (SourceIP.IndexOf(",") != -1)
  116. {
  117. SourceIP = SourceIP.Split(',')[0];
  118. }
  119. }
  120. SourceIP = SourceIP.Trim();
  121. }
  122. return SourceIP;
  123. }
  124. /// <summary>
  125. /// 字符串数组转十进制数组
  126. /// </summary>
  127. /// <param name="strArray">字符串数组</param>
  128. /// <returns>十进制数组</returns>
  129. public static decimal[] ToDecimalArray(string[] strArray)
  130. {
  131. decimal[] decArray = new decimal[strArray.Length];
  132. for (int i = 0; i < strArray.Length; i++)
  133. {
  134. decArray[i] = Convert.ToDecimal(strArray[i]);
  135. }
  136. return decArray;
  137. }
  138. /// <summary>
  139. /// 全局静态随机数生成器
  140. /// </summary>
  141. public static Random ranNumber = new Random();
  142. public static string DirtyFilter(string text)
  143. {
  144. if (text == null) return "";
  145. string dirtyWords = "";
  146. if (HttpContext.Current.Cache["dirtywords"] == null)
  147. {
  148. string path = HttpContext.Current.Server.MapPath("/Txt/dirtywords.txt");
  149. if (System.IO.File.Exists(path)) dirtyWords = System.IO.File.ReadAllText(path, Encoding.UTF8);
  150. HttpContext.Current.Cache.Add("dirtywords", dirtyWords, null, DateTime.Now.AddDays(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
  151. }
  152. dirtyWords = HttpContext.Current.Cache["dirtywords"].ToString();
  153. dirtyWords = dirtyWords.Trim('\r', '\n', ' ');
  154. if (dirtyWords != "")
  155. {
  156. text = System.Text.RegularExpressions.Regex.Replace(text, dirtyWords, "*", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  157. }
  158. return text;
  159. }
  160. public static string DirtyFilterBroadCast(string text)
  161. {
  162. if (text == null) return "";
  163. string dirtyWords = "";
  164. string path = HttpContext.Current.Server.MapPath("/Txt/boradcast.txt");
  165. if (System.IO.File.Exists(path)) dirtyWords = System.IO.File.ReadAllText(path, Encoding.UTF8);
  166. if (dirtyWords != "")
  167. {
  168. text = System.Text.RegularExpressions.Regex.Replace(text, dirtyWords, "**", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  169. }
  170. return text;
  171. }
  172. /// <summary>
  173. /// GB2312转化为UTF-8
  174. /// </summary>
  175. /// <param name="str"></param>
  176. /// <returns></returns>
  177. public static byte[] gb2312toutf8(string str)
  178. {
  179. Encoding utf8 = Encoding.GetEncoding("UTF-8");
  180. Encoding gb2312 = Encoding.GetEncoding("GB2312");
  181. byte[] gb = gb2312.GetBytes(str);
  182. gb = Encoding.Convert(gb2312, utf8, gb);
  183. //return utf8.GetString(gb);
  184. return gb;
  185. }
  186. /// <summary>
  187. /// UTF-8转化为GB2312
  188. /// </summary>
  189. /// <param name="text"></param>
  190. /// <returns></returns>
  191. public static byte[] utf8togb2312(string text)
  192. {
  193. Encoding utf8 = Encoding.GetEncoding("UTF-8");
  194. Encoding gb2312 = Encoding.GetEncoding("GB2312");
  195. byte[] bs = utf8.GetBytes(text);
  196. bs = Encoding.Convert(utf8, gb2312, bs);
  197. //return gb2312.GetString(bs);
  198. return bs;
  199. }
  200. }
  201. }