diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/SettingController.cs b/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/SettingController.cs index f30ecc46..a4a0945f 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/SettingController.cs +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/Controllers/SettingController.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Threading.Tasks; using Yi.Framework.Common.Const; using Yi.Framework.Common.Models; +using Yi.Framework.Core; using Yi.Framework.DTOModel; using Yi.Framework.Interface; using Yi.Framework.Model.Models; @@ -20,10 +21,12 @@ namespace Yi.Framework.ApiMicroservice.Controllers public class SettingController : ControllerBase { private readonly ILogger _logger; + private readonly CacheClientDB _cacheClientDB; - public SettingController(ILogger logger) + public SettingController(ILogger logger, CacheClientDB cacheClientDB) { _logger = logger; + _cacheClientDB = cacheClientDB; } @@ -33,21 +36,20 @@ namespace Yi.Framework.ApiMicroservice.Controllers /// /// [HttpGet] - public async Task GetSetting() - { - var p = RedisConst.stringData[RedisConst.ImageList_key]; - return Result.Success(); + public Result GetSetting() + { + return Result.Success().SetData( _cacheClientDB.Get(RedisConst.key)); } /// /// 更 /// - /// + /// /// [HttpPut] - public async Task UpdateSetting() + public Result UpdateSetting(SettingDto settingDto) { - + _cacheClientDB.Set(RedisConst.key, settingDto); return Result.Success(); } diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/Startup.cs b/Yi.Framework/Yi.Framework.ApiMicroservice/Startup.cs index 9d31f9d3..2129958d 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/Startup.cs +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/Startup.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Yi.Framework.Core; using Yi.Framework.Model.ModelFactory; using Yi.Framework.WebCore.MiddlewareExtend; using Yi.Framework.WebCore.Utility; @@ -83,7 +84,7 @@ namespace Yi.Framework.ApiMicroservice #endregion // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env,IDbContextFactory _DbFactory) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env,IDbContextFactory _DbFactory, CacheClientDB _cacheClientDB) { //if (env.IsDevelopment()) { @@ -141,7 +142,7 @@ namespace Yi.Framework.ApiMicroservice //ݿע #endregion app.UseDbSeedInitService(_DbFactory); - + app.UseRedisInitService(_cacheClientDB); #region //Endpointsע #endregion diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json b/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json index 159ff0ad..8beca74e 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json @@ -15,6 +15,7 @@ "Cors_Enabled": true, "RabbitMQ_Enabled": false, "Redis_Enabled": false, + "RedisSet_Enabled": false, "Kafka_Enabled": false, "MutiDB_Enabled": false, "DbList": [ "Sqlite", "Mysql", "Sqlserver", "Oracle" ], @@ -40,10 +41,10 @@ "SecurityKey": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDI2a2EJ7m872v0afyoSDJT2o1+SitIeJSWtLJU8/Wz2m7gStexajkeD+Lka6DSTy8gt9UwfgVQo6uKjVLG5Ex7PiGOODVqAEghBuS7JzIYU5RvI543nNDAPfnJsas96mSA7L/mD7RTE2drj6hf3oZjJpMPZUQI/B1Qjb5H3K3PNwIDAQAB" }, "RedisConn": { - "Host": "192.168.2.128", + "Host": "118.195.191.41", "Prot": 6379, "DB": 0, - "Password": "123456" + "Password": "Qz52013142020." }, "KafkaOptions": { "BrokerList": "192.168.3.230:9092", diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/evn/wwwroot/Excel/Excel_20211102225436.xlsx b/Yi.Framework/Yi.Framework.ApiMicroservice/evn/wwwroot/Excel/Excel_20211102225436.xlsx deleted file mode 100644 index 24bc5816..00000000 Binary files a/Yi.Framework/Yi.Framework.ApiMicroservice/evn/wwwroot/Excel/Excel_20211102225436.xlsx and /dev/null differ diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/evn/wwwroot/Excel/Excel_20211102225553.xlsx b/Yi.Framework/Yi.Framework.ApiMicroservice/evn/wwwroot/Excel/Excel_20211102225553.xlsx deleted file mode 100644 index adf47f29..00000000 Binary files a/Yi.Framework/Yi.Framework.ApiMicroservice/evn/wwwroot/Excel/Excel_20211102225553.xlsx and /dev/null differ diff --git a/Yi.Framework/Yi.Framework.Common/Const/RedisConst.cs b/Yi.Framework/Yi.Framework.Common/Const/RedisConst.cs index 3a22a248..6b99c95e 100644 --- a/Yi.Framework/Yi.Framework.Common/Const/RedisConst.cs +++ b/Yi.Framework/Yi.Framework.Common/Const/RedisConst.cs @@ -11,32 +11,32 @@ namespace Yi.Framework.Common.Const /// /// 前缀 /// - private const string prefix = "Yi:"; + public const string key = "YiFramework:data"; - /// - /// 初始化角色名 - /// - public const string InitRole_key = nameof(InitRole_key); + ///// + ///// 初始化角色名 + ///// + //public const string InitRole_key = nameof(InitRole_key); - /// - /// 标题名 - /// - public const string Title_key = nameof(Title_key); + ///// + ///// 标题名 + ///// + //public const string Title_key = nameof(Title_key); - /// - /// 图片列表名 - /// - public const string ImageList_key = nameof(ImageList_key); + ///// + ///// 图片列表名 + ///// + //public const string ImageList_key = nameof(ImageList_key); - public static Dictionary stringData = new Dictionary() - { - {prefix+nameof(InitRole_key), "普通用户"}, - {prefix+nameof(Title_key), "YiFramework"}, - }; + //public static Dictionary stringData = new Dictionary() + //{ + // {prefix+nameof(InitRole_key), "普通用户"}, + // {prefix+nameof(Title_key), "YiFramework"}, + //}; - public static Dictionary> listData = new Dictionary>() - { - {prefix+nameof(ImageList_key), new List(){"图片地址1", "图片地址2", "图片地址3", "图片地址4" } } - }; + //public static Dictionary> listData = new Dictionary>() + //{ + // {prefix+nameof(ImageList_key), new List(){"图片地址1", "图片地址2", "图片地址3", "图片地址4" } } + //}; } } diff --git a/Yi.Framework/Yi.Framework.DTOModel/SettingDto.cs b/Yi.Framework/Yi.Framework.DTOModel/SettingDto.cs index a327a37e..0b8df4b4 100644 --- a/Yi.Framework/Yi.Framework.DTOModel/SettingDto.cs +++ b/Yi.Framework/Yi.Framework.DTOModel/SettingDto.cs @@ -8,5 +8,9 @@ namespace Yi.Framework.DTOModel { public class SettingDto { + public string InitIcon_key { get; set; } + public string InitRole_key { get; set; } + public string Title_key { get; set; } + public List ImageList_key { get; set; } } } diff --git a/Yi.Framework/Yi.Framework.WebCore/Excel.cs b/Yi.Framework/Yi.Framework.WebCore/Excel.cs deleted file mode 100644 index a7961b87..00000000 --- a/Yi.Framework/Yi.Framework.WebCore/Excel.cs +++ /dev/null @@ -1,228 +0,0 @@ -using NPOI.SS.UserModel; -using NPOI.XSSF.UserModel; -using System; -using System.Collections.Generic; -using System.Data; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; - -namespace Yi.Framework.WebCore -{ - public class Excel - { - /// - /// 读取Excel多Sheet数据 - /// - /// 文件路径 - /// Sheet名 - /// - public static DataSet ReadExcelToDataSet(string filePath, string sheetName = null) - { - if (!File.Exists(filePath)) - { - //LogUtil.Debug($"未找到文件{filePath}"); - return null; - } - //获取文件信息 - FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); - IWorkbook workbook = WorkbookFactory.Create(fs); - //获取sheet信息 - ISheet sheet = null; - DataSet ds = new DataSet(); - if (!string.IsNullOrEmpty(sheetName)) - { - sheet = workbook.GetSheet(sheetName); - if (sheet == null) - { - //LogUtil.Debug($"{filePath}未找到sheet:{sheetName}"); - return null; - } - DataTable dt = ReadExcelFunc(workbook, sheet); - ds.Tables.Add(dt); - } - else - { - //遍历获取所有数据 - int sheetCount = workbook.NumberOfSheets; - for (int i = 0; i < sheetCount; i++) - { - sheet = workbook.GetSheetAt(i); - if (sheet != null) - { - DataTable dt = ReadExcelFunc(workbook, sheet); - ds.Tables.Add(dt); - } - } - } - return ds; - } - - /// - /// 读取Excel信息 - /// - /// 工作区 - /// sheet - /// - private static DataTable ReadExcelFunc(IWorkbook workbook, ISheet sheet) - { - DataTable dt = new DataTable(); - //获取列信息 - IRow cells = sheet.GetRow(sheet.FirstRowNum); - int cellsCount = cells.PhysicalNumberOfCells; - int emptyCount = 0; - int cellIndex = sheet.FirstRowNum; - List listColumns = new List(); - bool isFindColumn = false; - while (!isFindColumn) - { - emptyCount = 0; - listColumns.Clear(); - for (int i = 0; i < cellsCount; i++) - { - if (string.IsNullOrEmpty(cells.GetCell(i).StringCellValue)) - { - emptyCount++; - } - listColumns.Add(cells.GetCell(i).StringCellValue); - } - //这里根据逻辑需要,空列超过多少判断 - if (emptyCount == 0) - { - isFindColumn = true; - } - cellIndex++; - cells = sheet.GetRow(cellIndex); - } - - foreach (string columnName in listColumns) - { - if (dt.Columns.Contains(columnName)) - { - //如果允许有重复列名,自己做处理 - continue; - } - dt.Columns.Add(columnName, typeof(string)); - } - //开始获取数据 - int rowsCount = sheet.PhysicalNumberOfRows; - cellIndex += 1; - DataRow dr = null; - for (int i = cellIndex; i < rowsCount; i++) - { - cells = sheet.GetRow(i); - dr = dt.NewRow(); - for (int j = 0; j < dt.Columns.Count; j++) - { - //这里可以判断数据类型 - switch (cells.GetCell(j).CellType) - { - case CellType.String: - dr[j] = cells.GetCell(j).StringCellValue; - break; - case CellType.Numeric: - dr[j] = cells.GetCell(j).NumericCellValue.ToString(); - break; - case CellType.Unknown: - dr[j] = cells.GetCell(j).StringCellValue; - break; - } - } - dt.Rows.Add(dr); - } - return dt; - } - - /// - /// 导出Excel文件 - /// - /// 数据类型 - /// 数据实体 - /// 列对应关系,如Name->姓名 - /// 标题 - /// - public static byte[] ExportExcel(List entities, Dictionary dicColumns, string title = null) - { - if (entities.Count <= 0) - { - return null; - } - //HSSFWorkbook => xls - //XSSFWorkbook => xlsx - IWorkbook workbook = new XSSFWorkbook(); - ISheet sheet = workbook.CreateSheet("test");//名称自定义 - IRow cellsColumn = null; - IRow cellsData = null; - //获取实体属性名 - PropertyInfo[] properties = entities[0].GetType().GetProperties(); - int cellsIndex = 0; - //标题 - if (!string.IsNullOrEmpty(title)) - { - ICellStyle style = workbook.CreateCellStyle(); - //边框 - style.BorderBottom = BorderStyle.Dotted; - style.BorderLeft = BorderStyle.Hair; - style.BorderRight = BorderStyle.Hair; - style.BorderTop = BorderStyle.Dotted; - //水平对齐 - style.Alignment = HorizontalAlignment.Left; - - //垂直对齐 - style.VerticalAlignment = VerticalAlignment.Center; - - //设置字体 - IFont font = workbook.CreateFont(); - font.FontHeightInPoints = 10; - font.FontName = "微软雅黑"; - style.SetFont(font); - - IRow cellsTitle = sheet.CreateRow(0); - cellsTitle.CreateCell(0).SetCellValue(title); - cellsTitle.RowStyle = style; - //合并单元格 - sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 1, 0, dicColumns.Count - 1)); - cellsIndex = 2; - } - //列名 - cellsColumn = sheet.CreateRow(cellsIndex); - int index = 0; - Dictionary columns = new Dictionary(); - foreach (var item in dicColumns) - { - cellsColumn.CreateCell(index).SetCellValue(item.Value); - columns.Add(item.Value, index); - index++; - } - cellsIndex += 1; - //数据 - foreach (var item in entities) - { - cellsData = sheet.CreateRow(cellsIndex); - for (int i = 0; i < properties.Length; i++) - { - if (!dicColumns.ContainsKey(properties[i].Name)) continue; - //这里可以也根据数据类型做不同的赋值,也可以根据不同的格式参考上面的ICellStyle设置不同的样式 - object[] entityValues = new object[properties.Length]; - entityValues[i] = properties[i].GetValue(item); - //获取对应列下标 - index = columns[dicColumns[properties[i].Name]]; - cellsData.CreateCell(index).SetCellValue(entityValues[i].ToString()); - } - cellsIndex++; - } - - byte[] buffer = null; - using (MemoryStream ms = new MemoryStream()) - { - workbook.Write(ms); - buffer = ms.GetBuffer(); - ms.Close(); - } - - return buffer; - } - } -} diff --git a/Yi.Framework/Yi.Framework.Model/DbInit/DataSeed.cs b/Yi.Framework/Yi.Framework.WebCore/Init/DataSeed.cs similarity index 99% rename from Yi.Framework/Yi.Framework.Model/DbInit/DataSeed.cs rename to Yi.Framework/Yi.Framework.WebCore/Init/DataSeed.cs index 76f79513..4cd8ea76 100644 --- a/Yi.Framework/Yi.Framework.Model/DbInit/DataSeed.cs +++ b/Yi.Framework/Yi.Framework.WebCore/Init/DataSeed.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; using Yi.Framework.Model.ModelFactory; using Yi.Framework.Model.Models; -namespace Yi.Framework.Model.DbInit +namespace Yi.Framework.WebCore.Init { public class DataSeed { diff --git a/Yi.Framework/Yi.Framework.WebCore/Init/RedisInit.cs b/Yi.Framework/Yi.Framework.WebCore/Init/RedisInit.cs new file mode 100644 index 00000000..25888d21 --- /dev/null +++ b/Yi.Framework/Yi.Framework.WebCore/Init/RedisInit.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Common.Const; +using Yi.Framework.Core; +using Yi.Framework.DTOModel; + +namespace Yi.Framework.WebCore.Init +{ + public class RedisInit + { + public static void Seed(CacheClientDB _cacheClientDB) + { + + if (_cacheClientDB.Get(RedisConst.key)==null) + { + _cacheClientDB.Add(RedisConst.key, new SettingDto() + { + ImageList_key = { "默认图片", "默认图片" } , + InitRole_key= "默认角色", + Title_key= "默认标题", + InitIcon_key= "默认头像" + }) ; + } + + Console.WriteLine(nameof(RedisInit) + ":Redis初始成功!"); + } + } +} diff --git a/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/DbSeedInitExtend.cs b/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/DbSeedInitExtend.cs index 8d8d2d82..d43e2913 100644 --- a/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/DbSeedInitExtend.cs +++ b/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/DbSeedInitExtend.cs @@ -6,8 +6,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using Yi.Framework.Model.DbInit; using Yi.Framework.Model.ModelFactory; +using Yi.Framework.WebCore.Init; namespace Yi.Framework.WebCore.MiddlewareExtend { diff --git a/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/RedisExtension.cs b/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/RedisExtension.cs index 6a9826ce..f17beb8a 100644 --- a/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/RedisExtension.cs +++ b/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/RedisExtension.cs @@ -4,6 +4,7 @@ using Microsoft.OpenApi.Models; using System; using System.IO; using Yi.Framework.Common.IOCOptions; +using Yi.Framework.Core; namespace Yi.Framework.WebCore.MiddlewareExtend { @@ -17,6 +18,7 @@ namespace Yi.Framework.WebCore.MiddlewareExtend if (Appsettings.appBool("Redis_Enabled")) { services.Configure(Appsettings.appConfiguration("RedisConn")); + services.AddTransient(); } return services; } diff --git a/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/RedisInitExtend.cs b/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/RedisInitExtend.cs new file mode 100644 index 00000000..8f813afd --- /dev/null +++ b/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/RedisInitExtend.cs @@ -0,0 +1,37 @@ +using log4net; +using Microsoft.AspNetCore.Builder; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Core; +using Yi.Framework.Model.ModelFactory; +using Yi.Framework.WebCore.Init; + +namespace Yi.Framework.WebCore.MiddlewareExtend +{ + public static class RedisInitExtend + { + private static readonly ILog log = LogManager.GetLogger(typeof(RedisInitExtend)); + public static void UseRedisInitService(this IApplicationBuilder app, CacheClientDB _cacheClientDB) + { + + if (Appsettings.appBool("RedisSet_Enabled")) + { + if (app == null) throw new ArgumentNullException(nameof(app)); + + try + { + RedisInit.Seed(_cacheClientDB); + } + catch (Exception e) + { + log.Error($"Error occured seeding the RedisInit.\n{e.Message}"); + throw; + } + } + } + } +} diff --git a/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj b/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj index 6af582f3..4e946f1b 100644 --- a/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj +++ b/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj @@ -15,13 +15,13 @@ - +