From 8e0b85c4849fff82075e535f500ba9bb44741309 Mon Sep 17 00:00:00 2001 From: LittleFish-233 Date: Mon, 21 Aug 2023 10:39:20 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9C=8B=E6=9D=BF=E5=A8=98=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E9=85=8D=E7=BD=AE=E5=8D=A1=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Configurations/ConfigurationModel.cs | 15 +++++ .../.config/dotnet-tools.json | 12 ++++ .../CnGalWebSite.RobotClientX/App.razor | 2 +- .../Components/Home/EditCard.razor | 44 ++++++++++++++ .../Pages/Home/Index.razor | 6 ++ .../CnGalWebSite.RobotClientX/Program.cs | 2 +- .../Configurations/ConfigurationService.cs | 57 +++++++++++++++++++ .../Configurations/IConfigurationService.cs | 11 ++++ .../Services/QQClients/QQClientService.cs | 2 +- .../SensitiveWords/SensitiveWordService.cs | 5 +- .../CnGalWebSite.RobotClientX/_Imports.razor | 3 + .../CnGalWebSite.RobotClientX/libman.json | 5 ++ 12 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 CnGalWebSite/CnGalWebSite.RobotClientX.Models/Configurations/ConfigurationModel.cs create mode 100644 CnGalWebSite/CnGalWebSite.RobotClientX/.config/dotnet-tools.json create mode 100644 CnGalWebSite/CnGalWebSite.RobotClientX/Components/Home/EditCard.razor create mode 100644 CnGalWebSite/CnGalWebSite.RobotClientX/Services/Configurations/ConfigurationService.cs create mode 100644 CnGalWebSite/CnGalWebSite.RobotClientX/Services/Configurations/IConfigurationService.cs create mode 100644 CnGalWebSite/CnGalWebSite.RobotClientX/libman.json diff --git a/CnGalWebSite/CnGalWebSite.RobotClientX.Models/Configurations/ConfigurationModel.cs b/CnGalWebSite/CnGalWebSite.RobotClientX.Models/Configurations/ConfigurationModel.cs new file mode 100644 index 000000000..da24fd059 --- /dev/null +++ b/CnGalWebSite/CnGalWebSite.RobotClientX.Models/Configurations/ConfigurationModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CnGalWebSite.RobotClientX.Models.Configurations +{ + public class ConfigurationModel + { + public string ChatGPT_SystemMessageTemplate { get; set; } + + public string ChatGPT_UserMessageTemplate { get; set; } + } +} diff --git a/CnGalWebSite/CnGalWebSite.RobotClientX/.config/dotnet-tools.json b/CnGalWebSite/CnGalWebSite.RobotClientX/.config/dotnet-tools.json new file mode 100644 index 000000000..558293e1d --- /dev/null +++ b/CnGalWebSite/CnGalWebSite.RobotClientX/.config/dotnet-tools.json @@ -0,0 +1,12 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-ef": { + "version": "7.0.10", + "commands": [ + "dotnet-ef" + ] + } + } +} \ No newline at end of file diff --git a/CnGalWebSite/CnGalWebSite.RobotClientX/App.razor b/CnGalWebSite/CnGalWebSite.RobotClientX/App.razor index ef3f3ce04..b72d68781 100644 --- a/CnGalWebSite/CnGalWebSite.RobotClientX/App.razor +++ b/CnGalWebSite/CnGalWebSite.RobotClientX/App.razor @@ -19,7 +19,7 @@ @code{ - + [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(HeadOutlet))] protected override async Task OnParametersSetAsync() { I18n.SetCulture(System.Globalization.CultureInfo.GetCultureInfo("zh-CN"));//将语言切换成zh-CN diff --git a/CnGalWebSite/CnGalWebSite.RobotClientX/Components/Home/EditCard.razor b/CnGalWebSite/CnGalWebSite.RobotClientX/Components/Home/EditCard.razor new file mode 100644 index 000000000..55585b721 --- /dev/null +++ b/CnGalWebSite/CnGalWebSite.RobotClientX/Components/Home/EditCard.razor @@ -0,0 +1,44 @@ +@inject IConfigurationService _configurationService + + + + 编辑配置 + + + + + + + + + + + + + + + + mdi-check + 保存 + + + + +@code { + [Parameter] + public EventCallback ConfigurationChanged { get; set; } + + ConfigurationModel _configurationModel = new ConfigurationModel(); + + protected override void OnInitialized() + { + _configurationModel = _configurationService.GetConfiguration(); + } + + public async Task SaveEdit() + { + _configurationService.Save(); + await ConfigurationChanged.InvokeAsync(); + } +} diff --git a/CnGalWebSite/CnGalWebSite.RobotClientX/Pages/Home/Index.razor b/CnGalWebSite/CnGalWebSite.RobotClientX/Pages/Home/Index.razor index e64078351..6ce578a74 100644 --- a/CnGalWebSite/CnGalWebSite.RobotClientX/Pages/Home/Index.razor +++ b/CnGalWebSite/CnGalWebSite.RobotClientX/Pages/Home/Index.razor @@ -54,6 +54,7 @@ + @code { @@ -82,4 +83,9 @@ { _applicationLifetime.StopApplication(); } + + public void ConfigurationChanged() + { + StateHasChanged(); + } } diff --git a/CnGalWebSite/CnGalWebSite.RobotClientX/Program.cs b/CnGalWebSite/CnGalWebSite.RobotClientX/Program.cs index dccf7588f..cabea6e62 100644 --- a/CnGalWebSite/CnGalWebSite.RobotClientX/Program.cs +++ b/CnGalWebSite/CnGalWebSite.RobotClientX/Program.cs @@ -10,10 +10,10 @@ using Swashbuckle.AspNetCore.Filters; using System.Reflection; - var builder = WebApplication.CreateBuilder(args); //自动重置配置 builder.Configuration.AddJsonFile("appsettings.json", true, reloadOnChange: true); +builder.Configuration.AddJsonFile(Path.Combine(builder.Environment.WebRootPath,"Data","Setting.json"), true, reloadOnChange: true); // Add services to the container. builder.Services.AddRazorPages(); diff --git a/CnGalWebSite/CnGalWebSite.RobotClientX/Services/Configurations/ConfigurationService.cs b/CnGalWebSite/CnGalWebSite.RobotClientX/Services/Configurations/ConfigurationService.cs new file mode 100644 index 000000000..6c5daa3b6 --- /dev/null +++ b/CnGalWebSite/CnGalWebSite.RobotClientX/Services/Configurations/ConfigurationService.cs @@ -0,0 +1,57 @@ +using CnGalWebSite.RobotClientX.Models.Configurations; +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Unicode; + +namespace CnGalWebSite.RobotClientX.Services.Configurations +{ + public class ConfigurationService:IConfigurationService + { + private ConfigurationModel _configurationModel; + private readonly IConfiguration _configuration; + private readonly IWebHostEnvironment _webHostEnvironment; + + public ConfigurationService(IConfiguration configuration, IWebHostEnvironment webHostEnvironment) + { + _configuration = configuration; + _webHostEnvironment = webHostEnvironment; + + Load(); + } + + public void Load() + { + var path = Path.Combine(_webHostEnvironment.WebRootPath, "Data", "Setting.json"); + string jsonString = File.ReadAllText(path); + _configurationModel = JsonSerializer.Deserialize(jsonString)!; + + if(string.IsNullOrWhiteSpace( _configurationModel.ChatGPT_SystemMessageTemplate)) + { + _configurationModel.ChatGPT_SystemMessageTemplate = _configuration["ChatGPT_SystemMessageTemplate"]; + } + if (string.IsNullOrWhiteSpace(_configurationModel.ChatGPT_UserMessageTemplate)) + { + _configurationModel.ChatGPT_UserMessageTemplate = _configuration["ChatGPT_UserMessageTemplate"]; + } + + Save(); + } + + public void Save() + { + var path = Path.Combine(_webHostEnvironment.WebRootPath, "Data", "Setting.json"); + var options = new JsonSerializerOptions + { + WriteIndented = true, + Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.All) + }; + string jsonString = JsonSerializer.Serialize(_configurationModel, options); + File.WriteAllText(path, jsonString); + } + + public ConfigurationModel GetConfiguration() + { + return _configurationModel; + } + } +} diff --git a/CnGalWebSite/CnGalWebSite.RobotClientX/Services/Configurations/IConfigurationService.cs b/CnGalWebSite/CnGalWebSite.RobotClientX/Services/Configurations/IConfigurationService.cs new file mode 100644 index 000000000..c6006da0a --- /dev/null +++ b/CnGalWebSite/CnGalWebSite.RobotClientX/Services/Configurations/IConfigurationService.cs @@ -0,0 +1,11 @@ +using CnGalWebSite.RobotClientX.Models.Configurations; + +namespace CnGalWebSite.RobotClientX.Services.Configurations +{ + public interface IConfigurationService + { + ConfigurationModel GetConfiguration(); + + void Save(); + } +} diff --git a/CnGalWebSite/CnGalWebSite.RobotClientX/Services/QQClients/QQClientService.cs b/CnGalWebSite/CnGalWebSite.RobotClientX/Services/QQClients/QQClientService.cs index e7fdb6884..e62381db4 100644 --- a/CnGalWebSite/CnGalWebSite.RobotClientX/Services/QQClients/QQClientService.cs +++ b/CnGalWebSite/CnGalWebSite.RobotClientX/Services/QQClients/QQClientService.cs @@ -218,7 +218,7 @@ public async Task Init() }; } - _logger.LogInformation("CnGal资料站 看板娘 v3.4.6"); + _logger.LogInformation("CnGal资料站 看板娘 v3.4.7"); } diff --git a/CnGalWebSite/CnGalWebSite.RobotClientX/Services/SensitiveWords/SensitiveWordService.cs b/CnGalWebSite/CnGalWebSite.RobotClientX/Services/SensitiveWords/SensitiveWordService.cs index a8f4a3dac..3014bce29 100644 --- a/CnGalWebSite/CnGalWebSite.RobotClientX/Services/SensitiveWords/SensitiveWordService.cs +++ b/CnGalWebSite/CnGalWebSite.RobotClientX/Services/SensitiveWords/SensitiveWordService.cs @@ -1,9 +1,12 @@ using System; using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.AspNetCore.Components.Web; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; @@ -27,7 +30,7 @@ public SensitiveWordService(IConfiguration configuration, ILogger