diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Convert.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Convert.cs new file mode 100644 index 00000000..0e825ab4 --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Convert.cs @@ -0,0 +1,446 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Infrastructure.Extensions +{ + public static partial class Extensions + { + #region 转换为long + /// + /// 将object转换为long,若转换失败,则返回0。不抛出异常。 + /// + /// + /// + public static long ParseToLong(this object obj) + { + try + { + return long.Parse(obj.ToString()); + } + catch + { + return 0L; + } + } + + /// + /// 将object转换为long,若转换失败,则返回指定值。不抛出异常。 + /// + /// + /// + /// + public static long ParseToLong(this string str, long defaultValue) + { + try + { + return long.Parse(str); + } + catch + { + return defaultValue; + } + } + #endregion + + #region 转换为int + /// + /// 将object转换为int,若转换失败,则返回0。不抛出异常。 + /// + /// + /// + public static int ParseToInt(this object str) + { + try + { + return Convert.ToInt32(str); + } + catch + { + return 0; + } + } + + /// + /// 将object转换为int,若转换失败,则返回指定值。不抛出异常。 + /// null返回默认值 + /// + /// + /// + /// + public static int ParseToInt(this object str, int defaultValue) + { + if (str == null) + { + return defaultValue; + } + try + { + return Convert.ToInt32(str); + } + catch + { + return defaultValue; + } + } + #endregion + + #region 转换为short + /// + /// 将object转换为short,若转换失败,则返回0。不抛出异常。 + /// + /// + /// + public static short ParseToShort(this object obj) + { + try + { + return short.Parse(obj.ToString()); + } + catch + { + return 0; + } + } + + /// + /// 将object转换为short,若转换失败,则返回指定值。不抛出异常。 + /// + /// + /// + public static short ParseToShort(this object str, short defaultValue) + { + try + { + return short.Parse(str.ToString()); + } + catch + { + return defaultValue; + } + } + #endregion + + #region 转换为demical + /// + /// 将object转换为demical,若转换失败,则返回指定值。不抛出异常。 + /// + /// + /// + public static decimal ParseToDecimal(this object str, decimal defaultValue) + { + try + { + return decimal.Parse(str.ToString()); + } + catch + { + return defaultValue; + } + } + + /// + /// 将object转换为demical,若转换失败,则返回0。不抛出异常。 + /// + /// + /// + public static decimal ParseToDecimal(this object str) + { + try + { + return decimal.Parse(str.ToString()); + } + catch + { + return 0; + } + } + #endregion + + #region 转化为bool + /// + /// 将object转换为bool,若转换失败,则返回false。不抛出异常。 + /// + /// + /// + public static bool ParseToBool(this object str) + { + try + { + return bool.Parse(str.ToString()); + } + catch + { + return false; + } + } + + /// + /// 将object转换为bool,若转换失败,则返回指定值。不抛出异常。 + /// + /// + /// + public static bool ParseToBool(this object str, bool result) + { + try + { + return bool.Parse(str.ToString()); + } + catch + { + return result; + } + } + #endregion + + #region 转换为float + /// + /// 将object转换为float,若转换失败,则返回0。不抛出异常。 + /// + /// + /// + public static float ParseToFloat(this object str) + { + try + { + return float.Parse(str.ToString()); + } + catch + { + return 0; + } + } + + /// + /// 将object转换为float,若转换失败,则返回指定值。不抛出异常。 + /// + /// + /// + public static float ParseToFloat(this object str, float result) + { + try + { + return float.Parse(str.ToString()); + } + catch + { + return result; + } + } + #endregion + + #region 转换为Guid + /// + /// 将string转换为Guid,若转换失败,则返回Guid.Empty。不抛出异常。 + /// + /// + /// + public static Guid ParseToGuid(this string str) + { + try + { + return new Guid(str); + } + catch + { + return Guid.Empty; + } + } + #endregion + + #region 转换为DateTime + /// + /// 将string转换为DateTime,若转换失败,则返回日期最小值。不抛出异常。 + /// + /// + /// + public static DateTime ParseToDateTime(this string str) + { + try + { + if (string.IsNullOrWhiteSpace(str)) + { + return DateTime.MinValue; + } + if (str.Contains("-") || str.Contains("/")) + { + return DateTime.Parse(str); + } + else + { + int length = str.Length; + switch (length) + { + case 4: + return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture); + case 6: + return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture); + case 8: + return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture); + case 10: + return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture); + case 12: + return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture); + case 14: + return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture); + default: + return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture); + } + } + } + catch + { + return DateTime.MinValue; + } + } + + /// + /// 将string转换为DateTime,若转换失败,则返回默认值。 + /// + /// + /// + /// + public static DateTime ParseToDateTime(this string str, DateTime? defaultValue) + { + try + { + if (string.IsNullOrWhiteSpace(str)) + { + return defaultValue.GetValueOrDefault(); + } + if (str.Contains("-") || str.Contains("/")) + { + return DateTime.Parse(str); + } + else + { + int length = str.Length; + switch (length) + { + case 4: + return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture); + case 6: + return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture); + case 8: + return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture); + case 10: + return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture); + case 12: + return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture); + case 14: + return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture); + default: + return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture); + } + } + } + catch + { + return defaultValue.GetValueOrDefault(); + } + } + #endregion + + #region 转换为string + /// + /// 将object转换为string,若转换失败,则返回""。不抛出异常。 + /// + /// + /// + public static string ParseToString(this object obj) + { + try + { + if (obj == null) + { + return string.Empty; + } + else + { + return obj.ToString(); + } + } + catch + { + return string.Empty; + } + } + public static string ParseToStrings(this object obj) + { + try + { + var list = obj as IEnumerable; + if (list != null) + { + return string.Join(",", list); + } + else + { + return obj.ToString(); + } + } + catch + { + return string.Empty; + } + + } + #endregion + + #region 转换为double + /// + /// 将object转换为double,若转换失败,则返回0。不抛出异常。 + /// + /// + /// + public static double ParseToDouble(this object obj) + { + try + { + return double.Parse(obj.ToString()); + } + catch + { + return 0; + } + } + + /// + /// 将object转换为double,若转换失败,则返回指定值。不抛出异常。 + /// + /// + /// + /// + public static double ParseToDouble(this object str, double defaultValue) + { + try + { + return double.Parse(str.ToString()); + } + catch + { + return defaultValue; + } + } + #endregion + + #region 强制转换类型 + /// + /// 强制转换类型 + /// + /// + /// + /// + public static IEnumerable CastSuper(this IEnumerable source) + { + foreach (object item in source) + { + yield return (TResult)Convert.ChangeType(item, typeof(TResult)); + } + } + #endregion + } +} diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Enum.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Enum.cs new file mode 100644 index 00000000..07d2a300 --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Enum.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +//using Newtonsoft.Json; + +namespace Yi.Framework.Infrastructure.Extensions +{ + public static partial class Extensions + { + #region 枚举成员转成dictionary类型 + /// + /// 转成dictionary类型 + /// + /// + /// + public static Dictionary EnumToDictionary(this Type enumType) + { + Dictionary dictionary = new Dictionary(); + Type typeDescription = typeof(DescriptionAttribute); + FieldInfo[] fields = enumType.GetFields(); + int sValue = 0; + string sText = string.Empty; + foreach (FieldInfo field in fields) + { + if (field.FieldType.IsEnum) + { + sValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)); + object[] arr = field.GetCustomAttributes(typeDescription, true); + if (arr.Length > 0) + { + DescriptionAttribute da = (DescriptionAttribute)arr[0]; + sText = da.Description; + } + else + { + sText = field.Name; + } + dictionary.Add(sValue, sText); + } + } + return dictionary; + } + /// + /// 枚举成员转成键值对Json字符串 + /// + /// + /// + //public static string EnumToDictionaryString(this Type enumType) + //{ + // List> dictionaryList = EnumToDictionary(enumType).ToList(); + // var sJson = JsonConvert.SerializeObject(dictionaryList); + // return sJson; + //} + #endregion + + #region 获取枚举的描述 + /// + /// 获取枚举值对应的描述 + /// + /// + /// + public static string GetDescription(this System.Enum enumType) + { + FieldInfo EnumInfo = enumType.GetType().GetField(enumType.ToString()); + if (EnumInfo != null) + { + DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); + if (EnumAttributes.Length > 0) + { + return EnumAttributes[0].Description; + } + } + return enumType.ToString(); + } + #endregion + + #region 根据值获取枚举的描述 + public static string GetDescriptionByEnum(this object obj) + { + var tEnum = System.Enum.Parse(typeof(T), obj.ParseToString()) as System.Enum; + var description = tEnum.GetDescription(); + return description; + } + #endregion + } +} diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Exception.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Exception.cs new file mode 100644 index 00000000..e59e98f7 --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Exception.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Infrastructure.Extensions +{ + public static partial class Extensions + { + public static Exception GetOriginalException(this Exception ex) + { + if (ex.InnerException == null) return ex; + + return ex.InnerException.GetOriginalException(); + } + } +} diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Linq.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Linq.cs new file mode 100644 index 00000000..542da07f --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Linq.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Infrastructure.Extensions +{ + public static class LinqExtensions + { + public static Expression Property(this Expression expression, string propertyName) + { + return Expression.Property(expression, propertyName); + } + public static Expression AndAlso(this Expression left, Expression right) + { + return Expression.AndAlso(left, right); + } + public static Expression Call(this Expression instance, string methodName, params Expression[] arguments) + { + return Expression.Call(instance, instance.Type.GetMethod(methodName), arguments); + } + public static Expression GreaterThan(this Expression left, Expression right) + { + return Expression.GreaterThan(left, right); + } + public static Expression ToLambda(this Expression body, params ParameterExpression[] parameters) + { + return Expression.Lambda(body, parameters); + } + + public static Expression> True() { return param => true; } + + public static Expression> False() { return param => false; } + + /// + /// 组合And + /// + /// + public static Expression> And(this Expression> first, Expression> second) + { + return first.Compose(second, Expression.AndAlso); + } + /// + /// 组合Or + /// + /// + public static Expression> Or(this Expression> first, Expression> second) + { + return first.Compose(second, Expression.OrElse); + } + + /// + /// Combines the first expression with the second using the specified merge function. + /// + static Expression Compose(this Expression first, Expression second, Func merge) + { + var map = first.Parameters + .Select((f, i) => new { f, s = second.Parameters[i] }) + .ToDictionary(p => p.s, p => p.f); + var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body); + return Expression.Lambda(merge(first.Body, secondBody), first.Parameters); + } + + /// + /// ParameterRebinder + /// + private class ParameterRebinder : ExpressionVisitor + { + /// + /// The ParameterExpression map + /// + readonly Dictionary map; + /// + /// Initializes a new instance of the class. + /// + /// The map. + ParameterRebinder(Dictionary map) + { + this.map = map ?? new Dictionary(); + } + /// + /// Replaces the parameters. + /// + /// The map. + /// The exp. + /// Expression + public static Expression ReplaceParameters(Dictionary map, Expression exp) + { + return new ParameterRebinder(map).Visit(exp); + } + /// + /// Visits the parameter. + /// + /// The p. + /// Expression + protected override Expression VisitParameter(ParameterExpression p) + { + ParameterExpression replacement; + + if (map.TryGetValue(p, out replacement)) + { + p = replacement; + } + return base.VisitParameter(p); + } + } + } +} diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Validate.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Validate.cs new file mode 100644 index 00000000..d4031fad --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/Extension.Validate.cs @@ -0,0 +1,44 @@ +//using Microsoft.AspNetCore.Http; + +namespace Yi.Framework.Infrastructure.Extensions +{ + public static partial class Extensions + { + public static bool IsEmpty(this object value) + { + if (value != null && !string.IsNullOrEmpty(value.ParseToString())) + { + return false; + } + else + { + return true; + } + } + public static bool IsNotEmpty(this object value) + { + return !IsEmpty(value); + } + public static bool IsNullOrZero(this object value) + { + if (value == null || value.ParseToString().Trim() == "0") + { + return true; + } + else + { + return false; + } + } + + //public static bool IsAjaxRequest(this HttpRequest request) + //{ + // if (request == null) + // throw new ArgumentNullException("request"); + + // if (request.Headers != null) + // return request.Headers["X-Requested-With"] == "XMLHttpRequest"; + // return false; + //} + } +} diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/StringExtension.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/StringExtension.cs new file mode 100644 index 00000000..59cf9cdd --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Extensions/StringExtension.cs @@ -0,0 +1,232 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Yi.Framework.Infrastructure.Extensions +{ + public static class StringExtension + { + + /// + /// SQL条件拼接 + /// + /// + /// + /// + public static string If(this string str, bool condition) + { + return condition ? str : string.Empty; + } + /// + /// 判断是否为空 + /// + /// + /// + public static bool IfNotEmpty(this string str) + { + return !string.IsNullOrEmpty(str); + } + + /// + /// 注意:如果替换的旧值中有特殊符号,替换将会失败,解决办法 例如特殊符号是“(”: 要在调用本方法前加oldValue=oldValue.Replace("(","//("); + /// + /// + /// + /// + /// + public static string ReplaceFirst(this string input, string oldValue, string newValue) + { + Regex regEx = new Regex(oldValue, RegexOptions.Multiline); + return regEx.Replace(input, newValue == null ? "" : newValue, 1); + } + + /// + /// 骆驼峰转下划线 + /// + /// + /// + public static string ToSmallCamelCase(string name) + { + var stringBuilder = new StringBuilder(); + stringBuilder.Append(name.Substring(0, 1).ToLower()); + + for (var i = 0; i < name.Length; i++) + { + if (i == 0) + { + stringBuilder.Append(name.Substring(0, 1).ToLower()); + } + else + { + if (name[i] >= 'A' && name[i] <= 'Z') + { + stringBuilder.Append($"_{name.Substring(i, 1).ToLower()}"); + } + else + { + stringBuilder.Append(name[i]); + } + } + } + + return stringBuilder.ToString(); + } + + /// + /// 下划线命名转驼峰命名 + /// + /// + /// + public static string UnderScoreToCamelCase(this string underscore) + { + string[] ss = underscore.Split("_"); + if (ss.Length == 1) + { + return underscore; + } + + StringBuilder sb = new StringBuilder(); + sb.Append(ss[0]); + for (int i = 1; i < ss.Length; i++) + { + sb.Append(ss[i].FirstUpperCase()); + } + + return sb.ToString(); + } + + /// + /// 首字母转大写 + /// + /// + /// + public static string FirstUpperCase(this string str) + { + return string.IsNullOrEmpty(str) ? str : str.Substring(0, 1).ToUpper() + str[1..]; + } + + /// + /// 首字母转小写 + /// + /// + /// + public static string FirstLowerCase(this string str) + { + return string.IsNullOrEmpty(str) ? str : str.Substring(0, 1).ToLower() + str[1..]; + } + + /// + /// 截取指定字符串中间内容 + /// + /// + /// + /// + /// + public static string SubstringBetween(this string sourse, string startstr, string endstr) + { + string result = string.Empty; + int startindex, endindex; + try + { + startindex = sourse.IndexOf(startstr); + if (startindex == -1) + return result; + string tmpstr = sourse.Substring(startindex + startstr.Length); + endindex = tmpstr.IndexOf(endstr); + if (endindex == -1) + return result; + result = tmpstr.Remove(endindex); + } + catch (Exception ex) + { + Console.WriteLine("MidStrEx Err:" + ex.Message); + } + return result; + } + + /// + /// 转换为Pascal风格-每一个单词的首字母大写 + /// + /// 字段名 + /// 分隔符 + /// + public static string ConvertToPascal(this string fieldName, string fieldDelimiter) + { + string result = string.Empty; + if (fieldName.Contains(fieldDelimiter)) + { + //全部小写 + string[] array = fieldName.ToLower().Split(fieldDelimiter.ToCharArray()); + foreach (var t in array) + { + //首字母大写 + result += t.Substring(0, 1).ToUpper() + t[1..]; + } + } + else if (string.IsNullOrWhiteSpace(fieldName)) + { + result = fieldName; + } + else if (fieldName.Length == 1) + { + result = fieldName.ToUpper(); + } + else if (fieldName.Length == fieldName.CountUpper()) + { + result = fieldName.Substring(0, 1).ToUpper() + fieldName[1..].ToLower(); + } + else + { + result = fieldName.Substring(0, 1).ToUpper() + fieldName[1..]; + } + return result; + } + + /// + /// 大写字母个数 + /// + /// + /// + public static int CountUpper(this string str) + { + int count1 = 0; + char[] chars = str.ToCharArray(); + foreach (char num in chars) + { + if (num >= 'A' && num <= 'Z') + { + count1++; + } + //else if (num >= 'a' && num <= 'z') + //{ + // count2++; + //} + } + return count1; + } + + /// + /// 转换为Camel风格-第一个单词小写,其后每个单词首字母大写 + /// + /// 字段名 + /// 分隔符 + /// + public static string ConvertToCamel(this string fieldName, string fieldDelimiter) + { + //先Pascal + string result = fieldName.ConvertToPascal(fieldDelimiter); + //然后首字母小写 + if (result.Length == 1) + { + result = result.ToLower(); + } + else + { + result = result.Substring(0, 1).ToLower() + result[1..]; + } + + return result; + } + } +} diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Helper/ComputerHelper.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Helper/ComputerHelper.cs new file mode 100644 index 00000000..1ab82a26 --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Helper/ComputerHelper.cs @@ -0,0 +1,256 @@ +using System.Runtime.InteropServices; +using Newtonsoft.Json; +using Yi.Framework.Infrastructure.Extensions; + +namespace Yi.Framework.Infrastructure.Helper +{ + public class ComputerHelper + { + /// + /// 内存使用情况 + /// + /// + public static MemoryMetrics GetComputerInfo() + { + try + { + MemoryMetricsClient client = new(); + MemoryMetrics memoryMetrics = IsUnix() ? client.GetUnixMetrics() : client.GetWindowsMetrics(); + + memoryMetrics.FreeRam = Math.Round(memoryMetrics.Free / 1024, 2) + "GB"; + memoryMetrics.UsedRam = Math.Round(memoryMetrics.Used / 1024, 2) + "GB"; + memoryMetrics.TotalRAM = Math.Round(memoryMetrics.Total / 1024, 2) + "GB"; + memoryMetrics.RAMRate = Math.Ceiling(100 * memoryMetrics.Used / memoryMetrics.Total).ToString() + "%"; + memoryMetrics.CPURate = Math.Ceiling(GetCPURate().ParseToDouble()) + "%"; + return memoryMetrics; + } + catch (Exception ex) + { + Console.WriteLine("获取内存使用出错,msg=" + ex.Message + "," + ex.StackTrace); + } + return new MemoryMetrics(); + } + + /// + /// 获取内存大小 + /// + /// + public static List GetDiskInfos() + { + List diskInfos = new(); + + if (IsUnix()) + { + try + { + string output = ShellHelper.Bash("df -m / | awk '{print $2,$3,$4,$5,$6}'"); + var arr = output.Split('\n', StringSplitOptions.RemoveEmptyEntries); + if (arr.Length == 0) return diskInfos; + + var rootDisk = arr[1].Split(' ', (char)StringSplitOptions.RemoveEmptyEntries); + if (rootDisk == null || rootDisk.Length == 0) + { + return diskInfos; + } + DiskInfo diskInfo = new() + { + DiskName = "/", + TotalSize = long.Parse(rootDisk[0]) / 1024, + Used = long.Parse(rootDisk[1]) / 1024, + AvailableFreeSpace = long.Parse(rootDisk[2]) / 1024, + AvailablePercent = decimal.Parse(rootDisk[3].Replace("%", "")) + }; + diskInfos.Add(diskInfo); + } + catch (Exception ex) + { + Console.WriteLine("获取磁盘信息出错了" + ex.Message); + } + } + else + { + var driv = DriveInfo.GetDrives(); + foreach (var item in driv) + { + try + { + var obj = new DiskInfo() + { + DiskName = item.Name, + TypeName = item.DriveType.ToString(), + TotalSize = item.TotalSize / 1024 / 1024 / 1024, + AvailableFreeSpace = item.AvailableFreeSpace / 1024 / 1024 / 1024, + }; + obj.Used = obj.TotalSize - obj.AvailableFreeSpace; + obj.AvailablePercent = decimal.Ceiling(obj.Used / (decimal)obj.TotalSize * 100); + diskInfos.Add(obj); + } + catch (Exception ex) + { + Console.WriteLine("获取磁盘信息出错了" + ex.Message); + continue; + } + } + } + + return diskInfos; + } + + public static bool IsUnix() + { + var isUnix = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux); + return isUnix; + } + + public static string GetCPURate() + { + string cpuRate; + if (IsUnix()) + { + string output = ShellHelper.Bash("top -b -n1 | grep \"Cpu(s)\" | awk '{print $2 + $4}'"); + cpuRate = output.Trim(); + } + else + { + string output = ShellHelper.Cmd("wmic", "cpu get LoadPercentage"); + cpuRate = output.Replace("LoadPercentage", string.Empty).Trim(); + } + return cpuRate; + } + + /// + /// 获取系统运行时间 + /// + /// + public static string GetRunTime() + { + string runTime = string.Empty; + try + { + if (IsUnix()) + { + string output = ShellHelper.Bash("uptime -s").Trim(); + runTime = DateTimeHelper.FormatTime((DateTime.Now - output.ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong()); + } + else + { + string output = ShellHelper.Cmd("wmic", "OS get LastBootUpTime/Value"); + string[] outputArr = output.Split('=', (char)StringSplitOptions.RemoveEmptyEntries); + if (outputArr.Length == 2) + { + runTime = DateTimeHelper.FormatTime((DateTime.Now - outputArr[1].Split('.')[0].ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong()); + } + } + } + catch (Exception ex) + { + Console.WriteLine("获取runTime出错" + ex.Message); + } + return runTime; + } + } + + /// + /// 内存信息 + /// + public class MemoryMetrics + { + [JsonIgnore] + public double Total { get; set; } + [JsonIgnore] + public double Used { get; set; } + [JsonIgnore] + public double Free { get; set; } + + public string UsedRam { get; set; } + /// + /// CPU使用率% + /// + public string CPURate { get; set; } + /// + /// 总内存 GB + /// + public string TotalRAM { get; set; } + /// + /// 内存使用率 % + /// + public string RAMRate { get; set; } + /// + /// 空闲内存 + /// + public string FreeRam { get; set; } + } + + public class DiskInfo + { + /// + /// 磁盘名 + /// + public string DiskName { get; set; } + public string TypeName { get; set; } + public long TotalFree { get; set; } + public long TotalSize { get; set; } + /// + /// 已使用 + /// + public long Used { get; set; } + /// + /// 可使用 + /// + public long AvailableFreeSpace { get; set; } + public decimal AvailablePercent { get; set; } + } + + public class MemoryMetricsClient + { + #region 获取内存信息 + + /// + /// windows系统获取内存信息 + /// + /// + public MemoryMetrics GetWindowsMetrics() + { + string output = ShellHelper.Cmd("wmic", "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value"); + var metrics = new MemoryMetrics(); + var lines = output.Trim().Split('\n', (char)StringSplitOptions.RemoveEmptyEntries); + + if (lines.Length <= 0) return metrics; + + var freeMemoryParts = lines[0].Split('=', (char)StringSplitOptions.RemoveEmptyEntries); + var totalMemoryParts = lines[1].Split('=', (char)StringSplitOptions.RemoveEmptyEntries); + + metrics.Total = Math.Round(double.Parse(totalMemoryParts[1]) / 1024, 0); + metrics.Free = Math.Round(double.Parse(freeMemoryParts[1]) / 1024, 0);//m + metrics.Used = metrics.Total - metrics.Free; + + return metrics; + } + + /// + /// Unix系统获取 + /// + /// + public MemoryMetrics GetUnixMetrics() + { + string output = ShellHelper.Bash("free -m | awk '{print $2,$3,$4,$5,$6}'"); + var metrics = new MemoryMetrics(); + var lines = output.Split('\n', (char)StringSplitOptions.RemoveEmptyEntries); + + if (lines.Length <= 0) return metrics; + + if (lines != null && lines.Length > 0) + { + var memory = lines[1].Split(' ', (char)StringSplitOptions.RemoveEmptyEntries); + if (memory.Length >= 3) + { + metrics.Total = double.Parse(memory[0]); + metrics.Used = double.Parse(memory[1]); + metrics.Free = double.Parse(memory[2]);//m + } + } + return metrics; + } + #endregion + } +} \ No newline at end of file diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Helper/DateTimeHelper.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Helper/DateTimeHelper.cs new file mode 100644 index 00000000..206da822 --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Helper/DateTimeHelper.cs @@ -0,0 +1,139 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Yi.Framework.Infrastructure.Helper +{ + public class DateTimeHelper + { + /// + /// + /// + /// + /// + public static DateTime GetBeginTime(DateTime? dateTime, int days = 0) + { + if (dateTime == DateTime.MinValue || dateTime == null) + { + return DateTime.Now.AddDays(days); + } + return dateTime ?? DateTime.Now; + } + #region 时间戳转换 + + /// + /// 时间戳转本地时间-时间戳精确到秒 + /// + public static DateTime ToLocalTimeDateBySeconds(long unix) + { + var dto = DateTimeOffset.FromUnixTimeSeconds(unix); + return dto.ToLocalTime().DateTime; + } + + /// + /// 时间转时间戳Unix-时间戳精确到秒 + /// + public static long ToUnixTimestampBySeconds(DateTime dt) + { + DateTimeOffset dto = new DateTimeOffset(dt); + return dto.ToUnixTimeSeconds(); + } + + /// + /// 时间戳转本地时间-时间戳精确到毫秒 + /// + public static DateTime ToLocalTimeDateByMilliseconds(long unix) + { + var dto = DateTimeOffset.FromUnixTimeMilliseconds(unix); + return dto.ToLocalTime().DateTime; + } + + /// + /// 时间转时间戳Unix-时间戳精确到毫秒 + /// + public static long ToUnixTimestampByMilliseconds(DateTime dt) + { + DateTimeOffset dto = new DateTimeOffset(dt); + return dto.ToUnixTimeMilliseconds(); + } + + #endregion + + #region 毫秒转天时分秒 + /// + /// 毫秒转天时分秒 + /// + /// + /// + public static string FormatTime(long ms) + { + int ss = 1000; + int mi = ss * 60; + int hh = mi * 60; + int dd = hh * 24; + + long day = ms / dd; + long hour = (ms - day * dd) / hh; + long minute = (ms - day * dd - hour * hh) / mi; + long second = (ms - day * dd - hour * hh - minute * mi) / ss; + long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss; + + string sDay = day < 10 ? "0" + day : "" + day; //天 + string sHour = hour < 10 ? "0" + hour : "" + hour;//小时 + string sMinute = minute < 10 ? "0" + minute : "" + minute;//分钟 + string sSecond = second < 10 ? "0" + second : "" + second;//秒 + string sMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒 + sMilliSecond = milliSecond < 100 ? "0" + sMilliSecond : "" + sMilliSecond; + + return string.Format("{0} 天 {1} 小时 {2} 分 {3} 秒", sDay, sHour, sMinute, sSecond); + } + #endregion + + #region 获取unix时间戳 + /// + /// 获取unix时间戳 + /// + /// + /// + public static long GetUnixTimeStamp(DateTime dt) + { + long unixTime = ((DateTimeOffset)dt).ToUnixTimeMilliseconds(); + return unixTime; + } + #endregion + + #region 获取日期天的最小时间 + public static DateTime GetDayMinDate(DateTime dt) + { + DateTime min = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0); + return min; + } + #endregion + + #region 获取日期天的最大时间 + public static DateTime GetDayMaxDate(DateTime dt) + { + DateTime max = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59); + return max; + } + #endregion + + #region 获取日期天的最大时间 + public static string FormatDateTime(DateTime? dt) + { + if (dt != null) + { + if (dt.Value.Year == DateTime.Now.Year) + { + return dt.Value.ToString("MM-dd HH:mm"); + } + else + { + return dt.Value.ToString("yyyy-MM-dd HH:mm"); + } + } + return string.Empty; + } + #endregion + } +} diff --git a/Yi.Furion.Net6/Yi.Framework.Infrastructure/Helper/ShellHelper.cs b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Helper/ShellHelper.cs new file mode 100644 index 00000000..2c078810 --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Infrastructure/Helper/ShellHelper.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; + +namespace Yi.Framework.Infrastructure.Helper +{ + public class ShellHelper + { + /// + /// linux 系统命令 + /// + /// + /// + public static string Bash(string command) + { + var escapedArgs = command.Replace("\"", "\\\""); + var process = new Process() + { + StartInfo = new ProcessStartInfo + { + FileName = "/bin/bash", + Arguments = $"-c \"{escapedArgs}\"", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + } + }; + process.Start(); + string result = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + process.Dispose(); + return result; + } + + /// + /// windows系统命令 + /// + /// + /// + /// + public static string Cmd(string fileName, string args) + { + string output = string.Empty; + + var info = new ProcessStartInfo(); + info.FileName = fileName; + info.Arguments = args; + info.RedirectStandardOutput = true; + + using (var process = Process.Start(info)) + { + output = process.StandardOutput.ReadToEnd(); + } + return output; + } + } +} diff --git a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/IMonitorServerService.cs b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/IMonitorServerService.cs new file mode 100644 index 00000000..6d8608ca --- /dev/null +++ b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/IMonitorServerService.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Furion.Application.Rbac.Services +{ + public interface IMonitorServerService + { + } +} diff --git a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/MonitorServerService.cs b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/MonitorServerService.cs index 660a394d..07bbc82b 100644 --- a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/MonitorServerService.cs +++ b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/MonitorServerService.cs @@ -1,62 +1,64 @@ -//using System; -//using System.Collections.Generic; -//using System.Diagnostics; -//using System.Linq; -//using System.Runtime.InteropServices; -//using System.Text; -//using System.Threading.Tasks; -//using Furion.Logging; -//using Microsoft.AspNetCore.Hosting; -//using Microsoft.Extensions.Hosting; -//using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using Furion.Logging; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; +using Yi.Framework.Infrastructure.Extensions; +using Yi.Framework.Infrastructure.Helper; -//namespace Yi.Furion.Application.Rbac.Services -//{ +namespace Yi.Furion.Application.Rbac.Services +{ -// public class MonitorServerService -// { -// private OptionsSetting Options; -// private IWebHostEnvironment HostEnvironment; -// private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); -// public MonitorServerService(IOptions options, IWebHostEnvironment hostEnvironment) -// { -// this.HostEnvironment = hostEnvironment; -// this.Options = options.Value; -// } -// public void GGG() -// { -// int cpuNum = Environment.ProcessorCount; -// string computerName = Environment.MachineName; -// string osName = RuntimeInformation.OSDescription; -// string osArch = RuntimeInformation.OSArchitecture.ToString(); -// string version = RuntimeInformation.FrameworkDescription; -// string appRAM = ((double)Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + " MB"; -// string startTime = Process.GetCurrentProcess().StartTime.ToString("yyyy-MM-dd HH:mm:ss"); -// string sysRunTime = ComputerHelper.GetRunTime(); -// string serverIP = Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + ":" + Request.HttpContext.Connection.LocalPort;//获取服务器IP + public class MonitorServerService: IMonitorServerService,IDynamicApiController, ITransient + { + private IWebHostEnvironment _hostEnvironment; + private IHttpContextAccessor _httpContextAccessor; + public MonitorServerService(IWebHostEnvironment hostEnvironment, IHttpContextAccessor httpContextAccessor) + { + this._hostEnvironment = hostEnvironment; + _httpContextAccessor = httpContextAccessor; + } + [HttpGet("info")] + public object GetInfo() + { + int cpuNum = Environment.ProcessorCount; + string computerName = Environment.MachineName; + string osName = RuntimeInformation.OSDescription; + string osArch = RuntimeInformation.OSArchitecture.ToString(); + string version = RuntimeInformation.FrameworkDescription; + string appRAM = ((double)Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + " MB"; + string startTime = Process.GetCurrentProcess().StartTime.ToString("yyyy-MM-dd HH:mm:ss"); + string sysRunTime = ComputerHelper.GetRunTime(); + string serverIP = _httpContextAccessor.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + ":" + _httpContextAccessor.HttpContext.Connection.LocalPort;//获取服务器IP -// var programStartTime = Process.GetCurrentProcess().StartTime; -// string programRunTime = DateTimeHelper.FormatTime((DateTime.Now - programStartTime).TotalMilliseconds.ToString().Split('.')[0].ParseToLong()); -// var data = new -// { -// cpu = ComputerHelper.GetComputerInfo(), -// disk = ComputerHelper.GetDiskInfos(), -// sys = new { cpuNum, computerName, osName, osArch, serverIP, runTime = sysRunTime }, -// app = new -// { -// name = HostEnvironment.EnvironmentName, -// rootPath = HostEnvironment.ContentRootPath, -// webRootPath = HostEnvironment.WebRootPath, -// version, -// appRAM, -// startTime, -// runTime = programRunTime, -// host = serverIP -// }, -// }; + var programStartTime = Process.GetCurrentProcess().StartTime; + string programRunTime = DateTimeHelper.FormatTime((DateTime.Now - programStartTime).TotalMilliseconds.ToString().Split('.')[0].ParseToLong()); + var data = new + { + cpu = ComputerHelper.GetComputerInfo(), + disk = ComputerHelper.GetDiskInfos(), + sys = new { cpuNum, computerName, osName, osArch, serverIP, runTime = sysRunTime }, + app = new + { + name = _hostEnvironment.EnvironmentName, + rootPath = _hostEnvironment.ContentRootPath, + webRootPath = _hostEnvironment.WebRootPath, + version, + appRAM, + startTime, + runTime = programRunTime, + host = serverIP + }, + }; -// return SUCCESS(data); -// } + return data; + } -// } -//} + } +} diff --git a/Yi.Furion.Net6/Yi.Furion.Application/Yi.Furion.Application.xml b/Yi.Furion.Net6/Yi.Furion.Application/Yi.Furion.Application.xml index 9b15fe03..1fb18bfe 100644 --- a/Yi.Furion.Net6/Yi.Furion.Application/Yi.Furion.Application.xml +++ b/Yi.Furion.Net6/Yi.Furion.Application/Yi.Furion.Application.xml @@ -539,19 +539,5 @@ - - - 获取客户端信息 - - - - - - - 记录用户登陆信息 - - - - diff --git a/Yi.Furion.Net6/Yi.Furion.Core/Rbac/DataSeeds/MenuDataSeed.cs b/Yi.Furion.Net6/Yi.Furion.Core/Rbac/DataSeeds/MenuDataSeed.cs index dd98f701..7ccbe18f 100644 --- a/Yi.Furion.Net6/Yi.Furion.Core/Rbac/DataSeeds/MenuDataSeed.cs +++ b/Yi.Furion.Net6/Yi.Furion.Core/Rbac/DataSeeds/MenuDataSeed.cs @@ -91,7 +91,24 @@ namespace Yi.Furion.Core.Rbac.DataSeeds }; entities.Add(online); - + //服务监控 + MenuEntity server = new MenuEntity() + { + Id = SnowflakeHelper.NextId, + MenuName = "服务监控", + PermissionCode = "monitor:server:list", + MenuType = MenuTypeEnum.Menu, + Router = "server", + IsShow = true, + IsLink = false, + IsCache = true, + Component = "monitor/server/index", + MenuIcon = "server", + OrderNum = 98, + ParentId = monitoring.Id, + IsDeleted = false + }; + entities.Add(online); //系统工具 diff --git a/Yi.RuoYi.Vue3/src/api/monitor/server.js b/Yi.RuoYi.Vue3/src/api/monitor/server.js index e1f9ca21..2aafc089 100644 --- a/Yi.RuoYi.Vue3/src/api/monitor/server.js +++ b/Yi.RuoYi.Vue3/src/api/monitor/server.js @@ -3,7 +3,7 @@ import request from '@/utils/request' // 获取服务信息 export function getServer() { return request({ - url: '/monitor/server', + url: '/monitor-server/info', method: 'get' }) } \ No newline at end of file diff --git a/Yi.RuoYi.Vue3/src/views/monitor/server/index.vue b/Yi.RuoYi.Vue3/src/views/monitor/server/index.vue index 172443c8..91ccfc85 100644 --- a/Yi.RuoYi.Vue3/src/views/monitor/server/index.vue +++ b/Yi.RuoYi.Vue3/src/views/monitor/server/index.vue @@ -15,19 +15,19 @@
核心数
-
{{ server.cpu.cpuNum }}
+
{{ server.sys.cpuNum }}
-
用户使用率
-
{{ server.cpu.used }}%
+
总Cpu数
+
{{ server.cpu.total }}MB
系统使用率
-
{{ server.cpu.sys }}%
+
{{ Math.floor((server.cpu.used/server.cpu.total)*100)}}%
当前空闲率
-
{{ server.cpu.free }}%
+
{{ Math.floor((server.cpu.free/server.cpu.total)*100) }}%
@@ -44,29 +44,25 @@
属性
内存
-
JVM
总内存
-
{{ server.mem.total }}G
-
{{ server.jvm.total }}M
+
{{ server.cpu.totalRAM }}
已用内存
-
{{ server.mem.used}}G
-
{{ server.jvm.used}}M
+
{{ server.cpu.usedRam}}
剩余内存
-
{{ server.mem.free }}G
-
{{ server.jvm.free }}M
+
{{ server.cpu.freeRam }}
使用率
-
{{ server.mem.usage }}%
-
{{ server.jvm.usage }}%
+
{{ server.cpu.ramRate }}
+ @@ -88,7 +84,7 @@
服务器IP
-
{{ server.sys.computerIp }}
+
{{ server.sys.serverIP }}
系统架构
{{ server.sys.osArch }}
@@ -100,33 +96,33 @@ - +
- - - - + + + + - + - + - + - + - +
Java名称
{{ server.jvm.name }}
Java版本
{{ server.jvm.version }}
应用环境
{{ server.app.name }}
应用版本
{{ server.app.version }}
启动时间
{{ server.jvm.startTime }}
{{ server.app.startTime }}
运行时长
{{ server.jvm.runTime }}
{{ server.app.runTime }}
安装路径
{{ server.jvm.home }}
{{ server.app.rootPath }}
项目路径
{{ server.sys.userDir }}
{{ server.app.webRootPath }}
运行参数
{{ server.jvm.inputArgs }}
{{ server.app.name }}
@@ -142,7 +138,6 @@
盘符路径
-
文件系统
盘符类型
总大小
可用大小
@@ -150,15 +145,14 @@
已用百分比
- - -
{{ sysFile.dirName }}
-
{{ sysFile.sysTypeName }}
+ + +
{{ sysFile.diskName }}
{{ sysFile.typeName }}
-
{{ sysFile.total }}
-
{{ sysFile.free }}
+
{{ sysFile.totalSize }}
+
{{ sysFile.availableFreeSpace }}
{{ sysFile.used }}
-
{{ sysFile.usage }}%
+
{{ sysFile.availablePercent }}%