first commit

This commit is contained in:
qianhao.xu
2024-09-23 09:14:22 +08:00
commit fdbe20be5a
407 changed files with 35117 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
using Infrastructure.Attribute;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Reflection;
namespace Infrastructure
{
/// <summary>
/// App服务注册
/// </summary>
public static class AppServiceExtensions
{
/// <summary>
/// 注册引用程序域中所有有AppService标记的类的服务
/// </summary>
/// <param name="services"></param>
public static void AddAppService(this IServiceCollection services)
{
var cls = AppSettings.Get<string[]>("InjectClass");
if (cls == null || cls.Length <= 0)
{
throw new Exception("请更新appsettings类");
}
foreach (var item in cls)
{
Register(services, item);
}
}
private static void Register(IServiceCollection services, string item)
{
Assembly assembly = Assembly.Load(item);
foreach (var type in assembly.GetTypes())
{
var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>();
if (serviceAttribute != null)
{
var serviceType = serviceAttribute.ServiceType;
//情况1 适用于依赖抽象编程,注意这里只获取第一个
if (serviceType == null && serviceAttribute.InterfaceServiceType)
{
serviceType = type.GetInterfaces().FirstOrDefault();
}
//情况2 不常见特殊情况下才会指定ServiceType写起来麻烦
if (serviceType == null)
{
serviceType = type;
}
switch (serviceAttribute.ServiceLifetime)
{
case LifeTime.Singleton:
services.AddSingleton(serviceType, type);
break;
case LifeTime.Scoped:
services.AddScoped(serviceType, type);
break;
case LifeTime.Transient:
services.AddTransient(serviceType, type);
break;
default:
services.AddTransient(serviceType, type);
break;
}
//System.Console.WriteLine($"注册:{serviceType}");
}
}
}
}
}

View File

@@ -0,0 +1,34 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace Infrastructure
{
/// <summary>
/// 跨域扩展
/// </summary>
public static class CorsExtension
{
/// <summary>
/// 跨域配置
/// </summary>
/// <param name="services"></param>
/// <param name="configuration"></param>
public static void AddCors(this IServiceCollection services, IConfiguration configuration)
{
var corsUrls = configuration.GetSection("corsUrls").Get<string[]>();
//配置跨域
services.AddCors(c =>
{
c.AddPolicy("Policy", policy =>
{
policy.WithOrigins(corsUrls ?? Array.Empty<string>())
.AllowAnyHeader()//允许任意头
.AllowCredentials()//允许cookie
.AllowAnyMethod();//允许任意方法
});
});
}
}
}

View File

@@ -0,0 +1,42 @@

using Infrastructure.Extensions;
using Microsoft.AspNetCore.Http;
using System;
using System.Reflection;
namespace Infrastructure
{
public static class EntityExtension
{
public static TSource ToCreate<TSource>(this TSource source, HttpContext? context = null)
{
var types = source?.GetType();
if (types == null || context == null) return source;
BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
types.GetProperty("CreateTime", flag)?.SetValue(source, DateTime.Now, null);
types.GetProperty("AddTime", flag)?.SetValue(source, DateTime.Now, null);
types.GetProperty("CreateBy", flag)?.SetValue(source, context.GetName(), null);
types.GetProperty("Create_by", flag)?.SetValue(source, context.GetName(), null);
//types.GetProperty("UserId", flag)?.SetValue(source, context.GetUId(), null);
types.GetProperty("DeptId", flag)?.SetValue(source, context.GetDeptId(), null);
return source;
}
public static TSource ToUpdate<TSource>(this TSource source, HttpContext? context = null)
{
var types = source?.GetType();
if (types == null || context == null) return source;
BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
types.GetProperty("UpdateTime", flag)?.SetValue(source, DateTime.Now, null);
types.GetProperty("Update_time", flag)?.SetValue(source, DateTime.Now, null);
types.GetProperty("UpdateBy", flag)?.SetValue(source, context.GetName(), null);
types.GetProperty("Update_by", flag)?.SetValue(source, context.GetName(), null);
return source;
}
}
}

View File

@@ -0,0 +1,244 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Text.RegularExpressions;
using UAParser;
using DOAN.Infrastructure.IPTools;
namespace Infrastructure.Extensions
{
/// <summary>
/// HttpContext扩展类
/// </summary>
public static partial class HttpContextExtension
{
/// <summary>
/// 是否是ajax请求
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
//return request.Headers.ContainsKey("X-Requested-With") &&
// request.Headers["X-Requested-With"].Equals("XMLHttpRequest");
return request.Headers["X-Requested-With"] == "XMLHttpRequest" || request.Headers != null && request.Headers["X-Requested-With"] == "XMLHttpRequest";
}
/// <summary>
/// 获取客户端IP
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static string GetClientUserIp(this HttpContext context)
{
if (context == null) return "";
var result = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
if (string.IsNullOrEmpty(result))
{
result = context.Connection.RemoteIpAddress?.ToString();
}
if (string.IsNullOrEmpty(result))
throw new Exception("获取IP失败");
if (result.Contains("::1"))
result = "127.0.0.1";
result = result.Replace("::ffff:", "");
result = result.Split(':')?.FirstOrDefault() ?? "127.0.0.1";
result = IsIP(result) ? result : "127.0.0.1";
return result;
}
/// <summary>
/// 判断是否IP
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool IsIP(string ip)
{
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
/// <summary>
/// 获取登录用户id
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static long GetUId(this HttpContext context)
{
var uid = context.User.FindFirstValue(ClaimTypes.PrimarySid);
return !string.IsNullOrEmpty(uid) ? long.Parse(uid) : 0;
}
/// <summary>
/// 获取部门id
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static long GetDeptId(this HttpContext context)
{
var deptId = context.User.FindFirstValue(ClaimTypes.GroupSid);
return !string.IsNullOrEmpty(deptId) ? long.Parse(deptId) : 0;
}
/// <summary>
/// 获取登录用户名
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static string GetName(this HttpContext context)
{
var uid = context.User?.Identity?.Name;
return uid;
}
/// <summary>
/// 判断是否是管理员
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static bool IsAdmin(this HttpContext context)
{
var userName = context.GetName();
return userName == GlobalConstant.AdminRole;
}
/// <summary>
/// ClaimsIdentity
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static IEnumerable<ClaimsIdentity> GetClaims(this HttpContext context)
{
return context.User?.Identities;
}
//public static int GetRole(this HttpContext context)
//{
// var roleid = context.User.FindFirstValue(ClaimTypes.Role) ?? "0";
// return int.Parse(roleid);
//}
public static string GetUserAgent(this HttpContext context)
{
return context.Request.Headers["User-Agent"];
}
/// <summary>
/// 获取请求令牌
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static string GetToken(this HttpContext context)
{
return context.Request.Headers["Authorization"];
}
/// <summary>
/// 获取请求Url
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static string GetRequestUrl(this HttpContext context)
{
return context != null ? context.Request.Path.Value : "";
}
/// <summary>
/// 获取请求参数
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static string GetQueryString(this HttpContext context)
{
return context != null ? context.Request.QueryString.Value : "";
}
/// <summary>
/// 获取body请求参数
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static string GetBody(this HttpContext context)
{
context.Request.EnableBuffering();
//context.Request.Body.Seek(0, SeekOrigin.Begin);
//using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
////需要使用异步方式才能获取
//return reader.ReadToEndAsync().Result;
string body = string.Empty;
var buffer = new MemoryStream();
context.Request.Body.Seek(0, SeekOrigin.Begin);
context.Request.Body.CopyToAsync(buffer);
buffer.Position = 0;
try
{
using StreamReader streamReader = new(buffer, Encoding.UTF8);
body = streamReader.ReadToEndAsync().Result;
}
finally
{
buffer?.Dispose();
}
return body;
}
/// <summary>
/// 获取浏览器信息
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static ClientInfo GetClientInfo(this HttpContext context)
{
var str = context.GetUserAgent();
var uaParser = Parser.GetDefault();
ClientInfo c = uaParser.Parse(str);
return c;
}
/// <summary>
/// 根据IP获取地理位置
/// </summary>
/// <returns></returns>
public static string GetIpInfo(string IP)
{
var ipInfo = IpTool.Search(IP);
return ipInfo?.Province + "-" + ipInfo?.City + "-" + ipInfo?.NetworkOperator;
}
/// <summary>
/// 设置请求参数
/// </summary>
/// <param name="reqMethod"></param>
/// <param name="context"></param>
public static string GetRequestValue(this HttpContext context, string reqMethod)
{
string param = string.Empty;
if (HttpMethods.IsPost(reqMethod) || HttpMethods.IsPut(reqMethod) || HttpMethods.IsDelete(reqMethod))
{
param = context.GetBody();
string regex = "(?<=\"password\":\")[^\",]*";
param = Regex.Replace(param, regex, "***");
}
if (param.IsEmpty())
{
param = context.GetQueryString();
}
return param;
}
}
}

View File

@@ -0,0 +1,33 @@
using AspNetCoreRateLimit;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace DOAN.Infrastructure.WebExtensions
{
public static class IPRateExtension
{
public static void AddIPRate(this IServiceCollection services, IConfiguration configuration)
{
ArgumentNullException.ThrowIfNull(services);
//从appsettings.json中加载常规配置IpRateLimiting与配置文件中节点对应
services.Configure<IpRateLimitOptions>(configuration.GetSection("IpRateLimiting"));
//从appsettings.json中加载Ip规则
services.Configure<IpRateLimitPolicies>(configuration.GetSection("IpRateLimitPolicies"));
//注入计数器和规则存储
services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
//配置(解析器、计数器密钥生成器)
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
services.AddRateLimiter(limiterOptions =>
{
// 配置限流策略
});
}
}
}

View File

@@ -0,0 +1,39 @@
using Infrastructure;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Threading.Tasks;
namespace DOAN.Infrastructure.WebExtensions
{
public static class JwtExtension
{
public static void AddJwt(this IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddCookie()
.AddJwtBearer(o =>
{
o.TokenValidationParameters = JwtUtil.ValidParameters();
o.Events = new JwtBearerEvents
{
OnAuthenticationFailed = context =>
{
// 如果过期,把过期信息添加到头部
if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
{
Console.WriteLine("jwt过期了");
context.Response.Headers.Add("Token-Expired", "true");
}
return Task.CompletedTask;
},
};
});
}
}
}

View File

@@ -0,0 +1,26 @@
using Infrastructure.Helper;
using JinianNet.JNTemplate;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace Infrastructure
{
public static class LogoExtension
{
public static void AddLogo(this IServiceCollection services)
{
Console.ForegroundColor = ConsoleColor.Blue;
var contentTpl = JnHelper.ReadTemplate("", "logo.txt");
var content = contentTpl?.Render();
var url = AppSettings.GetConfig("urls");
Console.WriteLine(content);
Console.ForegroundColor = ConsoleColor.Blue;
//Console.WriteLine("🎉源码地址: https://gitee.com/izory/DOANAdminNetCore");
Console.WriteLine("📖官方文档http://www.izhaorui.cn");
Console.WriteLine("💰打赏作者http://www.izhaorui.cn/vip");
Console.WriteLine("📱移动端体验http://demo.izhaorui.cn/h5");
Console.WriteLine($"Swagger地址{url}/swagger/index.html");
Console.WriteLine($"初始化种子数据地址:{url}/common/InitSeedData");
}
}
}

View File

@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace DOAN.Infrastructure.WebExtensions
{
public static class RequestLimitExtension
{
/// <summary>
/// 请求body大小设置
/// </summary>
/// <param name="services"></param>
/// <param name="configuration"></param>
public static void AddRequestLimit(this IServiceCollection services, IConfiguration configuration)
{
var sizeM = configuration.GetSection("upload:requestLimitSize").Get<int>();
services.Configure<FormOptions>(x =>
{
x.MultipartBodyLengthLimit = sizeM * 1024 * 1024;
x.MemoryBufferThreshold = sizeM * 1024 * 1024;
x.ValueLengthLimit = int.MaxValue;
});
services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = sizeM * 1024 * 1024;
});
services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = sizeM * 1024 * 1024; // 设置最大请求体大小为500MB
});
}
}
}