This commit is contained in:
2025-04-30 09:51:36 +08:00
parent 561881c9aa
commit 09afa239a6
848 changed files with 385870 additions and 205 deletions

View File

@@ -0,0 +1,11 @@
namespace Yes.Domain.Core.Extensions
{
public static class ArticleSlugExtensions
{
public static bool IsSlug(this string input)
{
return Regex.IsMatch(input, @"^[a-zA-Z0-9-_]+$");
}
}
}

View File

@@ -0,0 +1,52 @@

namespace Yes.Domain.Core.Extensions
{
public static class ArticleSummaryExtensions
{
public static string ToSummary(this string content)
{
// 去除 HTML 标签
string text = RemoveHtmlTags(content);
// 去除 Markdown 标签(简单处理,可能不全面)
text = Regex.Replace(text, @"\!\[.*?\]\(.*?\)|\[.*?\]\(.*?\)|\*\*.*?\*\*|\*.*?\*|~~.*?~~", "");
int endIndex = GetEndIndex(text, 50, 100);
if (endIndex < 50)
{
return text;
}
else
{
return text[..(endIndex + 1)];
}
}
private static string RemoveHtmlTags(string html)
{
return Regex.Replace(html, @"<[^>]*>", "");
}
private static int GetEndIndex(string text, int start, int end)
{
if (text.Length < start)
{
return -1;
}
end = Math.Min(end, text.Length - 1);
for (int i = end; i >= start; i--)
{
if (".。!?”".Contains(text[i]))
{
return i;
}
}
return end;
}
}
}

View File

@@ -0,0 +1,25 @@
namespace Yes.Domain.Core.Extensions
{
public static class ArticleTagExtensions
{
public static string ToTag(this string[] tags)
{
return string.Join(",", tags);
}
public static string ToTag(this List<string> tags)
{
return string.Join(",", tags);
}
public static List<string> ToTags(this string tags)
{
if (string.IsNullOrEmpty(tags))
return new List<string>();
return tags.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
}
}

View File

@@ -0,0 +1,15 @@
namespace Yes.Domain.Core.Extensions
{
public static class DateTimeExtensions
{
public static DateTime ToSafeTime(this DateTime dateTime)
{
var minDateTime = DateTime.Parse("2000-1-1");
if (dateTime.Subtract(minDateTime).TotalMinutes < 0)
{
return minDateTime;
}
return dateTime;
}
}
}

View File

@@ -0,0 +1,21 @@

namespace Yes.Domain.Core.Extensions
{
public static class HttpContextExtensions
{
public static string GetIpAddress(this HttpContext httpContext)
{
var ipAddress = httpContext.Connection.RemoteIpAddress?.ToString();
// 如果使用了代理服务器,可能需要从X-Forwarded-For头获取真实的IP地址
if (ipAddress == null || ipAddress == "::1") // "::1" 是IPv6的localhost
{
var xForwardedForHeader = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
if (!string.IsNullOrEmpty(xForwardedForHeader))
{
ipAddress = xForwardedForHeader.Split(",").FirstOrDefault()?.Trim() ?? "";
}
}
return ipAddress ?? "";
}
}
}

View File

@@ -0,0 +1,39 @@

namespace Yes.Domain.Core.Extensions
{
public static class NameValueCollectionExtensions
{
public static int QueryInt(this NameValueCollection collection, string key, int defaultValue = 0)
{
key = key.ToUpper();
var value = collection.QueryString(key);
if (string.IsNullOrEmpty(value))
{
return defaultValue;
}
if (int.TryParse(value, out int result))
{
return result;
}
return defaultValue;
}
public static string QueryString(this NameValueCollection collection, string key)
{
key = key.ToUpper();
if (collection.AllKeys.Contains(key))
{
try
{
return collection[key]?.ToString() ?? "";
}
catch
{
return "";
}
}
return "";
}
}
}

View File

@@ -0,0 +1,20 @@
namespace Yes.Domain.Core.Extensions
{
public static class PasswordExtensions
{
public static string ToMd5(this string input)
{
if (string.IsNullOrEmpty(input))
{
return "";
}
using (var md5 = MD5.Create())
{
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
var strResult = BitConverter.ToString(result);
return strResult.Replace("-", "");
}
}
}
}

View File

@@ -0,0 +1,13 @@
namespace Yes.Domain.Core.Extensions
{
public static class StringExtensions
{
public static bool IsUrlValid(this string url)
{
var urlRegex = new Regex(@"^(https?|http)://[^\s/$.?#].[^\s]*$", RegexOptions.IgnoreCase);
return !string.IsNullOrEmpty(url) && urlRegex.IsMatch(url);
}
}
}