PublicFunction.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 Microsoft.IdentityModel.Tokens;
  10. using System.Security.Claims;
  11. using System.IdentityModel.Tokens.Jwt;
  12. using System.Text;
  13. using Aop.Api.Util;
  14. namespace MySystem
  15. {
  16. public class PublicFunction
  17. {
  18. #region 中译英
  19. public string TranslateZHToEn(string Source)
  20. {
  21. string result = function.GetWebRequest("http://fanyi.youdao.com/translate?&doctype=json&type=ZH_CN2EN&i=" + Source);
  22. //{"type":"ZH_CN2EN","errorCode":0,"elapsedTime":2,"translateResult":[[{"src":"大事件","tgt":"The big event"}]]}
  23. Match match = Regex.Match(result, "\"tgt\":\".*?\"");
  24. if (match.Success)
  25. {
  26. return match.Value.Replace("\"tgt\":", "").Trim('"');
  27. }
  28. return "";
  29. }
  30. #endregion
  31. #region 解析编辑器里的视频代码
  32. public string CheckMediaFromHtml(string content)
  33. {
  34. if (string.IsNullOrEmpty(content))
  35. {
  36. return "";
  37. }
  38. string result = content;
  39. MatchCollection mc = Regex.Matches(content, "<embed.*?/>");
  40. foreach (Match submc in mc)
  41. {
  42. string info = submc.Value;
  43. Match match = Regex.Match(info, "src=\".*?\"");
  44. if (match.Success)
  45. {
  46. string path = match.Value;
  47. path = path.Replace("src=\"", "");
  48. path = path.TrimEnd(new char[] { '"' });
  49. string html = "";
  50. string width = "600";
  51. string height = "300";
  52. Match width_match = Regex.Match(info, "width=\".*?\"");
  53. if (width_match.Success)
  54. {
  55. width = width_match.Value.Replace("width=", "").Trim('"');
  56. }
  57. Match height_match = Regex.Match(info, "height=\".*?\"");
  58. if (height_match.Success)
  59. {
  60. height = height_match.Value.Replace("height=", "").Trim('"');
  61. }
  62. if (path.EndsWith(".mp4"))
  63. {
  64. if (string.IsNullOrEmpty(html))
  65. {
  66. html = "<video controls=\"controls\" autoplay=\"autoplay\" poster=\"\" onplay=\"true\" width=\"" + width + "\" height=\"" + height + "\" onclick=\"this.play();\">" +
  67. "<source src=\"" + path + "\">" +
  68. "<source src=\"" + path + "\" type=\"video/mp4\">" +
  69. "<source src=\"" + path + "\" type=\"video/webm\">" +
  70. "<source src=\"" + path + "\" type=\"video/ogg\">" +
  71. "</video>";
  72. }
  73. }
  74. else if (path.EndsWith(".mp3"))
  75. {
  76. html = "<audio controls=\"controls\" width=\"" + width + "\" height=\"" + height + "\" onclick=\"this.play();\">" +
  77. "<source src=\"" + path + "\" type=\"audio/mp3\" />" +
  78. "<embed src=\"" + path + "\" height=\"100\" width=\"100\" />" +
  79. "</audio>";
  80. }
  81. result = result.Replace(info, html);
  82. }
  83. }
  84. return result;
  85. }
  86. #endregion
  87. #region 对象转Json字符串
  88. public static string ObjectToJsonString(object obj)
  89. {
  90. return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  91. }
  92. #endregion
  93. #region Json字符串转对象
  94. public static T DeserializeJSON<T>(string json)
  95. {
  96. return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
  97. }
  98. #endregion
  99. #region 删除文件
  100. public static bool DeleteFile(string VirtualPath)
  101. {
  102. string FilePath = function.getPath(VirtualPath);
  103. if (System.IO.File.Exists(FilePath))
  104. {
  105. System.IO.File.Delete(FilePath);
  106. return true;
  107. }
  108. return false;
  109. }
  110. #endregion
  111. #region 两点距离
  112. public static double GetDistanceNumber(string start, string end)
  113. {
  114. if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
  115. {
  116. string[] startpos = start.Split(',');
  117. string[] endpos = end.Split(',');
  118. double lng1 = double.Parse(startpos[0]);
  119. double lat1 = double.Parse(startpos[1]);
  120. double lng2 = double.Parse(endpos[0]);
  121. double lat2 = double.Parse(endpos[1]);
  122. double radLat1 = rad(lat1);
  123. double radLat2 = rad(lat2);
  124. double a = radLat1 - radLat2;
  125. double b = rad(lng1) - rad(lng2);
  126. 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)));
  127. s = s * EARTH_RADIUS;
  128. s = Math.Round(s * 10000) / 10000;
  129. return s;
  130. }
  131. return 10000000;
  132. }
  133. private static double rad(double d)
  134. {
  135. return d * Math.PI / 180.0;
  136. }
  137. private static double EARTH_RADIUS = 6378.137;
  138. #endregion
  139. #region 获取OSS开关
  140. public string GetOssStatus()
  141. {
  142. return "-oss";
  143. }
  144. #endregion
  145. #region 通过表名、文本、值获得字典数据
  146. public Dictionary<string, string> GetDictionaryByTableName(string tableEnName, string Text, string Value)
  147. {
  148. Text = Text.Split('_')[0];
  149. Value = Value.Split('_')[0];
  150. Dictionary<string, string> dic = new Dictionary<string, string>();
  151. DataTable dt = dbconn.dtable("select " + Text + "," + Value + " from " + tableEnName + " order by Sort desc,Id asc");
  152. foreach (DataRow dr in dt.Rows)
  153. {
  154. dic.Add(dr[1].ToString(), dr[0].ToString());
  155. }
  156. return dic;
  157. }
  158. #endregion
  159. #region 接口通用DES解密
  160. public static string DesDecrypt(string content)
  161. {
  162. content = HttpUtility.UrlDecode(content);
  163. return dbconn.DesDecrypt(content, "*ga34|^7");
  164. }
  165. #endregion
  166. #region 获取jwt的token
  167. public static string AppToken(string username)
  168. {
  169. string test = function.get_Random(10);
  170. var securityKey = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(JwtConfig.JwtSecret)), SecurityAlgorithms.HmacSha256);
  171. var claims = new Claim[] {
  172. new Claim(JwtRegisteredClaimNames.Iss, JwtConfig.JwtIss),
  173. new Claim(JwtRegisteredClaimNames.Aud, test),
  174. new Claim("Guid", Guid.NewGuid().ToString("D")),
  175. new Claim(ClaimTypes.Role, "system"),
  176. new Claim(ClaimTypes.Role, "admin"),
  177. };
  178. SecurityToken securityToken = new JwtSecurityToken(
  179. signingCredentials: securityKey,
  180. expires: DateTime.Now.AddDays(10),//过期时间
  181. claims: claims,
  182. audience: test,
  183. issuer: username
  184. );
  185. RedisDbconn.Instance.Set("utoken:" + username, test);
  186. RedisDbconn.Instance.SetExpire("utoken:" + username, 3600 * 24 * 10);
  187. //生成jwt令牌
  188. return new JwtSecurityTokenHandler().WriteToken(securityToken);
  189. }
  190. #endregion
  191. public static decimal NumberFormat(decimal number, int floatCount = 2)
  192. {
  193. string str = number.ToString();
  194. if (str.Contains("."))
  195. {
  196. string[] list = str.Split('.');
  197. if (list[1].Length > floatCount)
  198. {
  199. str = list[0] + "." + list[1].Substring(0, floatCount);
  200. }
  201. }
  202. else
  203. {
  204. str += ".00";
  205. }
  206. return decimal.Parse(str);
  207. }
  208. #region 获取网络文件内容
  209. public static string GetNetFileContent(string url)
  210. {
  211. string textContent = "";
  212. using (var client = new System.Net.WebClient())
  213. {
  214. try
  215. {
  216. textContent = client.DownloadString(url); // 通过 DownloadString 方法获取网页内容
  217. }
  218. catch (Exception ex)
  219. {
  220. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "获取网络文件内容异常");
  221. }
  222. }
  223. return textContent;
  224. }
  225. public static byte[] GetNetFileData(string url)
  226. {
  227. byte[] textContent = new byte[] { };
  228. using (var client = new System.Net.WebClient())
  229. {
  230. try
  231. {
  232. textContent = client.DownloadData(url); // 通过 DownloadString 方法获取网页内容
  233. }
  234. catch (Exception ex)
  235. {
  236. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "获取网络文件流异常");
  237. }
  238. }
  239. return textContent;
  240. }
  241. public static FileItem GetNetFileItem(string url)
  242. {
  243. string fileName = url;
  244. if(fileName.Contains("/"))
  245. {
  246. fileName = fileName.Substring(fileName.LastIndexOf("/") + 1);
  247. }
  248. FileItem item = new FileItem(fileName, GetNetFileData(url));
  249. return item;
  250. }
  251. #endregion
  252. }
  253. }