Skip to content

Commit

Permalink
看板娘添加动态配置卡片
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleFish-233 committed Aug 21, 2023
1 parent 057ca43 commit 8e0b85c
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 4 deletions.
@@ -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; }
}
}
12 changes: 12 additions & 0 deletions 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"
]
}
}
}
2 changes: 1 addition & 1 deletion CnGalWebSite/CnGalWebSite.RobotClientX/App.razor
Expand Up @@ -19,7 +19,7 @@
</ErrorHandler>

@code{

[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(HeadOutlet))]
protected override async Task OnParametersSetAsync()
{
I18n.SetCulture(System.Globalization.CultureInfo.GetCultureInfo("zh-CN"));//将语言切换成zh-CN
Expand Down
@@ -0,0 +1,44 @@
@inject IConfigurationService _configurationService

<MCard Class="mb-4">
<MCardTitle>
编辑配置
</MCardTitle>
<MCardText>
<MRow>
<MCol Cols="12">
<MTextarea Rows="3" @bind-Value="_configurationModel.ChatGPT_SystemMessageTemplate" Label="GPT 提示词" Outlined HideDetails="true"/>
</MCol>
<MCol Cols="12">
<MTextarea Rows="3" @bind-Value="_configurationModel.ChatGPT_UserMessageTemplate" Label="GPT 消息模板" Outlined HideDetails="true"/>
</MCol>
</MRow>

</MCardText>
<MCardActions Class="pb-6">
<MSpacer></MSpacer>
<MButton Color="primary"
OnClick="SaveEdit">
<MIcon Left>mdi-check</MIcon>
保存
</MButton>
</MCardActions>

</MCard>
@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();
}
}
6 changes: 6 additions & 0 deletions CnGalWebSite/CnGalWebSite.RobotClientX/Pages/Home/Index.razor
Expand Up @@ -54,6 +54,7 @@
</MButton>
</MCardActions>
</MCard>
<CnGalWebSite.RobotClientX.Components.Home.EditCard ConfigurationChanged="ConfigurationChanged"/>

@code {

Expand Down Expand Up @@ -82,4 +83,9 @@
{
_applicationLifetime.StopApplication();
}

public void ConfigurationChanged()
{
StateHasChanged();
}
}
2 changes: 1 addition & 1 deletion CnGalWebSite/CnGalWebSite.RobotClientX/Program.cs
Expand Up @@ -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();
Expand Down
@@ -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<ConfigurationModel>(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;
}
}
}
@@ -0,0 +1,11 @@
using CnGalWebSite.RobotClientX.Models.Configurations;

namespace CnGalWebSite.RobotClientX.Services.Configurations
{
public interface IConfigurationService
{
ConfigurationModel GetConfiguration();

void Save();
}
}
Expand Up @@ -218,7 +218,7 @@ public async Task Init()
};
}

_logger.LogInformation("CnGal资料站 看板娘 v3.4.6");
_logger.LogInformation("CnGal资料站 看板娘 v3.4.7");

}

Expand Down
@@ -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;

Expand All @@ -27,7 +30,7 @@ public SensitiveWordService(IConfiguration configuration, ILogger<SensitiveWordS
Load();
}


public void Load()
{
var path =Path.Combine( _webHostEnvironment.WebRootPath,"SensitiveWords");
Expand Down
3 changes: 3 additions & 0 deletions CnGalWebSite/CnGalWebSite.RobotClientX/_Imports.razor
Expand Up @@ -24,3 +24,6 @@
@using CnGalWebSite.RobotClientX.DataRepositories
@using System.Linq
@using CnGalWebSite.RobotClientX.Services.SensitiveWords
@using CnGalWebSite.RobotClientX.Services.Configurations
@using CnGalWebSite.RobotClientX.Models.Configurations
@using System.Diagnostics.CodeAnalysis;
5 changes: 5 additions & 0 deletions CnGalWebSite/CnGalWebSite.RobotClientX/libman.json
@@ -0,0 +1,5 @@
{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": []
}

0 comments on commit 8e0b85c

Please sign in to comment.