123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Data;
- using System.Text.RegularExpressions;
- using MySystem.BsModels;
- using Library;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using System.IO;
- namespace MySystem
- {
- public class PublicFunction
- {
- public WebCMSEntities db = new WebCMSEntities();
- public BsModels.WebCMSEntities bsdb = new BsModels.WebCMSEntities();
- #region 获取权限JSON数据
- public string GetRightJson()
- {
- string result = "[";
- List<BsModels.RightDic> list = bsdb.RightDic.ToList();
- List<BsModels.RightDic> Level1 = list.Where(m => m.RightLevel == 1).ToList();
- foreach (BsModels.RightDic sub1 in Level1)
- {
- result += "{title: '" + sub1.Name + "', id: '" + sub1.Id + "', spread: false, children: [";
- List<BsModels.RightDic> Level2 = list.Where(m => m.RightLevel == 2 && m.Id.StartsWith(sub1.Id)).ToList();
- if (Level2.Count > 0)
- {
- foreach (BsModels.RightDic sub2 in Level2)
- {
- result += "{title: '" + sub2.Name + "', id: '" + sub2.Id + "', spread: false, children: [";
- List<BsModels.RightDic> Level3 = list.Where(m => m.RightLevel == 3 && m.Id.StartsWith(sub2.Id)).ToList();
- if (Level3.Count > 0)
- {
- foreach (BsModels.RightDic sub3 in Level3)
- {
- result += "{title: '" + sub3.Name + "', id: '" + sub3.Id + "', spread: false, children: [";
- string rightString = ForOperateRight(sub3.Id);
- if (!string.IsNullOrEmpty(rightString))
- {
- result += rightString;
- }
- else
- {
- result += "{title: '基本权限', id: '" + sub3.Id + "_base'}";
- }
- result += "]},";
- }
- result = result.TrimEnd(',');
- }
- else
- {
- string rightString = ForOperateRight(sub2.Id);
- if (!string.IsNullOrEmpty(rightString))
- {
- result += rightString;
- }
- else
- {
- result += "{title: '基本权限', id: '" + sub2.Id + "_base'}";
- }
- }
- result += "]},";
- }
- result = result.TrimEnd(',');
- }
- else
- {
- result += ForOperateRight(sub1.Id);
- string rightString = ForOperateRight(sub1.Id);
- if (!string.IsNullOrEmpty(rightString))
- {
- result += rightString;
- }
- else
- {
- result += "{title: '基本权限', id: '" + sub1.Id + "_base'}";
- }
- }
- result += "]},";
- }
- result = result.TrimEnd(',');
- result += "]";
- return result;
- }
- //读取当前菜单有哪些权限
- public string ForOperateRight(string Id)
- {
- string result = "";
- List<BsModels.MenuRight> rights = bsdb.MenuRight.Where(m => m.MenuId.StartsWith(Id)).OrderBy(m => m.MenuId).ToList();
- foreach (BsModels.MenuRight right in rights)
- {
- result += "{title: '" + right.Name + "', id: '" + right.MenuId + "'},";
- }
- BsModels.RightDic rightDic = bsdb.RightDic.FirstOrDefault(m => m.Id == Id) ?? new BsModels.RightDic();
- if (!string.IsNullOrEmpty(rightDic.OtherRight))
- {
- string[] OtherRightList = rightDic.OtherRight.Split('\n');
- foreach (string SubOtherRight in OtherRightList)
- {
- string[] SubOtherRightData = SubOtherRight.Split('_');
- result += "{title: '" + SubOtherRightData[1] + "', id: '" + Id + "_" + SubOtherRightData[0] + "'},";
- }
- }
- result = result.TrimEnd(',');
- return result;
- }
- public string TranslateRightString(string RightString)
- {
- string result = "";
- if (!string.IsNullOrEmpty(RightString))
- {
- string[] RightList = RightString.Split(',');
- foreach (string sub in RightList)
- {
- string EndString = sub.Substring(sub.LastIndexOf("_") + 1);
- if (EndString.Length > 1 && !function.IsInt(EndString))
- {
- result += sub + ",";
- }
- }
- }
- return result.TrimEnd(',');
- }
- #endregion
- #region 中译英
- public string TranslateZHToEn(string Source)
- {
- string result = function.GetWebRequest("http://fanyi.youdao.com/translate?&doctype=json&type=ZH_CN2EN&i=" + Source);
- //{"type":"ZH_CN2EN","errorCode":0,"elapsedTime":2,"translateResult":[[{"src":"大事件","tgt":"The big event"}]]}
- Match match = Regex.Match(result, "\"tgt\":\".*?\"");
- if (match.Success)
- {
- return match.Value.Replace("\"tgt\":", "").Trim('"');
- }
- return "";
- }
- #endregion
- #region 解析编辑器里的视频代码
- public string CheckMediaFromHtml(string content)
- {
- if (string.IsNullOrEmpty(content))
- {
- return "";
- }
- string result = content;
- MatchCollection mc = Regex.Matches(content, "<embed.*?/>");
- foreach (Match submc in mc)
- {
- string info = submc.Value;
- Match match = Regex.Match(info, "src=\".*?\"");
- if (match.Success)
- {
- string path = match.Value;
- path = path.Replace("src=\"", "");
- path = path.TrimEnd(new char[] { '"' });
- string html = "";
- string width = "600";
- string height = "300";
- Match width_match = Regex.Match(info, "width=\".*?\"");
- if (width_match.Success)
- {
- width = width_match.Value.Replace("width=", "").Trim('"');
- }
- Match height_match = Regex.Match(info, "height=\".*?\"");
- if (height_match.Success)
- {
- height = height_match.Value.Replace("height=", "").Trim('"');
- }
- if (path.EndsWith(".mp4"))
- {
- if (string.IsNullOrEmpty(html))
- {
- html = "<video controls=\"controls\" autoplay=\"autoplay\" poster=\"\" onplay=\"true\" width=\"" + width + "\" height=\"" + height + "\" onclick=\"this.play();\">" +
- "<source src=\"" + path + "\">" +
- "<source src=\"" + path + "\" type=\"video/mp4\">" +
- "<source src=\"" + path + "\" type=\"video/webm\">" +
- "<source src=\"" + path + "\" type=\"video/ogg\">" +
- "</video>";
- }
- }
- else if (path.EndsWith(".mp3"))
- {
- html = "<audio controls=\"controls\" width=\"" + width + "\" height=\"" + height + "\" onclick=\"this.play();\">" +
- "<source src=\"" + path + "\" type=\"audio/mp3\" />" +
- "<embed src=\"" + path + "\" height=\"100\" width=\"100\" />" +
- "</audio>";
- }
- result = result.Replace(info, html);
- }
- }
- return result;
- }
- #endregion
- #region 对象转Json字符串
- public static string ObjectToJsonString(object obj)
- {
- return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
- }
- #endregion
- #region Json字符串转对象
- public static T DeserializeJSON<T>(string json)
- {
- return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
- }
- #endregion
- #region 删除文件
- public static bool DeleteFile(string VirtualPath)
- {
- string FilePath = function.getPath(VirtualPath);
- if (System.IO.File.Exists(FilePath))
- {
- System.IO.File.Delete(FilePath);
- return true;
- }
- return false;
- }
- #endregion
- #region 两点距离
- public static double GetDistanceNumber(string start, string end)
- {
- if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
- {
- string[] startpos = start.Split(',');
- string[] endpos = end.Split(',');
- double lng1 = double.Parse(startpos[0]);
- double lat1 = double.Parse(startpos[1]);
- double lng2 = double.Parse(endpos[0]);
- double lat2 = double.Parse(endpos[1]);
- double radLat1 = rad(lat1);
- double radLat2 = rad(lat2);
- double a = radLat1 - radLat2;
- double b = rad(lng1) - rad(lng2);
- 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)));
- s = s * EARTH_RADIUS;
- s = Math.Round(s * 10000) / 10000;
- return s;
- }
- return 10000000;
- }
- private static double rad(double d)
- {
- return d * Math.PI / 180.0;
- }
- private static double EARTH_RADIUS = 6378.137;
- #endregion
-
- #region 短信条数
- public int SMSCount()
- {
- SystemSet check = db.SystemSet.FirstOrDefault() ?? new SystemSet();
- return check.QueryCount;
- }
- #endregion
- #region 短信消耗
- public void UseSMS()
- {
- SystemSet check = db.SystemSet.FirstOrDefault();
- if (check != null)
- {
- check.QueryCount -= 1;
- db.SaveChanges();
- }
- }
- #endregion
- #region 获取OSS开关
- public string GetOssStatus()
- {
- SystemSet set = db.SystemSet.FirstOrDefault() ?? new SystemSet();
- return set.UploadOss == 1 ? "-oss" : "";
- }
- #endregion
- #region 获取上传文件参数
- public string GetUploadParam(string buttonId)
- {
- string tag = function.MD5_16(buttonId);
- string param = RedisDbconn.Instance.Get<string>("btn:" + tag);
- if (!string.IsNullOrEmpty(param))
- {
- return param;
- }
- SystemSet set = db.SystemSet.FirstOrDefault() ?? new SystemSet();
- return "{width:" + set.UploadAutoZoomWidth + ",height:" + set.UploadAutoZoomHeight + ",quality:" + set.UploadAutoZoomQuality + "},{max_file_size:" + set.UploadMaxSize + "}";
- }
- #endregion
- #region 获取上传文件提示文字
- public string GetUploadHint(string buttonId, bool ispic = true)
- {
- string tag = function.MD5_16(buttonId);
- string param = RedisDbconn.Instance.Get<string>("btn:" + tag);
- if (string.IsNullOrEmpty(param))
- {
- SystemSet set = db.SystemSet.FirstOrDefault() ?? new SystemSet();
- param = "{width:" + set.UploadAutoZoomWidth + ",height:" + set.UploadAutoZoomHeight + ",quality:" + set.UploadAutoZoomQuality + "},{max_file_size:" + set.UploadMaxSize + "}";
- }
- string[] json = param.Replace(" ", "").Replace("},{", "}#cut#{").Split(new string[] { "#cut#" }, StringSplitOptions.None);
- Dictionary<string, string> resize = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(json[0]);
- Dictionary<string, string> size = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(json[1]);
- int max_file_size = int.Parse(size["max_file_size"]) / 1024;
- string unit = "KB";
- if (max_file_size / 1024 > 0)
- {
- max_file_size = max_file_size / 1024;
- unit = "MB";
- }
- if (max_file_size / 1024 > 0)
- {
- max_file_size = max_file_size / 1024;
- unit = "GB";
- }
- if (ispic)
- {
- return "建议尺寸" + resize["width"] + "x" + resize["height"] + "," + max_file_size + unit + "以内";
- }
- return "建议" + max_file_size + "以内";
- }
- #endregion
- #region 通过表名、文本、值获得字典数据
- public Dictionary<string, string> GetDictionaryByTableName(string tableEnName, string Text, string Value)
- {
- Text = Text.Split('_')[0];
- Value = Value.Split('_')[0];
- Dictionary<string, string> dic = new Dictionary<string, string>();
- DataTable dt = dbconn.dtable("select " + Text + "," + Value + " from " + tableEnName + " order by Sort desc,Id asc");
- foreach (DataRow dr in dt.Rows)
- {
- dic.Add(dr[1].ToString(), dr[0].ToString());
- }
- return dic;
- }
- #endregion
- #region 获取Excel文件内容
- public DataTable ExcelToDataTable(string filepath)
- {
- List<string> result = new List<string>();
- DataTable dtTable = new DataTable();
- List<string> rowList = new List<string>();
- try
- {
- ISheet sheet;
- using (var stream = new FileStream(filepath, FileMode.Open))
- {
- stream.Position = 0;
- XSSFWorkbook xssWorkbook = new XSSFWorkbook(stream);
- sheet = xssWorkbook.GetSheetAt(0);
- IRow headerRow = sheet.GetRow(0);
- int cellCount = headerRow.LastCellNum;
- for (int j = 0; j < cellCount; j++)
- {
- ICell cell = headerRow.GetCell(j);
- if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue;
- {
- dtTable.Columns.Add(cell.ToString());
- }
- }
- for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
- {
- IRow row = sheet.GetRow(i);
- if (row == null) continue;
- if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
- for (int j = row.FirstCellNum; j < cellCount; j++)
- {
- if (row.GetCell(j) != null)
- {
- if (!string.IsNullOrEmpty(row.GetCell(j).ToString()) && !string.IsNullOrWhiteSpace(row.GetCell(j).ToString()))
- {
- rowList.Add(row.GetCell(j).ToString());
- }
- }
- }
- if (rowList.Count > 0)
- dtTable.Rows.Add(rowList.ToArray());
- rowList.Clear();
- }
- }
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "获取Excel文件内容异常");
- }
- return dtTable;
- }
- #endregion
- #region 获取网络文件内容
- public static string GetNetFileContent(string url)
- {
- string textContent = "";
- using (var client = new System.Net.WebClient())
- {
- try
- {
- textContent = client.DownloadString(url); // 通过 DownloadString 方法获取网页内容
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "获取网络文件内容异常");
- }
- }
- return textContent;
- }
- #endregion
- #region 测试创客Id
- public static int[] testids = { };
- #endregion
- #region 数据库结构
- public static Dictionary<string, Dictionary<string, string>> BsTables = new Dictionary<string, Dictionary<string, string>>();
- #endregion
- }
- }
|