PublicFunction.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Text.RegularExpressions;
  6. using MySystem.BsModels;
  7. using Library;
  8. using NPOI.SS.UserModel;
  9. using NPOI.XSSF.UserModel;
  10. using System.IO;
  11. namespace MySystem
  12. {
  13. public class PublicFunction
  14. {
  15. public WebCMSEntities db = new WebCMSEntities();
  16. public BsModels.WebCMSEntities bsdb = new BsModels.WebCMSEntities();
  17. #region 获取权限JSON数据
  18. public string GetRightJson()
  19. {
  20. string result = "[";
  21. List<BsModels.RightDic> list = bsdb.RightDic.ToList();
  22. List<BsModels.RightDic> Level1 = list.Where(m => m.RightLevel == 1).ToList();
  23. foreach (BsModels.RightDic sub1 in Level1)
  24. {
  25. result += "{title: '" + sub1.Name + "', id: '" + sub1.Id + "', spread: false, children: [";
  26. List<BsModels.RightDic> Level2 = list.Where(m => m.RightLevel == 2 && m.Id.StartsWith(sub1.Id)).ToList();
  27. if (Level2.Count > 0)
  28. {
  29. foreach (BsModels.RightDic sub2 in Level2)
  30. {
  31. result += "{title: '" + sub2.Name + "', id: '" + sub2.Id + "', spread: false, children: [";
  32. List<BsModels.RightDic> Level3 = list.Where(m => m.RightLevel == 3 && m.Id.StartsWith(sub2.Id)).ToList();
  33. if (Level3.Count > 0)
  34. {
  35. foreach (BsModels.RightDic sub3 in Level3)
  36. {
  37. result += "{title: '" + sub3.Name + "', id: '" + sub3.Id + "', spread: false, children: [";
  38. string rightString = ForOperateRight(sub3.Id);
  39. if (!string.IsNullOrEmpty(rightString))
  40. {
  41. result += rightString;
  42. }
  43. else
  44. {
  45. result += "{title: '基本权限', id: '" + sub3.Id + "_base'}";
  46. }
  47. result += "]},";
  48. }
  49. result = result.TrimEnd(',');
  50. }
  51. else
  52. {
  53. string rightString = ForOperateRight(sub2.Id);
  54. if (!string.IsNullOrEmpty(rightString))
  55. {
  56. result += rightString;
  57. }
  58. else
  59. {
  60. result += "{title: '基本权限', id: '" + sub2.Id + "_base'}";
  61. }
  62. }
  63. result += "]},";
  64. }
  65. result = result.TrimEnd(',');
  66. }
  67. else
  68. {
  69. result += ForOperateRight(sub1.Id);
  70. string rightString = ForOperateRight(sub1.Id);
  71. if (!string.IsNullOrEmpty(rightString))
  72. {
  73. result += rightString;
  74. }
  75. else
  76. {
  77. result += "{title: '基本权限', id: '" + sub1.Id + "_base'}";
  78. }
  79. }
  80. result += "]},";
  81. }
  82. result = result.TrimEnd(',');
  83. result += "]";
  84. return result;
  85. }
  86. //读取当前菜单有哪些权限
  87. public string ForOperateRight(string Id)
  88. {
  89. string result = "";
  90. List<BsModels.MenuRight> rights = bsdb.MenuRight.Where(m => m.MenuId.StartsWith(Id)).OrderBy(m => m.MenuId).ToList();
  91. foreach (BsModels.MenuRight right in rights)
  92. {
  93. result += "{title: '" + right.Name + "', id: '" + right.MenuId + "'},";
  94. }
  95. BsModels.RightDic rightDic = bsdb.RightDic.FirstOrDefault(m => m.Id == Id) ?? new BsModels.RightDic();
  96. if (!string.IsNullOrEmpty(rightDic.OtherRight))
  97. {
  98. string[] OtherRightList = rightDic.OtherRight.Split('\n');
  99. foreach (string SubOtherRight in OtherRightList)
  100. {
  101. string[] SubOtherRightData = SubOtherRight.Split('_');
  102. result += "{title: '" + SubOtherRightData[1] + "', id: '" + Id + "_" + SubOtherRightData[0] + "'},";
  103. }
  104. }
  105. result = result.TrimEnd(',');
  106. return result;
  107. }
  108. public string TranslateRightString(string RightString)
  109. {
  110. string result = "";
  111. if (!string.IsNullOrEmpty(RightString))
  112. {
  113. string[] RightList = RightString.Split(',');
  114. foreach (string sub in RightList)
  115. {
  116. string EndString = sub.Substring(sub.LastIndexOf("_") + 1);
  117. if (EndString.Length > 1 && !function.IsInt(EndString))
  118. {
  119. result += sub + ",";
  120. }
  121. }
  122. }
  123. return result.TrimEnd(',');
  124. }
  125. #endregion
  126. #region 中译英
  127. public string TranslateZHToEn(string Source)
  128. {
  129. string result = function.GetWebRequest("http://fanyi.youdao.com/translate?&doctype=json&type=ZH_CN2EN&i=" + Source);
  130. //{"type":"ZH_CN2EN","errorCode":0,"elapsedTime":2,"translateResult":[[{"src":"大事件","tgt":"The big event"}]]}
  131. Match match = Regex.Match(result, "\"tgt\":\".*?\"");
  132. if (match.Success)
  133. {
  134. return match.Value.Replace("\"tgt\":", "").Trim('"');
  135. }
  136. return "";
  137. }
  138. #endregion
  139. #region 解析编辑器里的视频代码
  140. public string CheckMediaFromHtml(string content)
  141. {
  142. if (string.IsNullOrEmpty(content))
  143. {
  144. return "";
  145. }
  146. string result = content;
  147. MatchCollection mc = Regex.Matches(content, "<embed.*?/>");
  148. foreach (Match submc in mc)
  149. {
  150. string info = submc.Value;
  151. Match match = Regex.Match(info, "src=\".*?\"");
  152. if (match.Success)
  153. {
  154. string path = match.Value;
  155. path = path.Replace("src=\"", "");
  156. path = path.TrimEnd(new char[] { '"' });
  157. string html = "";
  158. string width = "600";
  159. string height = "300";
  160. Match width_match = Regex.Match(info, "width=\".*?\"");
  161. if (width_match.Success)
  162. {
  163. width = width_match.Value.Replace("width=", "").Trim('"');
  164. }
  165. Match height_match = Regex.Match(info, "height=\".*?\"");
  166. if (height_match.Success)
  167. {
  168. height = height_match.Value.Replace("height=", "").Trim('"');
  169. }
  170. if (path.EndsWith(".mp4"))
  171. {
  172. if (string.IsNullOrEmpty(html))
  173. {
  174. html = "<video controls=\"controls\" autoplay=\"autoplay\" poster=\"\" onplay=\"true\" width=\"" + width + "\" height=\"" + height + "\" onclick=\"this.play();\">" +
  175. "<source src=\"" + path + "\">" +
  176. "<source src=\"" + path + "\" type=\"video/mp4\">" +
  177. "<source src=\"" + path + "\" type=\"video/webm\">" +
  178. "<source src=\"" + path + "\" type=\"video/ogg\">" +
  179. "</video>";
  180. }
  181. }
  182. else if (path.EndsWith(".mp3"))
  183. {
  184. html = "<audio controls=\"controls\" width=\"" + width + "\" height=\"" + height + "\" onclick=\"this.play();\">" +
  185. "<source src=\"" + path + "\" type=\"audio/mp3\" />" +
  186. "<embed src=\"" + path + "\" height=\"100\" width=\"100\" />" +
  187. "</audio>";
  188. }
  189. result = result.Replace(info, html);
  190. }
  191. }
  192. return result;
  193. }
  194. #endregion
  195. #region 对象转Json字符串
  196. public static string ObjectToJsonString(object obj)
  197. {
  198. return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  199. }
  200. #endregion
  201. #region Json字符串转对象
  202. public static T DeserializeJSON<T>(string json)
  203. {
  204. return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
  205. }
  206. #endregion
  207. #region 删除文件
  208. public static bool DeleteFile(string VirtualPath)
  209. {
  210. string FilePath = function.getPath(VirtualPath);
  211. if (System.IO.File.Exists(FilePath))
  212. {
  213. System.IO.File.Delete(FilePath);
  214. return true;
  215. }
  216. return false;
  217. }
  218. #endregion
  219. #region 两点距离
  220. public static double GetDistanceNumber(string start, string end)
  221. {
  222. if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
  223. {
  224. string[] startpos = start.Split(',');
  225. string[] endpos = end.Split(',');
  226. double lng1 = double.Parse(startpos[0]);
  227. double lat1 = double.Parse(startpos[1]);
  228. double lng2 = double.Parse(endpos[0]);
  229. double lat2 = double.Parse(endpos[1]);
  230. double radLat1 = rad(lat1);
  231. double radLat2 = rad(lat2);
  232. double a = radLat1 - radLat2;
  233. double b = rad(lng1) - rad(lng2);
  234. 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)));
  235. s = s * EARTH_RADIUS;
  236. s = Math.Round(s * 10000) / 10000;
  237. return s;
  238. }
  239. return 10000000;
  240. }
  241. private static double rad(double d)
  242. {
  243. return d * Math.PI / 180.0;
  244. }
  245. private static double EARTH_RADIUS = 6378.137;
  246. #endregion
  247. #region 短信条数
  248. public int SMSCount()
  249. {
  250. SystemSet check = db.SystemSet.FirstOrDefault() ?? new SystemSet();
  251. return check.QueryCount;
  252. }
  253. #endregion
  254. #region 短信消耗
  255. public void UseSMS()
  256. {
  257. SystemSet check = db.SystemSet.FirstOrDefault();
  258. if (check != null)
  259. {
  260. check.QueryCount -= 1;
  261. db.SaveChanges();
  262. }
  263. }
  264. #endregion
  265. #region 获取OSS开关
  266. public string GetOssStatus()
  267. {
  268. SystemSet set = db.SystemSet.FirstOrDefault() ?? new SystemSet();
  269. return set.UploadOss == 1 ? "-oss" : "";
  270. }
  271. #endregion
  272. #region 获取上传文件参数
  273. public string GetUploadParam(string buttonId)
  274. {
  275. string tag = function.MD5_16(buttonId);
  276. string param = RedisDbconn.Instance.Get<string>("btn:" + tag);
  277. if (!string.IsNullOrEmpty(param))
  278. {
  279. return param;
  280. }
  281. SystemSet set = db.SystemSet.FirstOrDefault() ?? new SystemSet();
  282. return "{width:" + set.UploadAutoZoomWidth + ",height:" + set.UploadAutoZoomHeight + ",quality:" + set.UploadAutoZoomQuality + "},{max_file_size:" + set.UploadMaxSize + "}";
  283. }
  284. #endregion
  285. #region 获取上传文件提示文字
  286. public string GetUploadHint(string buttonId, bool ispic = true)
  287. {
  288. string tag = function.MD5_16(buttonId);
  289. string param = RedisDbconn.Instance.Get<string>("btn:" + tag);
  290. if (string.IsNullOrEmpty(param))
  291. {
  292. SystemSet set = db.SystemSet.FirstOrDefault() ?? new SystemSet();
  293. param = "{width:" + set.UploadAutoZoomWidth + ",height:" + set.UploadAutoZoomHeight + ",quality:" + set.UploadAutoZoomQuality + "},{max_file_size:" + set.UploadMaxSize + "}";
  294. }
  295. string[] json = param.Replace(" ", "").Replace("},{", "}#cut#{").Split(new string[] { "#cut#" }, StringSplitOptions.None);
  296. Dictionary<string, string> resize = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(json[0]);
  297. Dictionary<string, string> size = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(json[1]);
  298. int max_file_size = int.Parse(size["max_file_size"]) / 1024;
  299. string unit = "KB";
  300. if (max_file_size / 1024 > 0)
  301. {
  302. max_file_size = max_file_size / 1024;
  303. unit = "MB";
  304. }
  305. if (max_file_size / 1024 > 0)
  306. {
  307. max_file_size = max_file_size / 1024;
  308. unit = "GB";
  309. }
  310. if (ispic)
  311. {
  312. return "建议尺寸" + resize["width"] + "x" + resize["height"] + "," + max_file_size + unit + "以内";
  313. }
  314. return "建议" + max_file_size + "以内";
  315. }
  316. #endregion
  317. #region 通过表名、文本、值获得字典数据
  318. public Dictionary<string, string> GetDictionaryByTableName(string tableEnName, string Text, string Value)
  319. {
  320. Text = Text.Split('_')[0];
  321. Value = Value.Split('_')[0];
  322. Dictionary<string, string> dic = new Dictionary<string, string>();
  323. DataTable dt = dbconn.dtable("select " + Text + "," + Value + " from " + tableEnName + " order by Sort desc,Id asc");
  324. foreach (DataRow dr in dt.Rows)
  325. {
  326. dic.Add(dr[1].ToString(), dr[0].ToString());
  327. }
  328. return dic;
  329. }
  330. #endregion
  331. #region 获取Excel文件内容
  332. public DataTable ExcelToDataTable(string filepath)
  333. {
  334. List<string> result = new List<string>();
  335. DataTable dtTable = new DataTable();
  336. List<string> rowList = new List<string>();
  337. try
  338. {
  339. ISheet sheet;
  340. using (var stream = new FileStream(filepath, FileMode.Open))
  341. {
  342. stream.Position = 0;
  343. XSSFWorkbook xssWorkbook = new XSSFWorkbook(stream);
  344. sheet = xssWorkbook.GetSheetAt(0);
  345. IRow headerRow = sheet.GetRow(0);
  346. int cellCount = headerRow.LastCellNum;
  347. for (int j = 0; j < cellCount; j++)
  348. {
  349. ICell cell = headerRow.GetCell(j);
  350. if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue;
  351. {
  352. dtTable.Columns.Add(cell.ToString());
  353. }
  354. }
  355. for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
  356. {
  357. IRow row = sheet.GetRow(i);
  358. if (row == null) continue;
  359. if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
  360. for (int j = row.FirstCellNum; j < cellCount; j++)
  361. {
  362. if (row.GetCell(j) != null)
  363. {
  364. if (!string.IsNullOrEmpty(row.GetCell(j).ToString()) && !string.IsNullOrWhiteSpace(row.GetCell(j).ToString()))
  365. {
  366. rowList.Add(row.GetCell(j).ToString());
  367. }
  368. }
  369. }
  370. if (rowList.Count > 0)
  371. dtTable.Rows.Add(rowList.ToArray());
  372. rowList.Clear();
  373. }
  374. }
  375. }
  376. catch (Exception ex)
  377. {
  378. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "获取Excel文件内容异常");
  379. }
  380. return dtTable;
  381. }
  382. #endregion
  383. #region 获取网络文件内容
  384. public static string GetNetFileContent(string url)
  385. {
  386. string textContent = "";
  387. using (var client = new System.Net.WebClient())
  388. {
  389. try
  390. {
  391. textContent = client.DownloadString(url); // 通过 DownloadString 方法获取网页内容
  392. }
  393. catch (Exception ex)
  394. {
  395. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "获取网络文件内容异常");
  396. }
  397. }
  398. return textContent;
  399. }
  400. #endregion
  401. #region 测试创客Id
  402. public static int[] testids = { };
  403. #endregion
  404. #region 数据库结构
  405. public static Dictionary<string, Dictionary<string, string>> BsTables = new Dictionary<string, Dictionary<string, string>>();
  406. #endregion
  407. }
  408. }