diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseCrudController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseCrudController.cs index a47868d3..e63c36a6 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseCrudController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/BaseCrudController.cs @@ -2,6 +2,7 @@ using Yi.Framework.Common.Models; using Yi.Framework.Model.Query; using Yi.Framework.Repository; +using Yi.Framework.WebCore.AttributeExtend; namespace Yi.Framework.ApiMicroservice.Controllers { @@ -10,38 +11,45 @@ namespace Yi.Framework.ApiMicroservice.Controllers public class BaseCrudController : ControllerBase where T : class,new() { private readonly ILogger _logger; - public IRepository _iRepository; public BaseCrudController(ILogger logger, IRepository iRepository) { _logger = logger; _iRepository = iRepository; } - + [Permission($"{nameof(T)}:Get:One")] [HttpGet] - public async Task Get() + public async Task Get(object id) + { + return Result.Success().SetData(await _iRepository.GetByIdAsync(id)); + } + [Permission($"{nameof(T)}:Get:List")] + [HttpGet] + public async Task GetList() { return Result.Success().SetData(await _iRepository.GetListAsync()); } - + [Permission($"{nameof(T)}:Get:Page")] [HttpPost] - public async Task Page(QueryCondition queryCondition) + public async Task Page(QueryCondition queryCondition) { - return Result.Success().SetData(_iRepository.CommonPage(queryCondition)); + return Result.Success().SetData(await _iRepository.CommonPage(queryCondition)); } - + [Permission($"{nameof(T)}:Add")] [HttpPost] public async Task Add(T entity) { return Result.Success().SetData(await _iRepository.InsertReturnEntityAsync(entity)); } + [Permission($"{nameof(T)}:Update")] [HttpPut] public async Task Update(T entity) { return Result.Success().SetStatus(await _iRepository.UpdateAsync(entity)); } + [Permission($"{nameof(T)}:Delete:List")] [HttpDelete] - public async Task Delete(object[] ids) + public async Task DeleteList(object[] ids) { return Result.Success().SetStatus(await _iRepository.DeleteByIdsAsync(ids)); } diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs index b601f1a9..6882a8b4 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs @@ -10,6 +10,7 @@ using Yi.Framework.Interface; using Yi.Framework.Model.Models; using Yi.Framework.Repository; using Yi.Framework.WebCore; +using Yi.Framework.WebCore.AttributeExtend; using Yi.Framework.WebCore.AuthorizationPolicy; namespace Yi.Framework.ApiMicroservice.Controllers @@ -20,12 +21,13 @@ namespace Yi.Framework.ApiMicroservice.Controllers { public UserController(ILogger logger, IUserService iUserService) : base(logger, iUserService) { - } [HttpGet] - public async Task Test() + [Permission("user:query:list")] + public async Task PermissionTest() { - return Ok(await _iRepository.GetListAsync()); + return Result.Success().SetData( await _iRepository.GetListAsync()); + } } } diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs index ce979087..e84864cb 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs @@ -7,6 +7,7 @@ using Autofac; using Yi.Framework.Common.Models; using Yi.Framework.Language; using Microsoft.Extensions.Localization; +using Yi.Framework.WebCore.AttributeExtend; var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddCommandLine(args); @@ -48,6 +49,10 @@ builder.Host.ConfigureLogging(loggingBuilder => #endregion builder.Services.AddIocService(builder.Configuration); #region +//Sqlsugar上下文注入 +#endregion +builder.Services.AddSqlsugarServer(); +#region //Quartz任务调度配置 #endregion builder.Services.AddQuartzService(); @@ -55,9 +60,13 @@ builder.Services.AddQuartzService(); //控制器+过滤器配置 #endregion builder.Services.AddControllers(optios => { - //optios.Filters.Add(typeof(CustomExceptionFilterAttribute)); + //optios.Filters.Add(); }).AddJsonFileService(); #region +//权限过滤器 +#endregion +builder.Services.AddSingleton(); +#region //Swagger服务配置 #endregion builder.Services.AddSwaggerService(); @@ -92,8 +101,7 @@ builder.Services.AddSMSService(); #region //CAP服务配置 #endregion -builder.Services.AddCAPService(); - +builder.Services.AddCAPService(); #region //国际化配置 #endregion @@ -119,17 +127,15 @@ ServiceLocator.Instance = app.Services; #region //错误抓取反馈注入 #endregion -//app.UseErrorHandlingService(); +app.UseErrorHandlingService(); #region //静态文件注入 #endregion -//app.UseStaticFiles(); - +app.UseStaticFiles(); #region //多语言国际化注入 #endregion app.UseLocalizerService(); - #region //HttpsRedirection注入 #endregion @@ -161,7 +167,7 @@ app.UseConsulService(); #region //redis种子注入 #endregion -app.UseRedisSeedInitService(app.Services.GetService()); +app.UseRedisSeedInitService(); #region //Endpoints注入 #endregion diff --git a/Yi.Framework.Net6/Yi.Framework.Common/Models/PageModel.cs b/Yi.Framework.Net6/Yi.Framework.Common/Models/PageModel.cs index 491f1c7e..513b83c6 100644 --- a/Yi.Framework.Net6/Yi.Framework.Common/Models/PageModel.cs +++ b/Yi.Framework.Net6/Yi.Framework.Common/Models/PageModel.cs @@ -6,8 +6,14 @@ using System.Threading.Tasks; namespace Yi.Framework.Common.Models { - public class PageModel - { + public class PageModel + { + public int Total { get; set; } + public T Data { get; set; } + } + + public class PageModel : PageModel + { } } diff --git a/Yi.Framework.Net6/Yi.Framework.Repository/DataContext.cs b/Yi.Framework.Net6/Yi.Framework.Repository/DataContext.cs index 1f01d075..f4ad2611 100644 --- a/Yi.Framework.Net6/Yi.Framework.Repository/DataContext.cs +++ b/Yi.Framework.Net6/Yi.Framework.Repository/DataContext.cs @@ -5,68 +5,11 @@ namespace Yi.Framework.Repository { public class DataContext : SimpleClient where T : class, new() { - public DataContext(ISqlSugarClient context = null!) : base(context) + public DataContext(ISqlSugarClient context) : base(context) { - if (context == null) - { - base.Context = Db; - } + Db =base.Context; } - /// - /// SqlSugarScope鎿嶄綔鏁版嵁搴撴槸绾跨▼瀹夌殑鍙互鍗曚緥 - /// - public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig() - { - DbType = SqlSugar.DbType.MySql, - //ConnectionString = Appsettings.app("ConnectionStrings", "mysqlConnection"), - ConnectionString= "server=119.91.207.67;port=3306;database=yi-sqlsugar-dev;user id=root;password=Qz52013142020.", - IsAutoCloseConnection = true - }, - db => - { - - db.Aop.DataExecuting = (oldValue, entityInfo) => - { - //var httpcontext = ServiceLocator.Instance.GetService().HttpContext; - switch (entityInfo.OperationType) - { - case DataFilterType.InsertByObject: - if (entityInfo.PropertyName == "CreateUser") - { - //entityInfo.SetValue(new Guid(httpcontext.Request.Headers["Id"].ToString())); - } - - if (entityInfo.PropertyName == "TenantId") - { - //鐜板湪涓嶈兘鐩存帴缁欎簡锛岃鏍规嵁鍒ゆ柇涓涓嬬鎴风瓑绾э紝濡傛灉绉熸埛绛夌骇鏄1锛屼笉缁欙紝闇瑕佽嚜宸卞幓璧嬪硷紝濡傛灉绉熸埛绛夌骇鏄0锛屽氨鎵ц涓嬮潰鐨勩 - //entityInfo.SetValue(new Guid(httpcontext.Request.Headers["TenantId"].ToString())); - //鏌ヨ鐨勬椂鍊欙紝涔熼渶瑕佸垽鏂竴涓嬶紝濡傛灉鏄鎴风瓑绾э紝涓嶈绉熸埛鏉′欢锛屽鏋滄槸瓒呯骇绉熸埛锛屽氨杩斿洖鎵鏈 - } - break; - case DataFilterType.UpdateByObject: - if (entityInfo.PropertyName == "ModifyTime") - { - entityInfo.SetValue(DateTime.Now); - } - if (entityInfo.PropertyName == "ModifyUser") - { - //entityInfo.SetValue(new Guid(httpcontext.Request.Headers["Id"].ToString())); - } - break; - } - //inset鐢熸晥 - - }; - //濡傛灉鐢ㄥ崟渚嬮厤缃缁熶竴鍐欏湪杩欏効 - db.Aop.OnLogExecuting = (s, p) => - { - - Console.WriteLine("_______________________________________________"); - Console.WriteLine(s); - }; - - }); - - + + public ISqlSugarClient Db; } } diff --git a/Yi.Framework.Net6/Yi.Framework.Repository/IRepository.cs b/Yi.Framework.Net6/Yi.Framework.Repository/IRepository.cs index 8d7c1107..61ea7e37 100644 --- a/Yi.Framework.Net6/Yi.Framework.Repository/IRepository.cs +++ b/Yi.Framework.Net6/Yi.Framework.Repository/IRepository.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; +using Yi.Framework.Common.Models; using Yi.Framework.Model.Query; namespace Yi.Framework.Repository @@ -13,6 +14,6 @@ namespace Yi.Framework.Repository { public Task InsertReturnEntityAsync(T entity); public Task> StoreAsync(string storeName, object para); - public object CommonPage(QueryCondition queryCondition); + public Task>> CommonPage(QueryCondition pars); } } diff --git a/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs b/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs index 39f4c57f..5237b7a4 100644 --- a/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs +++ b/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs @@ -1,6 +1,7 @@ 锘縰sing SqlSugar; using System.Data; using System.Linq.Expressions; +using Yi.Framework.Common.Models; using Yi.Framework.Model.Query; /***杩欓噷闈㈠啓鐨勪唬鐮佷笉浼氱粰瑕嗙洊,濡傛灉瑕侀噸鏂扮敓鎴愯鍒犻櫎 Repository.cs ***/ @@ -17,12 +18,8 @@ namespace Yi.Framework.Repository /// 鏋勯犲嚱鏁 /// /// - public Repository(ISqlSugarClient context = null) : base(context)//娉ㄦ剰杩欓噷瑕佹湁榛樿鍊肩瓑浜巒ull + public Repository(ISqlSugarClient context) : base(context)//娉ㄦ剰杩欓噷瑕佹湁榛樿鍊肩瓑浜巒ull { - if (context == null) - { - base.Context = Db; - } } @@ -52,9 +49,9 @@ namespace Yi.Framework.Repository /// 浠撳偍鎵╁睍鏂规硶:鍗曡〃鏌ヨ閫氱敤鍒嗛〉 /// /// - public object CommonPage(QueryCondition pars) + public async Task>> CommonPage(QueryCondition pars) { - int tolCount = 0; + RefAsync tolCount = 0; var sugarParamters = pars.Parameters.Select(it => (IConditionalModel)new ConditionalModel() { ConditionalType = it.ConditionalType, @@ -66,14 +63,15 @@ namespace Yi.Framework.Repository { foreach (var item in pars.OrderBys) { - query.OrderBy(item.ToSqlFilter());//鏍煎紡 id asc鎴栬 id desc + query.OrderBy(item.ToSqlFilter()); } } - var result = query.Where(sugarParamters).ToPageList(pars.Index, pars.Size, ref tolCount); - return new + var result =await query.Where(sugarParamters).ToPageListAsync(pars.Index, pars.Size, tolCount); + + return new PageModel> { - count = tolCount, - data = result + Total = tolCount.Value, + Data = result }; } } diff --git a/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs b/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs index 661e4155..27b14913 100644 --- a/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs +++ b/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs @@ -1,11 +1,14 @@ -锘縰sing Yi.Framework.Interface; +锘縰sing SqlSugar; +using Yi.Framework.Interface; using Yi.Framework.Model.Models; using Yi.Framework.Repository; namespace Yi.Framework.Service { - public partial class UserService + public partial class UserService { - + public UserService(ISqlSugarClient context) : base(context) + { + } } } diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/AttributeExtend/PermissionAttribute.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/AttributeExtend/PermissionAttribute.cs new file mode 100644 index 00000000..e2a053ec --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/AttributeExtend/PermissionAttribute.cs @@ -0,0 +1,51 @@ +锘縰sing Microsoft.AspNetCore.Mvc.Filters; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Yi.Framework.WebCore.AttributeExtend +{ + [AttributeUsage(AttributeTargets.Method)] + public class PermissionAttribute : ActionFilterAttribute + { + private string permission { get; set; } + + public PermissionAttribute(string permission) + { + this.permission = permission; + } + + /// + /// 鍔ㄤ綔閴存潈 + /// + /// + /// + public override void OnActionExecuting(ActionExecutingContext context) + { + base.OnActionExecuting(context); + + if (string.IsNullOrEmpty(permission)) + { + throw new Exception("鏉冮檺涓嶈兘涓虹┖锛"); + } + + //鍙互浠嶳edis寰楀埌鐢ㄦ埛鑿滃崟鍒楄〃锛屾垨鑰呯洿鎺ヤ粠jwt涓幏鍙 + + var result = false; + + //鍒ゆ柇鏉冮檺鏄惁瀛樺湪Redis涓 + if (permission.Length>0) + { + result = true; + } + + + if (!result) + { + throw new Exception("鎷︽埅鏈巿鏉冭姹傦紒"); + } + } + + } +} \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/CAPExtend.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/CAPExtend.cs index b7014130..80869150 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/CAPExtend.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/CAPExtend.cs @@ -12,7 +12,7 @@ namespace Yi.Framework.WebCore.MiddlewareExtend { public static class CAPExtend { - public static IServiceCollection AddCAPService(this IServiceCollection services) + public static IServiceCollection AddCAPService(this IServiceCollection services) { if (Appsettings.appBool("CAP_Enabled")) { @@ -31,9 +31,9 @@ namespace Yi.Framework.WebCore.MiddlewareExtend x.FailedRetryInterval = 60;//second x.FailedThresholdCallback = failed => { - var logger = failed.ServiceProvider.GetService>(); - logger.LogError($@"MessageType {failed.MessageType} 澶辫触浜嗭紝 閲嶈瘯浜 {x.FailedRetryCount} 娆, - 娑堟伅鍚嶇О: {failed.Message.GetName()}");//do anything + //var logger = failed.ServiceProvider.GetService>(); + //logger.LogError($@"MessageType {failed.MessageType} 澶辫触浜嗭紝 閲嶈瘯浜 {x.FailedRetryCount} 娆, + //娑堟伅鍚嶇О: {failed.Message.GetName()}");//do anything }; if (Appsettings.appBool("CAPDashboard_Enabled")) { diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/RedisInitExtend.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/RedisInitExtend.cs index 63b603a6..4a0c02b6 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/RedisInitExtend.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/RedisInitExtend.cs @@ -1,11 +1,13 @@ 锘縰sing log4net; using Microsoft.AspNetCore.Builder; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Yi.Framework.Common.Models; using Yi.Framework.Core; namespace Yi.Framework.WebCore.MiddlewareExtend @@ -13,12 +15,12 @@ namespace Yi.Framework.WebCore.MiddlewareExtend public static class RedisInitExtend { private static readonly ILog log = LogManager.GetLogger(typeof(RedisInitExtend)); - public static void UseRedisSeedInitService(this IApplicationBuilder app, CacheClientDB _cacheClientDB) + public static void UseRedisSeedInitService(this IApplicationBuilder app ) { if (Appsettings.appBool("RedisSeed_Enabled")) { - if (app == null) throw new ArgumentNullException(nameof(app)); + var _cacheClientDB = ServiceLocator.Instance.GetService(); try { diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/SqlsugarExtension.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/SqlsugarExtension.cs new file mode 100644 index 00000000..b2437811 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/SqlsugarExtension.cs @@ -0,0 +1,72 @@ +锘縰sing Microsoft.Extensions.DependencyInjection; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.WebCore.MiddlewareExtend +{ + public static class SqlsugarExtension + { + public static void AddSqlsugarServer(this IServiceCollection services) + { + SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig() + { + DbType = SqlSugar.DbType.MySql, + ConnectionString = Appsettings.app("DbConn", "WriteUrl"), + IsAutoCloseConnection = true + }, + db => + { + + db.Aop.DataExecuting = (oldValue, entityInfo) => + { + //var httpcontext = ServiceLocator.Instance.GetService().HttpContext; + switch (entityInfo.OperationType) + { + case DataFilterType.InsertByObject: + if (entityInfo.PropertyName == "CreateUser") + { + //entityInfo.SetValue(new Guid(httpcontext.Request.Headers["Id"].ToString())); + } + + if (entityInfo.PropertyName == "TenantId") + { + //鐜板湪涓嶈兘鐩存帴缁欎簡锛岃鏍规嵁鍒ゆ柇涓涓嬬鎴风瓑绾э紝濡傛灉绉熸埛绛夌骇鏄1锛屼笉缁欙紝闇瑕佽嚜宸卞幓璧嬪硷紝濡傛灉绉熸埛绛夌骇鏄0锛屽氨鎵ц涓嬮潰鐨勩 + //entityInfo.SetValue(new Guid(httpcontext.Request.Headers["TenantId"].ToString())); + //鏌ヨ鐨勬椂鍊欙紝涔熼渶瑕佸垽鏂竴涓嬶紝濡傛灉鏄鎴风瓑绾э紝涓嶈绉熸埛鏉′欢锛屽鏋滄槸瓒呯骇绉熸埛锛屽氨杩斿洖鎵鏈 + } + break; + case DataFilterType.UpdateByObject: + if (entityInfo.PropertyName == "ModifyTime") + { + entityInfo.SetValue(DateTime.Now); + } + if (entityInfo.PropertyName == "ModifyUser") + { + //entityInfo.SetValue(new Guid(httpcontext.Request.Headers["Id"].ToString())); + } + break; + } + //inset鐢熸晥 + + }; + //濡傛灉鐢ㄥ崟渚嬮厤缃缁熶竴鍐欏湪杩欏効 + db.Aop.OnLogExecuting = (s, p) => + { + + Console.WriteLine("_______________________________________________"); + Console.WriteLine(s); + }; + + }); + services.AddSingleton(sqlSugar);//杩欒竟鏄疭qlSugarScope鐢ˋddSingleton + + + + } + } + +} \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj b/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj index 47494c31..bc373a9f 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj @@ -4,6 +4,12 @@ net6.0 + + + + + + @@ -32,8 +38,4 @@ - - - -