新乡民调(来自息县民调) - 主标

CommonHelper.cs 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System;
  2. using System.Web;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using CallCenter.Utility;
  6. namespace CallCenter.Utility.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. /// <summary>
  33. /// 获取大写的MD5签名结果
  34. /// </summary>
  35. /// <param name="encypStr"></param>
  36. /// <param name="charset"></param>
  37. /// <returns></returns>
  38. public static string GetMD5(string encypStr, string charset)
  39. {
  40. string retStr;
  41. MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();
  42. //创建md5对象
  43. byte[] inputBye;
  44. byte[] outputBye;
  45. //使用GB2312编码方式把字符串转化为字节数组.
  46. try
  47. {
  48. inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
  49. }
  50. catch (Exception ex)
  51. {
  52. inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
  53. }
  54. outputBye = m5.ComputeHash(inputBye);
  55. retStr = System.BitConverter.ToString(outputBye);
  56. retStr = retStr.Replace("-", "").ToUpper();
  57. return retStr;
  58. }
  59. public static string escape(string value)
  60. {
  61. if (value == null) return "";
  62. return Microsoft.JScript.GlobalObject.escape(value);
  63. }
  64. public static string unescape(string value)
  65. {
  66. if (value == null) return "";
  67. return Microsoft.JScript.GlobalObject.unescape(value);
  68. }
  69. public static string toString(object value)
  70. {
  71. if (value == null)
  72. {
  73. return "";
  74. }
  75. else
  76. {
  77. return value.ToString();
  78. }
  79. }
  80. public static int ToInt32(object value)
  81. {
  82. string str = toString(value);
  83. int result = 0;
  84. if (int.TryParse(str, out result) == false)
  85. {
  86. result = 0;
  87. }
  88. return result;
  89. }
  90. public static long ToInt64(object value)
  91. {
  92. string str = toString(value);
  93. long result = 0;
  94. if (long.TryParse(str, out result) == false)
  95. {
  96. result = 0;
  97. }
  98. return result;
  99. }
  100. /// <summary>
  101. /// 是否登录
  102. /// </summary>
  103. /// <returns></returns>
  104. public static bool IsLogin(string Uid, string PassToken)
  105. {
  106. if (string.IsNullOrEmpty(Uid) || string.IsNullOrEmpty(PassToken) || Uid == "0" || PassToken == "0")
  107. {
  108. return false;
  109. }
  110. if (PassToken.Length <= 37)
  111. {
  112. return false;
  113. }
  114. string salt = PassToken.Substring(32);
  115. if (MD5(Uid + salt + "0fc7b78cfa1cb49f5d265e261dc70675") == PassToken.Substring(0, 32))
  116. {
  117. return true;
  118. }
  119. else
  120. {
  121. return false;
  122. }
  123. }
  124. public static bool IsLogin()
  125. {
  126. var uid = HttpContext.Current.Request["uid"];
  127. var PassToken = HttpContext.Current.Request["PassToken"];
  128. return CommonHelper.IsLogin(uid, PassToken);
  129. }
  130. public static string getSourceIP()
  131. {
  132. string SourceIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; /* 获得用户 IP 地址 */
  133. if (string.IsNullOrEmpty(SourceIP))
  134. {
  135. SourceIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; /* 兼容已有程序 */
  136. }
  137. if (SourceIP != null)
  138. {
  139. if (SourceIP.IndexOf(";") != -1)
  140. {
  141. SourceIP = SourceIP.Split(';')[0];
  142. if (SourceIP.IndexOf(",") != -1)
  143. {
  144. SourceIP = SourceIP.Split(',')[0];
  145. }
  146. }
  147. SourceIP = SourceIP.Trim();
  148. }
  149. return SourceIP;
  150. }
  151. /// <summary>
  152. /// 字符串数组转十进制数组
  153. /// </summary>
  154. /// <param name="strArray">字符串数组</param>
  155. /// <returns>十进制数组</returns>
  156. public static decimal[] ToDecimalArray(string[] strArray)
  157. {
  158. decimal[] decArray = new decimal[strArray.Length];
  159. for (int i = 0; i < strArray.Length; i++)
  160. {
  161. decArray[i] = Convert.ToDecimal(strArray[i]);
  162. }
  163. return decArray;
  164. }
  165. /// <summary>
  166. /// 全局静态随机数生成器
  167. /// </summary>
  168. public static Random ranNumber = new Random();
  169. public static string DirtyFilter(string text)
  170. {
  171. if (text == null) return "";
  172. string dirtyWords = "";
  173. if (HttpContext.Current.Cache["dirtywords"] == null)
  174. {
  175. string path = HttpContext.Current.Server.MapPath("/Txt/dirtywords.txt");
  176. if (System.IO.File.Exists(path)) dirtyWords = System.IO.File.ReadAllText(path, Encoding.UTF8);
  177. HttpContext.Current.Cache.Add("dirtywords", dirtyWords, null, DateTime.Now.AddDays(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
  178. }
  179. dirtyWords = HttpContext.Current.Cache["dirtywords"].ToString();
  180. dirtyWords = dirtyWords.Trim('\r', '\n', ' ');
  181. if (dirtyWords != "")
  182. {
  183. text = System.Text.RegularExpressions.Regex.Replace(text, dirtyWords, "*", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  184. }
  185. return text;
  186. }
  187. public static string DirtyFilterBroadCast(string text)
  188. {
  189. if (text == null) return "";
  190. string dirtyWords = "";
  191. string path = HttpContext.Current.Server.MapPath("/Txt/boradcast.txt");
  192. if (System.IO.File.Exists(path)) dirtyWords = System.IO.File.ReadAllText(path, Encoding.UTF8);
  193. if (dirtyWords != "")
  194. {
  195. text = System.Text.RegularExpressions.Regex.Replace(text, dirtyWords, "**", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  196. }
  197. return text;
  198. }
  199. /// <summary>
  200. /// GB2312转化为UTF-8
  201. /// </summary>
  202. /// <param name="str"></param>
  203. /// <returns></returns>
  204. public static byte[] gb2312toutf8(string str)
  205. {
  206. Encoding utf8 = Encoding.GetEncoding("UTF-8");
  207. Encoding gb2312 = Encoding.GetEncoding("GB2312");
  208. byte[] gb = gb2312.GetBytes(str);
  209. gb = Encoding.Convert(gb2312, utf8, gb);
  210. //return utf8.GetString(gb);
  211. return gb;
  212. }
  213. /// <summary>
  214. /// UTF-8转化为GB2312
  215. /// </summary>
  216. /// <param name="text"></param>
  217. /// <returns></returns>
  218. public static byte[] utf8togb2312(string text)
  219. {
  220. Encoding utf8 = Encoding.GetEncoding("UTF-8");
  221. Encoding gb2312 = Encoding.GetEncoding("GB2312");
  222. byte[] bs = utf8.GetBytes(text);
  223. bs = Encoding.Convert(utf8, gb2312, bs);
  224. //return gb2312.GetString(bs);
  225. return bs;
  226. }
  227. public static string GetRemainingDay()
  228. {
  229. try
  230. {
  231. string days = "0";
  232. var date = DateTime.Parse(Configs.GetValue("AuthDate"));
  233. int remindday = Int32.Parse(Configs.GetValue("AuthRemindDay"));
  234. var subtime = date.Subtract(DateTime.Now);
  235. if (subtime.TotalDays <= remindday)
  236. {
  237. days = Math.Floor(subtime.TotalDays).ToString();
  238. }
  239. return days;
  240. }
  241. catch (Exception ex)
  242. {
  243. return "0";
  244. }
  245. }
  246. }
  247. }