提交 6ab69f5b 编写于 作者: lwplvx's avatar lwplvx

rename files

上级 db7aa644
using System;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using Aurora.Infrastructure.User;
using Aurora.Infrastructure.Tenant;
using Aurora.Common.User;
using Aurora.Common.Tenant;
namespace Aurora.Common
{
public static class CommonExtensions
{
public static IServiceCollection AddCommonService(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException("services");
}
//注入请求上下文
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IUsersCache, UsersCache>();
services.AddScoped<IUserContextProvider, UserContextProvider>();
services.AddScoped<ITenantContextProvider, TenantContextProvider>();
return services;
}
}
}
...@@ -18,7 +18,7 @@ namespace Aurora.Common ...@@ -18,7 +18,7 @@ namespace Aurora.Common
/// </summary> /// </summary>
public string TenantCode { get; set; } public string TenantCode { get; set; }
public ICurrentUser CurrentUser { get; set; } public IUserContext CurrentUser { get; set; }
public T RequestEntity { get; set; } public T RequestEntity { get; set; }
} }
......
...@@ -7,36 +7,33 @@ using Aurora.Infrastructure.Tenant; ...@@ -7,36 +7,33 @@ using Aurora.Infrastructure.Tenant;
namespace Aurora.Common.Tenant namespace Aurora.Common.Tenant
{ {
public class CurrentTenantProvider : ICurrentTenantProvider public class TenantContextProvider : ITenantContextProvider
{ {
private CurrentTenant currentTenant { get; set; } readonly IHttpContextAccessor _httpContextAccessor;
public TenantContextProvider(IHttpContextAccessor httpContextAccessor)
IHttpContextAccessor httpContextAccessor;
public CurrentTenantProvider(IHttpContextAccessor _httpContextAccessor)
{ {
httpContextAccessor = _httpContextAccessor; _httpContextAccessor = httpContextAccessor;
} }
public ICurrentTenant GetCurrentTenant() public ITenantContext GetTenantContext()
{ {
CurrentTenant _currentTenant = new CurrentTenant(); TenantContext tenantContext = new();
if (httpContextAccessor.HttpContext == null) //cap if (_httpContextAccessor.HttpContext == null) //cap
{ {
//todo //todo
_currentTenant = currentTenant;
} }
else if (httpContextAccessor.HttpContext.User != null && else if (_httpContextAccessor.HttpContext.User != null &&
httpContextAccessor.HttpContext.User.Identity.IsAuthenticated) //external _httpContextAccessor.HttpContext.User.Identity.IsAuthenticated) //external
{ {
ClaimsPrincipal claimsPrincipal = httpContextAccessor.HttpContext.User; ClaimsPrincipal claimsPrincipal = _httpContextAccessor.HttpContext.User;
var subject = claimsPrincipal.Claims.FirstOrDefault(w => w.Type == JwtClaimTypes.Role); var subject = claimsPrincipal.Claims.FirstOrDefault(w => w.Type == JwtClaimTypes.Role);
_currentTenant.TenantCode = subject.Value; tenantContext.TenantCode = subject.Value;
} }
else //internal else //internal
{ {
_currentTenant.TenantCode = httpContextAccessor.HttpContext.Request.Headers[ConstString.CurrentTenantCode].FirstOrDefault(); tenantContext.TenantCode = _httpContextAccessor.HttpContext.Request.Headers[ConstString.CurrentTenantCode].FirstOrDefault();
} }
return _currentTenant; return tenantContext;
} }
} }
......
...@@ -6,7 +6,7 @@ using Aurora.Infrastructure.Tenant; ...@@ -6,7 +6,7 @@ using Aurora.Infrastructure.Tenant;
namespace Aurora.Common.Tenant namespace Aurora.Common.Tenant
{ {
public class CurrentTenant : ICurrentTenant public class TenantContext : ITenantContext
{ {
public string TenantCode { get; set; } public string TenantCode { get; set; }
} }
......
...@@ -13,7 +13,7 @@ using Aurora.Common.Permission; ...@@ -13,7 +13,7 @@ using Aurora.Common.Permission;
namespace Aurora.Common.User namespace Aurora.Common.User
{ {
public class CurrentUser : ICurrentUser public class UserContext : IUserContext
{ {
/// <summary> /// <summary>
/// 用户登录用户Code /// 用户登录用户Code
......
...@@ -11,28 +11,28 @@ using Aurora.Infrastructure.User; ...@@ -11,28 +11,28 @@ using Aurora.Infrastructure.User;
namespace Aurora.Common.User namespace Aurora.Common.User
{ {
public class CurrentUserProvider : ICurrentUserProvider public class UserContextProvider : IUserContextProvider
{ {
IHttpContextAccessor httpContextAccessor; readonly IHttpContextAccessor _httpContextAccessor;
IConfiguration _configuration; readonly IUsersCache _usersCache;
public CurrentUserProvider(IHttpContextAccessor _httpContextAccessor, IConfiguration configuration) public UserContextProvider(IHttpContextAccessor httpContextAccessor, IUsersCache usersCache)
{ {
httpContextAccessor = _httpContextAccessor; _httpContextAccessor = httpContextAccessor;
_configuration = configuration; _usersCache = usersCache;
} }
public ICurrentUser GetCurrentUser() public IUserContext GetUserContext()
{ {
ICurrentUser currentUser = new CurrentUser(); IUserContext currentUser = new UserContext();
if (httpContextAccessor.HttpContext == null) //cap if (_httpContextAccessor.HttpContext == null) //cap
{ {
currentUser.UserCode = "cap"; currentUser.UserCode = "cap";
currentUser.UserName = "cap"; currentUser.UserName = "cap";
} }
else if (httpContextAccessor.HttpContext.User != null && else if (_httpContextAccessor.HttpContext.User != null &&
httpContextAccessor.HttpContext.User.Identity.IsAuthenticated) //external _httpContextAccessor.HttpContext.User.Identity.IsAuthenticated) //external
{ {
ClaimsPrincipal claimsPrincipal = httpContextAccessor.HttpContext.User; ClaimsPrincipal claimsPrincipal = _httpContextAccessor.HttpContext.User;
var subject = claimsPrincipal.Claims.FirstOrDefault(w => w.Type == JwtClaimTypes.Subject); var subject = claimsPrincipal.Claims.FirstOrDefault(w => w.Type == JwtClaimTypes.Subject);
var subjectRole = claimsPrincipal.Claims.FirstOrDefault(w => w.Type == JwtClaimTypes.Role); var subjectRole = claimsPrincipal.Claims.FirstOrDefault(w => w.Type == JwtClaimTypes.Role);
...@@ -46,10 +46,10 @@ namespace Aurora.Common.User ...@@ -46,10 +46,10 @@ namespace Aurora.Common.User
return currentUser; return currentUser;
} }
private ICurrentUser GetCurrentUserFromRedis(string usercode, string tenantCode) private IUserContext GetCurrentUserFromRedis(string usercode, string tenantCode)
{ {
// var redisUserInfo = new CacheRedis(RedisDBType.User, _configuration).GetJSON<CurrentUserInfo>($"{tenantCode}:{usercode}"); // var redisUserInfo = new CacheRedis(RedisDBType.User, _configuration).GetJSON<CurrentUserInfo>($"{tenantCode}:{usercode}");
return new CurrentUser(); return new UserContext();
// return redisUserInfo; // return redisUserInfo;
} }
} }
......
using Aurora.Infrastructure.User;
namespace Aurora.Common.User
{
public class UsersCache:IUsersCache
{
}
}
\ No newline at end of file
...@@ -7,9 +7,9 @@ namespace Aurora.Infrastructure.Response ...@@ -7,9 +7,9 @@ namespace Aurora.Infrastructure.Response
{ {
public class ResponseModel<T> public class ResponseModel<T>
{ {
ErrorCodes ErrorCode { get; set; } public ErrorCodes ErrorCode { get; set; }
T Data { get; set; } public T Data { get; set; }
public ResponseModel(T data) public ResponseModel(T data)
{ {
......
...@@ -2,7 +2,7 @@ using System; ...@@ -2,7 +2,7 @@ using System;
namespace Aurora.Infrastructure.Tenant namespace Aurora.Infrastructure.Tenant
{ {
public interface ICurrentTenant public interface ITenantContext
{ {
string TenantCode { get; set; } string TenantCode { get; set; }
......
...@@ -2,9 +2,9 @@ using System; ...@@ -2,9 +2,9 @@ using System;
namespace Aurora.Infrastructure.Tenant namespace Aurora.Infrastructure.Tenant
{ {
public interface ICurrentTenantProvider public interface ITenantContextProvider
{ {
ICurrentTenant GetCurrentTenant(); ITenantContext GetTenantContext();
} }
} }
...@@ -5,7 +5,7 @@ using System.Collections.Generic; ...@@ -5,7 +5,7 @@ using System.Collections.Generic;
namespace Aurora.Infrastructure.User namespace Aurora.Infrastructure.User
{ {
public interface ICurrentUser public interface IUserContext
{ {
/// <summary> /// <summary>
/// 用户登录用户Code /// 用户登录用户Code
......
...@@ -2,8 +2,8 @@ using System; ...@@ -2,8 +2,8 @@ using System;
namespace Aurora.Infrastructure.User namespace Aurora.Infrastructure.User
{ {
public interface ICurrentUserProvider public interface IUserContextProvider
{ {
ICurrentUser GetCurrentUser(); IUserContext GetUserContext();
} }
} }
namespace Aurora.Infrastructure.User
{
public interface IUsersCache
{
}
}
\ No newline at end of file
...@@ -25,11 +25,6 @@ namespace Aurora.Core.Api ...@@ -25,11 +25,6 @@ namespace Aurora.Core.Api
services.AddAutoMapper(Assembly.Load("Aurora.Core.Service")); services.AddAutoMapper(Assembly.Load("Aurora.Core.Service"));
//注入请求上下文
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<ICurrentUserProvider, CurrentUserProvider>();
services.AddScoped<ICurrentTenantProvider, CurrentTenantProvider>();
services.AddDbContext<ApplicationDbContext>(option => services.AddDbContext<ApplicationDbContext>(option =>
option.UseMySQL(configuration["ConnectionStrings:MySql"])); option.UseMySQL(configuration["ConnectionStrings:MySql"]));
services.AddDbContext<ApplicationReadonlyDbContext>(option => services.AddDbContext<ApplicationReadonlyDbContext>(option =>
......
...@@ -15,6 +15,7 @@ using Microsoft.OpenApi.Models; ...@@ -15,6 +15,7 @@ using Microsoft.OpenApi.Models;
using System.IO; using System.IO;
using Aurora.Core.EntityFramework; using Aurora.Core.EntityFramework;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Aurora.Common;
namespace Aurora.Core.Api namespace Aurora.Core.Api
{ {
...@@ -36,9 +37,10 @@ namespace Aurora.Core.Api ...@@ -36,9 +37,10 @@ namespace Aurora.Core.Api
{ {
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Aurora.Core.Api", Version = "v1" }); c.SwaggerDoc("v1", new OpenApiInfo { Title = "Aurora.Core.Api", Version = "v1" });
}); });
services.AddModuleCoreApi(Configuration);
services.AddCommonService();
services.AddModuleCoreApi(Configuration);
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
......
...@@ -5,6 +5,7 @@ namespace Aurora.Core.Domain.Entities ...@@ -5,6 +5,7 @@ namespace Aurora.Core.Domain.Entities
{ {
public class Category : TenantEntityBase public class Category : TenantEntityBase
{ {
public int ParentId { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Remark { get; set; } public string Remark { get; set; }
} }
......
using System; using System;
using Aurora.Common.Entities;
namespace Aurora.Common.Entities namespace Aurora.Domain.Entities
{ {
public class EntityBase:IEntity public class EntityBase:IEntity
{ {
......
using System; using System;
using Aurora.Common.Entities;
namespace Aurora.Common.Entities namespace Aurora.Domain.Entities
{ {
public class TenantEntityBase : EntityBase, ITenantEntity public class TenantEntityBase : EntityBase, ITenantEntity
{ {
......
...@@ -10,6 +10,7 @@ using System.Threading; ...@@ -10,6 +10,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Aurora.Common.Entities; using Aurora.Common.Entities;
using Aurora.Core.Domain.Entities; using Aurora.Core.Domain.Entities;
using Aurora.Domain.Entities;
using Aurora.Infrastructure.Tenant; using Aurora.Infrastructure.Tenant;
using Aurora.Infrastructure.User; using Aurora.Infrastructure.User;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
...@@ -18,16 +19,15 @@ namespace Aurora.Core.EntityFramework ...@@ -18,16 +19,15 @@ namespace Aurora.Core.EntityFramework
{ {
public class ApplicationDbContext : DbContext public class ApplicationDbContext : DbContext
{ {
private readonly ICurrentUser currentUser; private readonly IUserContext currentUser;
private readonly ICurrentTenant currentTenant; private readonly ITenantContext currentTenant;
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options
, IUserContextProvider currentUserProvider,
ITenantContextProvider currentTenantProvider) : base(options)
{ {
// , currentUser = currentUserProvider.GetUserContext();
// ICurrentUserProvider currentUserProvider, currentTenant = currentTenantProvider.GetTenantContext();
// ICurrentTenantProvider currentTenantProvider
// currentUser = currentUserProvider.GetCurrentUser();
// currentTenant = currentTenantProvider.GetCurrentTenant();
} }
public DbSet<Category> Categories { get; set; } public DbSet<Category> Categories { get; set; }
...@@ -46,17 +46,19 @@ namespace Aurora.Core.EntityFramework ...@@ -46,17 +46,19 @@ namespace Aurora.Core.EntityFramework
// 配置表名映射 // 配置表名映射
builder.Entity<Category>().ToTable("category"); builder.Entity<Category>().ToTable("category");
base.OnModelCreating(builder); base.OnModelCreating(builder);
} }
private static IList<Type> _baseEntityTypesCache; private static IList<Type> _baseEntityTypesCache;
private static IList<Type> GetBaseEntityTypes() private static IList<Type> GetBaseEntityTypes()
{ {
if (_baseEntityTypesCache != null) if (_baseEntityTypesCache != null)
return _baseEntityTypesCache.ToList(); return _baseEntityTypesCache.ToList();
_baseEntityTypesCache = (from t in typeof(TenantEntityBase).GetTypeInfo().Assembly.DefinedTypes var assembly = typeof(TenantEntityBase).GetTypeInfo().Assembly;
// var assembly=Assembly.Load("Aurora.Core.Domain");
_baseEntityTypesCache = (from t in assembly.DefinedTypes
where t.BaseType == typeof(TenantEntityBase) where t.BaseType == typeof(TenantEntityBase)
select t.AsType()).ToList(); select t.AsType()).ToList();
return _baseEntityTypesCache; return _baseEntityTypesCache;
......
...@@ -10,6 +10,7 @@ using System.Threading; ...@@ -10,6 +10,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Aurora.Common.Entities; using Aurora.Common.Entities;
using Aurora.Core.Domain.Entities; using Aurora.Core.Domain.Entities;
using Aurora.Domain.Entities;
using Aurora.Infrastructure.Tenant; using Aurora.Infrastructure.Tenant;
using Aurora.Infrastructure.User; using Aurora.Infrastructure.User;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
...@@ -18,16 +19,12 @@ namespace Aurora.Core.EntityFramework ...@@ -18,16 +19,12 @@ namespace Aurora.Core.EntityFramework
{ {
public class ApplicationReadonlyDbContext : DbContext public class ApplicationReadonlyDbContext : DbContext
{ {
private readonly ICurrentUser currentUser; private readonly ITenantContext currentTenant;
private readonly ICurrentTenant currentTenant;
public ApplicationReadonlyDbContext(DbContextOptions<ApplicationReadonlyDbContext> options) : base(options) public ApplicationReadonlyDbContext(DbContextOptions<ApplicationReadonlyDbContext> options
, ITenantContextProvider currentTenantProvider) : base(options)
{ {
// , currentTenant = currentTenantProvider.GetTenantContext();
// ICurrentUserProvider currentUserProvider,
// ICurrentTenantProvider currentTenantProvider
// currentUser = currentUserProvider.GetCurrentUser();
// currentTenant = currentTenantProvider.GetCurrentTenant();
} }
public DbSet<Category> Categories { get; set; } public DbSet<Category> Categories { get; set; }
...@@ -56,12 +53,14 @@ namespace Aurora.Core.EntityFramework ...@@ -56,12 +53,14 @@ namespace Aurora.Core.EntityFramework
{ {
if (_baseEntityTypesCache != null) if (_baseEntityTypesCache != null)
return _baseEntityTypesCache.ToList(); return _baseEntityTypesCache.ToList();
_baseEntityTypesCache = (from t in typeof(TenantEntityBase).GetTypeInfo().Assembly.DefinedTypes var assembly = typeof(TenantEntityBase).GetTypeInfo().Assembly;
// var assembly=Assembly.Load("Aurora.Core.Domain");
_baseEntityTypesCache = (from t in assembly.DefinedTypes
where t.BaseType == typeof(TenantEntityBase) where t.BaseType == typeof(TenantEntityBase)
select t.AsType()).ToList(); select t.AsType()).ToList();
return _baseEntityTypesCache; return _baseEntityTypesCache;
} }
protected static readonly MethodInfo SetGlobalQueryMethod = typeof(ApplicationDbContext).GetMethods(BindingFlags.Public | BindingFlags.Instance) protected static readonly MethodInfo SetGlobalQueryMethod = typeof(ApplicationReadonlyDbContext).GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Single(t => t.IsGenericMethod && t.Name == "SetGlobalQuery"); .Single(t => t.IsGenericMethod && t.Name == "SetGlobalQuery");
public void SetGlobalQuery<T>(ModelBuilder builder) where T : TenantEntityBase public void SetGlobalQuery<T>(ModelBuilder builder) where T : TenantEntityBase
......
# 记录一些想法和思考
## 接口日志如何实现
可以记录操作路径,参数,业务类型,业务描述的
* 后端接口过滤器
* 统一的日志中间件(程序中的中间件,或者 类似网关的日志中间件)
* 前端埋点 ?
...@@ -23,6 +23,7 @@ SET FOREIGN_KEY_CHECKS = 0; ...@@ -23,6 +23,7 @@ SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `category`; DROP TABLE IF EXISTS `category`;
CREATE TABLE `category` ( CREATE TABLE `category` (
`Id` int NOT NULL AUTO_INCREMENT, `Id` int NOT NULL AUTO_INCREMENT,
`parentId` int NULL,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`remark` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `remark` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`createTime` datetime NOT NULL, `createTime` datetime NOT NULL,
...@@ -34,3 +35,4 @@ CREATE TABLE `category` ( ...@@ -34,3 +35,4 @@ CREATE TABLE `category` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SET FOREIGN_KEY_CHECKS = 1; SET FOREIGN_KEY_CHECKS = 1;
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册