Нет описания

CommonHelper.cs 13KB

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