PublicFunction.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Text.RegularExpressions;
  6. using Library;
  7. using System.IO;
  8. using System.Web;
  9. using System.Security.Claims;
  10. using System.Text;
  11. namespace MySystem
  12. {
  13. public class PublicFunction
  14. {
  15. #region 中译英
  16. public string TranslateZHToEn(string Source)
  17. {
  18. string result = function.GetWebRequest("http://fanyi.youdao.com/translate?&doctype=json&type=ZH_CN2EN&i=" + Source);
  19. //{"type":"ZH_CN2EN","errorCode":0,"elapsedTime":2,"translateResult":[[{"src":"大事件","tgt":"The big event"}]]}
  20. Match match = Regex.Match(result, "\"tgt\":\".*?\"");
  21. if (match.Success)
  22. {
  23. return match.Value.Replace("\"tgt\":", "").Trim('"');
  24. }
  25. return "";
  26. }
  27. #endregion
  28. #region 解析编辑器里的视频代码
  29. public string CheckMediaFromHtml(string content)
  30. {
  31. if (string.IsNullOrEmpty(content))
  32. {
  33. return "";
  34. }
  35. string result = content;
  36. MatchCollection mc = Regex.Matches(content, "<embed.*?/>");
  37. foreach (Match submc in mc)
  38. {
  39. string info = submc.Value;
  40. Match match = Regex.Match(info, "src=\".*?\"");
  41. if (match.Success)
  42. {
  43. string path = match.Value;
  44. path = path.Replace("src=\"", "");
  45. path = path.TrimEnd(new char[] { '"' });
  46. string html = "";
  47. string width = "600";
  48. string height = "300";
  49. Match width_match = Regex.Match(info, "width=\".*?\"");
  50. if (width_match.Success)
  51. {
  52. width = width_match.Value.Replace("width=", "").Trim('"');
  53. }
  54. Match height_match = Regex.Match(info, "height=\".*?\"");
  55. if (height_match.Success)
  56. {
  57. height = height_match.Value.Replace("height=", "").Trim('"');
  58. }
  59. if (path.EndsWith(".mp4"))
  60. {
  61. if (string.IsNullOrEmpty(html))
  62. {
  63. html = "<video controls=\"controls\" autoplay=\"autoplay\" poster=\"\" onplay=\"true\" width=\"" + width + "\" height=\"" + height + "\" onclick=\"this.play();\">" +
  64. "<source src=\"" + path + "\">" +
  65. "<source src=\"" + path + "\" type=\"video/mp4\">" +
  66. "<source src=\"" + path + "\" type=\"video/webm\">" +
  67. "<source src=\"" + path + "\" type=\"video/ogg\">" +
  68. "</video>";
  69. }
  70. }
  71. else if (path.EndsWith(".mp3"))
  72. {
  73. html = "<audio controls=\"controls\" width=\"" + width + "\" height=\"" + height + "\" onclick=\"this.play();\">" +
  74. "<source src=\"" + path + "\" type=\"audio/mp3\" />" +
  75. "<embed src=\"" + path + "\" height=\"100\" width=\"100\" />" +
  76. "</audio>";
  77. }
  78. result = result.Replace(info, html);
  79. }
  80. }
  81. return result;
  82. }
  83. #endregion
  84. #region 对象转Json字符串
  85. public static string ObjectToJsonString(object obj)
  86. {
  87. return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  88. }
  89. #endregion
  90. #region Json字符串转对象
  91. public static T DeserializeJSON<T>(string json)
  92. {
  93. return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
  94. }
  95. #endregion
  96. #region 删除文件
  97. public static bool DeleteFile(string VirtualPath)
  98. {
  99. string FilePath = function.getPath(VirtualPath);
  100. if (System.IO.File.Exists(FilePath))
  101. {
  102. System.IO.File.Delete(FilePath);
  103. return true;
  104. }
  105. return false;
  106. }
  107. #endregion
  108. #region 两点距离
  109. public static double GetDistanceNumber(string start, string end)
  110. {
  111. if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
  112. {
  113. string[] startpos = start.Split(',');
  114. string[] endpos = end.Split(',');
  115. double lng1 = double.Parse(startpos[0]);
  116. double lat1 = double.Parse(startpos[1]);
  117. double lng2 = double.Parse(endpos[0]);
  118. double lat2 = double.Parse(endpos[1]);
  119. double radLat1 = rad(lat1);
  120. double radLat2 = rad(lat2);
  121. double a = radLat1 - radLat2;
  122. double b = rad(lng1) - rad(lng2);
  123. double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
  124. s = s * EARTH_RADIUS;
  125. s = Math.Round(s * 10000) / 10000;
  126. return s;
  127. }
  128. return 10000000;
  129. }
  130. private static double rad(double d)
  131. {
  132. return d * Math.PI / 180.0;
  133. }
  134. private static double EARTH_RADIUS = 6378.137;
  135. #endregion
  136. #region 获取OSS开关
  137. public string GetOssStatus()
  138. {
  139. return "-oss";
  140. }
  141. #endregion
  142. #region 通过表名、文本、值获得字典数据
  143. public Dictionary<string, string> GetDictionaryByTableName(string tableEnName, string Text, string Value)
  144. {
  145. Text = Text.Split('_')[0];
  146. Value = Value.Split('_')[0];
  147. Dictionary<string, string> dic = new Dictionary<string, string>();
  148. DataTable dt = dbconn.dtable("select " + Text + "," + Value + " from " + tableEnName + " order by Sort desc,Id asc");
  149. foreach (DataRow dr in dt.Rows)
  150. {
  151. dic.Add(dr[1].ToString(), dr[0].ToString());
  152. }
  153. return dic;
  154. }
  155. #endregion
  156. #region 接口通用DES解密
  157. public static string DesDecrypt(string content)
  158. {
  159. content = HttpUtility.UrlDecode(content);
  160. return dbconn.DesDecrypt(content, "*ga34|^7");
  161. }
  162. #endregion
  163. }
  164. }