From a9547257e64479e6eec0c5f9be9fd068e03a1d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Fri, 23 Jun 2023 16:36:00 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E5=A4=9A=E5=BA=93?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E5=81=B6=E5=85=88bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Infrastructure/App/App.cs | 6 +++++ Infrastructure/GlobalConstant.cs | 11 +++++----- Infrastructure/OptionsSetting.cs | 5 +---- .../System/CodeGeneratorController.cs | 22 +++++++------------ ZR.Admin.WebApi/Extensions/DbExtension.cs | 2 +- ZR.Admin.WebApi/Program.cs | 1 - ZR.Admin.WebApi/appsettings.json | 17 +++++++------- ZR.CodeGenerator/CodeGeneratorTool.cs | 6 ++--- ZR.CodeGenerator/DbProvider.cs | 3 +-- .../Service/CodeGeneraterService.cs | 3 +-- 10 files changed, 35 insertions(+), 41 deletions(-) diff --git a/Infrastructure/App/App.cs b/Infrastructure/App/App.cs index 89307f5..4c8357c 100644 --- a/Infrastructure/App/App.cs +++ b/Infrastructure/App/App.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using System; using System.Security.Claims; @@ -9,6 +10,11 @@ namespace Infrastructure { public static class App { + /// + /// 全局配置文件 + /// + public static OptionsSetting OptionsSetting => CatchOrDefault(() => ServiceProvider?.GetService>()?.Value); + /// /// 服务提供器 /// diff --git a/Infrastructure/GlobalConstant.cs b/Infrastructure/GlobalConstant.cs index 25fc8d0..ff72abe 100644 --- a/Infrastructure/GlobalConstant.cs +++ b/Infrastructure/GlobalConstant.cs @@ -1,14 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Infrastructure +namespace Infrastructure { /// /// 全局静态常量 /// public class GlobalConstant { + /// + /// 代码生成常量 + /// + public static readonly string CodeGenDbConfig; + /// /// 管理员权限 /// diff --git a/Infrastructure/OptionsSetting.cs b/Infrastructure/OptionsSetting.cs index fc85ef3..86726f9 100644 --- a/Infrastructure/OptionsSetting.cs +++ b/Infrastructure/OptionsSetting.cs @@ -19,6 +19,7 @@ namespace Infrastructure public JwtSettings JwtSettings { get; set; } public Gen Gen { get; set; } public List DbConfigs { get; set; } + public DbConfigs CodeGenDbConfig { get; set; } } /// /// 发送邮件数据配置 @@ -94,10 +95,6 @@ namespace Infrastructure public int DbType { get; set; } public string ConfigId { get; set; } public bool IsAutoCloseConnection { get; set; } - /// - /// 是否代码生成使用库 - /// - public bool IsGenerateDb { get; set; } public string DbName { get; set; } } diff --git a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs index dcfe5c7..1e40c48 100644 --- a/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs +++ b/ZR.Admin.WebApi/Controllers/System/CodeGeneratorController.cs @@ -2,7 +2,6 @@ using Infrastructure.Attribute; using Infrastructure.Enums; using Infrastructure.Extensions; -using IP2Region.Ex.Models; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; @@ -13,7 +12,6 @@ using ZR.CodeGenerator.Model; using ZR.CodeGenerator.Service; using ZR.Common; using ZR.Model; -using ZR.Model.System; using ZR.Model.System.Dto; using ZR.Model.System.Generate; using ZR.Service.System.IService; @@ -153,7 +151,7 @@ namespace ZR.Admin.WebApi.Controllers { throw new CustomException("表不能为空"); } - var dbConfig = AppSettings.Get>("dbConfigs").FirstOrDefault(f => f.IsGenerateDb); + DbConfigs dbConfig = AppSettings.Get(nameof(GlobalConstant.CodeGenDbConfig)); string[] tableNames = tables.Split(',', StringSplitOptions.RemoveEmptyEntries); int result = 0; foreach (var tableName in tableNames) @@ -218,29 +216,25 @@ namespace ZR.Admin.WebApi.Controllers /// /// 预览代码 /// + /// /// - /// /// [HttpPost("preview/{tableId}")] [ActionPermissionFilter(Permission = "tool:gen:preview")] - public IActionResult Preview(long tableId = 0, int VueVersion = 0) + public IActionResult Preview([FromQuery] GenerateDto dto, [FromRoute] int tableId = 0) { - GenerateDto dto = new() - { - TableId = tableId, - VueVersion = VueVersion - }; + dto.TableId = tableId; if (dto == null || dto.TableId <= 0) { throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); } var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); - var dbConfig = AppSettings.Get>("dbConfigs").FirstOrDefault(f => f.IsGenerateDb); - + var dbConfig = AppSettings.Get(nameof(GlobalConstant.CodeGenDbConfig)); + dto.DbType = dbConfig.DbType; dto.GenTable = genTableInfo; dto.IsPreview = true; - //生成代码 + CodeGeneratorTool.Generate(dto); return SUCCESS(dto.GenCodes); @@ -261,7 +255,7 @@ namespace ZR.Admin.WebApi.Controllers throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); } var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); - var dbConfig = AppSettings.Get>("dbConfigs").FirstOrDefault(f => f.IsGenerateDb); + var dbConfig = AppSettings.Get(nameof(GlobalConstant.CodeGenDbConfig)); dto.DbType = dbConfig.DbType; dto.GenTable = genTableInfo; diff --git a/ZR.Admin.WebApi/Extensions/DbExtension.cs b/ZR.Admin.WebApi/Extensions/DbExtension.cs index 5840448..fa16f43 100644 --- a/ZR.Admin.WebApi/Extensions/DbExtension.cs +++ b/ZR.Admin.WebApi/Extensions/DbExtension.cs @@ -36,7 +36,7 @@ namespace ZR.Admin.WebApi.Extensions List dbConfigs = Configuration.GetSection("DbConfigs").Get>(); var iocList = new List(); - foreach (var item in dbConfigs.FindAll(f => !f.IsGenerateDb)) + foreach (var item in dbConfigs) { iocList.Add(new IocConfig() { diff --git a/ZR.Admin.WebApi/Program.cs b/ZR.Admin.WebApi/Program.cs index 232f917..87780c8 100644 --- a/ZR.Admin.WebApi/Program.cs +++ b/ZR.Admin.WebApi/Program.cs @@ -62,7 +62,6 @@ builder.Services.AddAuthentication(options => }; }); -//InternalApp.InternalServices = builder.Services; builder.Services.AddSingleton(new AppSettings(builder.Configuration)); builder.Services.AddAppService(); //开启计划任务 diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json index ee4aaab..f7862a1 100644 --- a/ZR.Admin.WebApi/appsettings.json +++ b/ZR.Admin.WebApi/appsettings.json @@ -12,18 +12,17 @@ "DbType": 1, //数据库类型 MySql = 0, SqlServer = 1, Oracle = 3,PgSql = 4 "ConfigId": "0", //多租户唯一标识 "IsAutoCloseConnection": true - }, - { - //代码生成连接字符串,注意{dbName}为固定格式,不要填写数据库名 - "Conn": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=admin;Password=admin123;Initial Catalog={dbName};", - "DbType": 1, - "ConfigId": "0", - "IsAutoCloseConnection": true, - "DbName": "ZrAdmin", //代码生成默认连接数据库 - "IsGenerateDb": true //是否代码生成使用库,不要改动 } //...下面添加更多的数据库源 ], + //代码生成数据库配置 + "CodeGenDbConfig": { + //代码生成连接字符串,注意{dbName}为固定格式,不要填写数据库名 + "Conn": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=admin;Password=admin123;Initial Catalog={dbName};", + "DbType": 1, + "IsAutoCloseConnection": true, + "DbName": "ZrAdmin" //代码生成默认连接数据库 + }, "urls": "http://localhost:8888", //项目启动url,如果改动端口前端对应devServer也需要进行修改 "corsUrls": "http://localhost:8887", //跨域地址(前端启动项目,前后端分离单独部署需要设置),多个用","隔开 "JwtSettings": { diff --git a/ZR.CodeGenerator/CodeGeneratorTool.cs b/ZR.CodeGenerator/CodeGeneratorTool.cs index 41a6509..36dfb7a 100644 --- a/ZR.CodeGenerator/CodeGeneratorTool.cs +++ b/ZR.CodeGenerator/CodeGeneratorTool.cs @@ -414,9 +414,9 @@ namespace ZR.CodeGenerator OptionsSetting optionsSetting = new(); var gen = AppSettings.Get("gen"); - var dbConfigs = AppSettings.Get>("dbConfigs"); - var dbConfig = dbConfigs.FirstOrDefault(f => f.IsGenerateDb); - optionsSetting.DbConfigs = dbConfigs; + var dbConfig = AppSettings.Get("CodeGenDbConfig"); + + optionsSetting.CodeGenDbConfig = dbConfig; optionsSetting.Gen = gen ?? throw new CustomException("代码生成节点配置异常"); optionsSetting.Gen.GenDbConfig = dbConfig ?? throw new CustomException("代码生成节点数据配置异常"); List genTableColumns = new(); diff --git a/ZR.CodeGenerator/DbProvider.cs b/ZR.CodeGenerator/DbProvider.cs index afbff3d..da58700 100644 --- a/ZR.CodeGenerator/DbProvider.cs +++ b/ZR.CodeGenerator/DbProvider.cs @@ -20,9 +20,8 @@ namespace ZR.CodeGenerator /// public SqlSugarClient GetSugarDbContext(string dbName = "") { - List dbConfigs = AppSettings.Get>("dbConfigs"); + DbConfigs configs = AppSettings.Get("CodeGenDbConfig"); - DbConfigs configs = dbConfigs.Find(f => f.IsGenerateDb == true); string connStr = configs.Conn; if (!string.IsNullOrEmpty(dbName)) diff --git a/ZR.CodeGenerator/Service/CodeGeneraterService.cs b/ZR.CodeGenerator/Service/CodeGeneraterService.cs index bbe2557..85024e6 100644 --- a/ZR.CodeGenerator/Service/CodeGeneraterService.cs +++ b/ZR.CodeGenerator/Service/CodeGeneraterService.cs @@ -17,8 +17,7 @@ namespace ZR.CodeGenerator.Service { var db = GetSugarDbContext(); //Oracle库特殊处理 - List dbConfigs = AppSettings.Get>("dbConfigs"); - DbConfigs configs = dbConfigs.Find(f => f.IsGenerateDb == true); + DbConfigs configs = AppSettings.Get(nameof(GlobalConstant.CodeGenDbConfig)); if (configs.DbType == 3) { return new List() { configs?.DbName };