From 3b0c316928d33a05dda6cd9946d0434fad8c9122 Mon Sep 17 00:00:00 2001
From: lq1405 <2769838458@qq.com>
Date: Wed, 13 Nov 2024 14:00:39 +0800
Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=8F=90=E7=A4=BA=E8=AF=8D?=
=?UTF-8?q?=E9=A2=84=E8=AE=BE=E7=9A=84=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=AE=8C?=
=?UTF-8?q?=E5=96=84=E8=BD=AC=E5=8F=91=E6=8E=A5=E5=8F=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
LMS.Common/Dictionary/AllOptions.cs | 3 +-
LMS.Common/Enum/PromptEnum.cs | 23 +-
LMS.DAO/ApplicationDbContext.cs | 4 +
LMS.DAO/UserDAO/UserBasicDAO.cs | 19 +
LMS.Repository/DB/Options.cs | 68 ++--
LMS.Repository/DB/Prompt.cs | 6 +-
LMS.Repository/DB/PromptType.cs | 38 +-
LMS.Repository/DB/Role.cs | 3 +-
LMS.Repository/DTO/PromptDto/PromptDto.cs | 15 +
LMS.Repository/DTO/PromptDto/PromptNameDto.cs | 10 +
.../DTO/PromptTypeDto/PrompTypeNameModel.cs | 8 +
.../DTO/PromptTypeDto/PromptTypeBasic.cs | 29 ++
.../DTO/PromptTypeDto/PromptTypeDto.cs | 12 +
LMS.Repository/Forward/ForwardModel.cs | 48 +++
LMS.Repository/LMS.Repository.csproj | 4 -
.../PromptModel/ModifyPromptModel.cs | 45 +++
.../PromptModel/ModifyPromptTypeModal.cs | 29 ++
LMS.service/Configuration/AutoMapperConfig.cs | 11 +
.../DatabaseConfiguration.cs | 1 +
.../Configuration/ServiceConfiguration.cs | 5 +
LMS.service/Controllers/ForwardController.cs | 73 ++++
LMS.service/Controllers/PromptController.cs | 216 ++++++++++
LMS.service/LMS.service.csproj | 1 +
LMS.service/Service/ForwardWordService.cs | 147 +++++++
LMS.service/Service/MachineService.cs | 33 +-
.../Service/PromptService/PromptService.cs | 377 ++++++++++++++++++
.../PromptService/PromptTypeService.cs | 358 +++++++++++++++++
27 files changed, 1487 insertions(+), 99 deletions(-)
create mode 100644 LMS.Repository/DTO/PromptDto/PromptDto.cs
create mode 100644 LMS.Repository/DTO/PromptDto/PromptNameDto.cs
create mode 100644 LMS.Repository/DTO/PromptTypeDto/PrompTypeNameModel.cs
create mode 100644 LMS.Repository/DTO/PromptTypeDto/PromptTypeBasic.cs
create mode 100644 LMS.Repository/DTO/PromptTypeDto/PromptTypeDto.cs
create mode 100644 LMS.Repository/Forward/ForwardModel.cs
create mode 100644 LMS.Repository/PromptModel/ModifyPromptModel.cs
create mode 100644 LMS.Repository/PromptModel/ModifyPromptTypeModal.cs
create mode 100644 LMS.service/Controllers/ForwardController.cs
create mode 100644 LMS.service/Controllers/PromptController.cs
create mode 100644 LMS.service/Service/ForwardWordService.cs
create mode 100644 LMS.service/Service/PromptService/PromptService.cs
create mode 100644 LMS.service/Service/PromptService/PromptTypeService.cs
diff --git a/LMS.Common/Dictionary/AllOptions.cs b/LMS.Common/Dictionary/AllOptions.cs
index baebdc9..f44015c 100644
--- a/LMS.Common/Dictionary/AllOptions.cs
+++ b/LMS.Common/Dictionary/AllOptions.cs
@@ -6,6 +6,7 @@ public class AllOptions
{
{ "all", [] },
{ "tts", ["EdgeTTsRoles"] },
- { "software", ["LaitoolHomePage", "LaitoolNotice", "LaitoolUpdateContent","LaitoolVersion"]}
+ { "software", ["LaitoolHomePage", "LaitoolNotice", "LaitoolUpdateContent","LaitoolVersion"]},
+ { "trial" , ["LaiToolTrialDays"] }
};
}
diff --git a/LMS.Common/Enum/PromptEnum.cs b/LMS.Common/Enum/PromptEnum.cs
index 2e66584..c3b2bfe 100644
--- a/LMS.Common/Enum/PromptEnum.cs
+++ b/LMS.Common/Enum/PromptEnum.cs
@@ -2,14 +2,6 @@
{
public class PromptEnum
{
- public enum PromptType
- {
- ///
- /// 开头提示词
- ///
- StartPrompt = 0,
- }
-
public class PromptStatus
{
///
@@ -21,6 +13,15 @@
///提示词状态:禁用
///
public const string Disable = "disable";
+
+ ///
+ /// 所有有效的提示词类型状态
+ ///
+ public static readonly HashSet ValidStatuses =
+ [
+ Enable,
+ Disable
+ ];
}
public class PromptTypeStatus
@@ -38,11 +39,11 @@
///
/// 所有有效的提示词类型状态
///
- public static readonly HashSet ValidStatuses = new HashSet
- {
+ public static readonly HashSet ValidStatuses =
+ [
Enable,
Disable
- };
+ ];
}
}
diff --git a/LMS.DAO/ApplicationDbContext.cs b/LMS.DAO/ApplicationDbContext.cs
index 723eedd..5d8fff3 100644
--- a/LMS.DAO/ApplicationDbContext.cs
+++ b/LMS.DAO/ApplicationDbContext.cs
@@ -20,6 +20,10 @@ namespace LMS.DAO
public DbSet PermissionType { get; set; }
+ public DbSet PromptType { get; set; }
+
+ public DbSet Prompt { get; set; }
+
public DbSet Machine { get; set; }
public DbSet RefreshTokens { get; set; }
diff --git a/LMS.DAO/UserDAO/UserBasicDAO.cs b/LMS.DAO/UserDAO/UserBasicDAO.cs
index f8cdea4..efdcc69 100644
--- a/LMS.DAO/UserDAO/UserBasicDAO.cs
+++ b/LMS.DAO/UserDAO/UserBasicDAO.cs
@@ -1,5 +1,6 @@
using LMS.Repository.Models.DB;
using Microsoft.AspNetCore.Identity;
+using OneOf.Types;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -24,6 +25,24 @@ namespace LMS.DAO.UserDAO
}
return await _userManager.FindByIdAsync(userId.ToString()) != null;
}
+
+ ///
+ /// 判断传入的数据是不是有管理员权限或者是超级管理员权限
+ ///
+ ///
+ ///
+ ///
+ public async Task CheckUserIsAdminOrSuperAdmin(long? userId)
+ {
+ if (userId == null)
+ {
+ return false;
+ }
+ User? user = await _userManager.FindByIdAsync(userId.ToString() ?? "0") ?? throw new Exception("用户不存在");
+
+ bool isAdminOrSuperAdmin = await _userManager.IsInRoleAsync(user, "Admin") || await _userManager.IsInRoleAsync(user, "Super Admin");
+ return isAdminOrSuperAdmin;
+ }
}
}
diff --git a/LMS.Repository/DB/Options.cs b/LMS.Repository/DB/Options.cs
index cab9e8a..5695cad 100644
--- a/LMS.Repository/DB/Options.cs
+++ b/LMS.Repository/DB/Options.cs
@@ -1,5 +1,6 @@
using LMS.Common.Enum;
using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@@ -18,44 +19,47 @@ public class Options
public OptionTypeEnum Type { get; set; } = OptionTypeEnum.String;
// 写一个字段,映射Value,判断是不是json字符串,是的话就解析成对象
- [NotMapped]
- public object? ValueObject
+ // 写一个字段,映射Value,判断是不是json字符串,是的话就解析成对象
+ public T? GetValueObject()
{
- get
+ if (string.IsNullOrEmpty(Value))
{
- if (string.IsNullOrEmpty(Value))
- {
- return Value;
- }
-
- if (Type == OptionTypeEnum.JSON)
- {
- return JsonConvert.DeserializeObject(Value ?? "{}");
- }
-
- if (Type == OptionTypeEnum.Number)
- {
- return Convert.ToDouble(Value);
- }
-
- return Value;
+ return default;
}
- set
- {
- if (value == null)
- {
- Value = string.Empty;
- return;
- }
- if (Type == OptionTypeEnum.JSON)
+ if (Type == OptionTypeEnum.JSON)
+ {
+ return JsonConvert.DeserializeObject(Value ?? "{}");
+ }
+
+ if (Type == OptionTypeEnum.Number)
+ {
+ if (double.TryParse(Value, out double result))
{
- Value = JsonConvert.SerializeObject(value);
- }
- else
- {
- Value = value.ToString();
+ return (T)Convert.ChangeType(result, typeof(T));
}
+ return default;
+ }
+
+ return (T)Convert.ChangeType(Value, typeof(T));
+ }
+
+ // 写一个方法,设置Value的值
+ public void SetValueObject(T value)
+ {
+ if (value == null)
+ {
+ Value = string.Empty;
+ return;
+ }
+
+ if (Type == OptionTypeEnum.JSON)
+ {
+ Value = JsonConvert.SerializeObject(value);
+ }
+ else
+ {
+ Value = value.ToString();
}
}
}
diff --git a/LMS.Repository/DB/Prompt.cs b/LMS.Repository/DB/Prompt.cs
index afaa291..4de484a 100644
--- a/LMS.Repository/DB/Prompt.cs
+++ b/LMS.Repository/DB/Prompt.cs
@@ -1,4 +1,4 @@
-namespace LMS.Repository.Models.DB
+namespace LMS.Repository.DB
{
public class Prompt
{
@@ -45,7 +45,7 @@
///
/// 创建者
///
- public string CreateUserId { get; set; }
+ public long CreateUserId { get; set; }
///
/// 创建时间
@@ -55,7 +55,7 @@
///
/// 更新者
///
- public string UpdateUserId { get; set; }
+ public long UpdateUserId { get; set; }
///
/// 更新时间
diff --git a/LMS.Repository/DB/PromptType.cs b/LMS.Repository/DB/PromptType.cs
index f204e63..15f1f22 100644
--- a/LMS.Repository/DB/PromptType.cs
+++ b/LMS.Repository/DB/PromptType.cs
@@ -1,35 +1,12 @@
-namespace LMS.Repository.Models.DB
+using LMS.Repository.DTO.PromptTypeDto;
+
+namespace LMS.Repository.Models.DB
{
///
/// 提示词类型的库
///
- public class PromptType
+ public class PromptType : PromptTypeBasic
{
- ///
- /// ID
- ///
- public string Id { get; set; }
-
- ///
- /// 提示词类型编码
- ///
- public string Code { get; set; }
-
- ///
- /// 提示词类型名称
- ///
- public string Name { get; set; }
-
- ///
- /// 提示词类型描述
- ///
- public string? Remark { get; set; }
-
- ///
- /// 提示词类型状态
- ///
- public string Status { get; set; }
-
///
/// 是否删除
///
@@ -38,7 +15,7 @@
///
/// 创建用户ID
///
- public string CreateUserId { get; set; }
+ public long CreateUserId { get; set; }
///
/// 创建时间
@@ -48,14 +25,11 @@
///
/// 更新者ID
///
- public string UpdateUserId { get; set; }
+ public long UpdateUserId { get; set; }
///
/// 更新时间
///
public DateTime UpdateTime { get; set; }
-
-
-
}
}
diff --git a/LMS.Repository/DB/Role.cs b/LMS.Repository/DB/Role.cs
index b4b1f52..d4af099 100644
--- a/LMS.Repository/DB/Role.cs
+++ b/LMS.Repository/DB/Role.cs
@@ -1,5 +1,4 @@
-using LMS.Tools.Extensions;
-using Microsoft.AspNetCore.Identity;
+using Microsoft.AspNetCore.Identity;
namespace LMS.Repository.Models.DB
{
diff --git a/LMS.Repository/DTO/PromptDto/PromptDto.cs b/LMS.Repository/DTO/PromptDto/PromptDto.cs
new file mode 100644
index 0000000..4a404c5
--- /dev/null
+++ b/LMS.Repository/DTO/PromptDto/PromptDto.cs
@@ -0,0 +1,15 @@
+using LMS.Repository.DB;
+using LMS.Repository.DTO.PromptTypeDto;
+using LMS.Repository.DTO.UserDto;
+
+namespace LMS.Repository.DTO.PromptDto;
+
+public class PromptDto : Prompt
+{
+
+ public UserBaseDto CreatedUser { get; set; }
+
+ public UserBaseDto UpdatedUser { get; set; }
+
+ public PromptTypeBasic? PromptType { get; set; }
+}
diff --git a/LMS.Repository/DTO/PromptDto/PromptNameDto.cs b/LMS.Repository/DTO/PromptDto/PromptNameDto.cs
new file mode 100644
index 0000000..857f958
--- /dev/null
+++ b/LMS.Repository/DTO/PromptDto/PromptNameDto.cs
@@ -0,0 +1,10 @@
+namespace LMS.Repository.DTO.PromptDto;
+
+public class PromptNameDto
+{
+ public string Id { get; set; }
+
+ public string Name { get; set; }
+
+ public string PromptTypeId { get; set; }
+}
diff --git a/LMS.Repository/DTO/PromptTypeDto/PrompTypeNameModel.cs b/LMS.Repository/DTO/PromptTypeDto/PrompTypeNameModel.cs
new file mode 100644
index 0000000..8558096
--- /dev/null
+++ b/LMS.Repository/DTO/PromptTypeDto/PrompTypeNameModel.cs
@@ -0,0 +1,8 @@
+namespace LMS.Repository.DTO.PromptTypeDto;
+
+public class PrompTypeNameModel
+{
+ public string Name { get; set; }
+
+ public string Id { get; set; }
+}
diff --git a/LMS.Repository/DTO/PromptTypeDto/PromptTypeBasic.cs b/LMS.Repository/DTO/PromptTypeDto/PromptTypeBasic.cs
new file mode 100644
index 0000000..f5ec73e
--- /dev/null
+++ b/LMS.Repository/DTO/PromptTypeDto/PromptTypeBasic.cs
@@ -0,0 +1,29 @@
+namespace LMS.Repository.DTO.PromptTypeDto;
+
+public class PromptTypeBasic
+{
+ ///
+ /// ID
+ ///
+ public string Id { get; set; }
+
+ ///
+ /// 提示词类型编码
+ ///
+ public string Code { get; set; }
+
+ ///
+ /// 提示词类型名称
+ ///
+ public string Name { get; set; }
+
+ ///
+ /// 提示词类型描述
+ ///
+ public string? Remark { get; set; }
+
+ ///
+ /// 提示词类型状态
+ ///
+ public string Status { get; set; }
+}
diff --git a/LMS.Repository/DTO/PromptTypeDto/PromptTypeDto.cs b/LMS.Repository/DTO/PromptTypeDto/PromptTypeDto.cs
new file mode 100644
index 0000000..39c9ce1
--- /dev/null
+++ b/LMS.Repository/DTO/PromptTypeDto/PromptTypeDto.cs
@@ -0,0 +1,12 @@
+using LMS.Repository.DTO.UserDto;
+using LMS.Repository.Models.DB;
+
+namespace LMS.Repository.DTO.PromptTypeDto
+{
+ public class PromptTypeDto : PromptType
+ {
+ public UserBaseDto? UpdatedUser { get; set; }
+
+ public UserBaseDto? CreatedUser { get; set; }
+ }
+}
diff --git a/LMS.Repository/Forward/ForwardModel.cs b/LMS.Repository/Forward/ForwardModel.cs
new file mode 100644
index 0000000..17a225f
--- /dev/null
+++ b/LMS.Repository/Forward/ForwardModel.cs
@@ -0,0 +1,48 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace LMS.Repository.Forward;
+
+public class ForwardModel
+{
+ ///
+ /// 提示词类型ID
+ ///
+ [Required]
+ public string PromptTypeId { get; set; }
+
+ ///
+ /// 提示词ID
+ ///
+ [Required]
+ public string PromptId { get; set; }
+
+ ///
+ /// GPT 请求网址
+ ///
+ [Required]
+ public string GptUrl { get; set; }
+
+ ///
+ /// 调用的模型
+ ///
+ [Required]
+ public string Model { get; set; }
+
+ ///
+ /// 机器码
+ ///
+ [Required]
+ public string MachineId { get; set; }
+
+ ///
+ /// API Key
+ ///
+ [Required]
+ public string ApiKey { get; set; }
+
+ ///
+ /// 处理之前的文案,基础文案
+ ///
+ [Required]
+ public string Word { get; set; } = string.Empty;
+}
diff --git a/LMS.Repository/LMS.Repository.csproj b/LMS.Repository/LMS.Repository.csproj
index e389625..31743cd 100644
--- a/LMS.Repository/LMS.Repository.csproj
+++ b/LMS.Repository/LMS.Repository.csproj
@@ -6,10 +6,6 @@
enable
-
-
-
-
diff --git a/LMS.Repository/PromptModel/ModifyPromptModel.cs b/LMS.Repository/PromptModel/ModifyPromptModel.cs
new file mode 100644
index 0000000..822bb90
--- /dev/null
+++ b/LMS.Repository/PromptModel/ModifyPromptModel.cs
@@ -0,0 +1,45 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace LMS.Repository.PromptModel;
+
+public class ModifyPromptModel
+{
+ ///
+ /// 名称
+ ///
+ [Required]
+ public string Name { get; set; }
+
+ ///
+ /// 提示词类型
+ ///
+ [Required]
+ public string PromptTypeId { get; set; }
+
+ ///
+ /// 提示词预设字符串
+ ///
+ [Required]
+ public string PromptString { get; set; }
+
+ ///
+ /// 描述
+ ///
+ public string? Description { get; set; }
+
+ ///
+ /// 备注
+ ///
+ public string? Remark { get; set; }
+
+ ///
+ /// 版本
+ ///
+ public int Version { get; set; }
+
+ [Required]
+ ///
+ /// 状态
+ ///
+ public string Status { get; set; }
+}
diff --git a/LMS.Repository/PromptModel/ModifyPromptTypeModal.cs b/LMS.Repository/PromptModel/ModifyPromptTypeModal.cs
new file mode 100644
index 0000000..1370cde
--- /dev/null
+++ b/LMS.Repository/PromptModel/ModifyPromptTypeModal.cs
@@ -0,0 +1,29 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace LMS.Repository.PromptModel;
+
+public class ModifyPromptTypeModal
+{
+ ///
+ /// 名称
+ ///
+ [Required]
+ public string Name { get; set; }
+
+ ///
+ /// 提示词类型编码
+ ///
+ [Required]
+ public string Code { get; set; }
+
+ ///
+ /// 状态
+ ///
+ [Required]
+ public string Status { get; set; }
+
+ ///
+ /// 备注
+ ///
+ public string? Remark { get; set; } = string.Empty;
+}
diff --git a/LMS.service/Configuration/AutoMapperConfig.cs b/LMS.service/Configuration/AutoMapperConfig.cs
index 0ce4bb7..b2755fd 100644
--- a/LMS.service/Configuration/AutoMapperConfig.cs
+++ b/LMS.service/Configuration/AutoMapperConfig.cs
@@ -1,10 +1,13 @@
using AutoMapper;
using LMS.Repository.DB;
using LMS.Repository.DTO;
+using LMS.Repository.DTO.PromptDto;
+using LMS.Repository.DTO.PromptTypeDto;
using LMS.Repository.DTO.UserDto;
using LMS.Repository.Models.DB;
using LMS.Repository.Models.Machine;
using LMS.Repository.Models.Promission;
+using LMS.Repository.PromptModel;
using LMS.Repository.RequestModel.Permission;
using static LMS.Repository.DTO.MachineResponse.MachineDto;
@@ -30,6 +33,14 @@ namespace Lai_server.Configuration
CreateMap();
+ CreateMap();
+
+ CreateMap();
+
+ CreateMap();
+
+ CreateMap();
+
}
}
}
diff --git a/LMS.service/Configuration/InitConfiguration/DatabaseConfiguration.cs b/LMS.service/Configuration/InitConfiguration/DatabaseConfiguration.cs
index a22d12e..b03d0c8 100644
--- a/LMS.service/Configuration/InitConfiguration/DatabaseConfiguration.cs
+++ b/LMS.service/Configuration/InitConfiguration/DatabaseConfiguration.cs
@@ -102,6 +102,7 @@ public class DatabaseConfiguration(IServiceProvider serviceProvider) : IHostedSe
new Options { Key = "LaitoolUpdateContent", Value = string.Empty, Type = OptionTypeEnum.String },
new Options { Key = "LaitoolNotice", Value = string.Empty, Type = OptionTypeEnum.String },
new Options { Key = "LaitoolVersion", Value = string.Empty, Type = OptionTypeEnum.String },
+ new Options { Key = "LaiToolTrialDays", Value = "2" , Type = OptionTypeEnum.Number}
];
// 遍历所有的配置项,如果没有则添加
diff --git a/LMS.service/Configuration/ServiceConfiguration.cs b/LMS.service/Configuration/ServiceConfiguration.cs
index 4393cb3..0b5e103 100644
--- a/LMS.service/Configuration/ServiceConfiguration.cs
+++ b/LMS.service/Configuration/ServiceConfiguration.cs
@@ -5,6 +5,7 @@ using LMS.DAO.UserDAO;
using LMS.service.Configuration.InitConfiguration;
using LMS.service.Service;
using LMS.service.Service.PermissionService;
+using LMS.service.Service.PromptService;
using LMS.service.Service.RoleService;
using LMS.service.Service.UserService;
@@ -27,6 +28,10 @@ namespace Lai_server.Configuration
services.AddScoped();
services.AddScoped();
services.AddScoped();
+ services.AddScoped();
+ services.AddScoped();
+ services.AddScoped();
+
// 注入 DAO
diff --git a/LMS.service/Controllers/ForwardController.cs b/LMS.service/Controllers/ForwardController.cs
new file mode 100644
index 0000000..2894c48
--- /dev/null
+++ b/LMS.service/Controllers/ForwardController.cs
@@ -0,0 +1,73 @@
+using LMS.DAO;
+using LMS.Repository.Forward;
+using LMS.service.Service;
+using Microsoft.AspNetCore.Mvc;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Serialization;
+using static LMS.Common.Enums.ResponseCodeEnum;
+
+namespace LMS.service.Controllers;
+
+[Route("lms/[controller]/[action]")]
+[ApiController]
+public class ForwardController(ForwardWordService forwardWordService) : ControllerBase
+{
+ private readonly ForwardWordService _forwardWordService = forwardWordService;
+
+ ///
+ /// 转发OpenAi格式的请求
+ ///
+ ///
+ ///
+ [HttpPost]
+ public async Task>> ForwardWord([FromBody] ForwardModel request)
+ {
+ if (!ModelState.IsValid)
+ {
+ return APIResponseModel