PublicFunction.cs 7.9 KB

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