From 76b57c5717f8ae51ca63baf04dae9edbbf628c1b Mon Sep 17 00:00:00 2001 From: lq1405 <2769838458@qq.com> Date: Sat, 22 Feb 2025 15:00:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eget=E8=BD=AC=E5=8F=91?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=20=E6=96=B0=E5=A2=9ELaitool=20=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=B8=AD=E7=9A=84=E7=BB=98=E5=9B=BE=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LMS.Common/Dictionary/AllOptions.cs | 8 +- LMS.Common/Enum/OptionTypeEnum.cs | 8 ++ LMS.Repository/Forward/SimpleTransferModel.cs | 13 ++++ .../DatabaseConfiguration.cs | 9 +-- LMS.service/Controllers/ForwardController.cs | 16 +++- LMS.service/Controllers/OptionsController.cs | 7 -- LMS.service/Service/ForwardWordService.cs | 77 +++++++++++++++++-- 7 files changed, 118 insertions(+), 20 deletions(-) diff --git a/LMS.Common/Dictionary/AllOptions.cs b/LMS.Common/Dictionary/AllOptions.cs index f44015c..5eb2f57 100644 --- a/LMS.Common/Dictionary/AllOptions.cs +++ b/LMS.Common/Dictionary/AllOptions.cs @@ -1,4 +1,7 @@ -namespace LMS.Common.Dictionary; + +using LMS.Common.Enum; + +namespace LMS.Common.Dictionary; public class AllOptions { @@ -7,6 +10,7 @@ public class AllOptions { "all", [] }, { "tts", ["EdgeTTsRoles"] }, { "software", ["LaitoolHomePage", "LaitoolNotice", "LaitoolUpdateContent","LaitoolVersion"]}, - { "trial" , ["LaiToolTrialDays"] } + { "trial" , ["LaiToolTrialDays"] }, + { "image", [OptionKeyName.LaitoolFluxApiModelList] } }; } diff --git a/LMS.Common/Enum/OptionTypeEnum.cs b/LMS.Common/Enum/OptionTypeEnum.cs index d98c315..8648191 100644 --- a/LMS.Common/Enum/OptionTypeEnum.cs +++ b/LMS.Common/Enum/OptionTypeEnum.cs @@ -6,3 +6,11 @@ public enum OptionTypeEnum JSON = 2, Number = 3, } + +public static class OptionKeyName +{ + /// + /// LaiTool Flux API 模型列表的Option Key + /// + public const string LaitoolFluxApiModelList = "LaitoolFluxApiModelList"; +} diff --git a/LMS.Repository/Forward/SimpleTransferModel.cs b/LMS.Repository/Forward/SimpleTransferModel.cs index 4aaa430..6871c54 100644 --- a/LMS.Repository/Forward/SimpleTransferModel.cs +++ b/LMS.Repository/Forward/SimpleTransferModel.cs @@ -17,3 +17,16 @@ public class SimpleTransferModel /// public string dataString { get; set; } } + +public class GetTransferModel +{ + /// + /// GPT的完整地址 + /// + public string url { get; set; } + + /// + /// 对应的API站的使用的APIkey + /// + public string? APIKey { get; set; } +} diff --git a/LMS.service/Configuration/InitConfiguration/DatabaseConfiguration.cs b/LMS.service/Configuration/InitConfiguration/DatabaseConfiguration.cs index b03d0c8..9bcec72 100644 --- a/LMS.service/Configuration/InitConfiguration/DatabaseConfiguration.cs +++ b/LMS.service/Configuration/InitConfiguration/DatabaseConfiguration.cs @@ -1,12 +1,10 @@ -using LMS.DAO; +using LMS.Common.Enum; +using LMS.DAO; using LMS.Repository.DB; using LMS.Repository.Models.DB; using LMS.Tools.Extensions; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; -using LMS.Common.Enum; -using System.Threading; -using static LMS.Common.Enums.ResponseCodeEnum; namespace LMS.service.Configuration.InitConfiguration; @@ -102,7 +100,8 @@ 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} + new Options { Key = "LaiToolTrialDays", Value = "2" , Type = OptionTypeEnum.Number}, + new Options { Key = OptionKeyName.LaitoolFluxApiModelList, Value = "{}" , Type = OptionTypeEnum.JSON } ]; // 遍历所有的配置项,如果没有则添加 diff --git a/LMS.service/Controllers/ForwardController.cs b/LMS.service/Controllers/ForwardController.cs index 805d646..267dc55 100644 --- a/LMS.service/Controllers/ForwardController.cs +++ b/LMS.service/Controllers/ForwardController.cs @@ -82,7 +82,7 @@ public class ForwardController(ForwardWordService forwardWordService) : Controll #endregion - #region 普通转发接口,直接转发,不要系统数据 + #region Post 直接转发接口 /// /// 转发所有的OpenAI格式的AI请求 @@ -100,4 +100,18 @@ public class ForwardController(ForwardWordService forwardWordService) : Controll } #endregion + + #region Get直接转发接口 + + [HttpPost] + public async Task>> GetTransfer([FromBody] GetTransferModel getTransferModel) + { + if (!ModelState.IsValid) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError); + } + return await _forwardWordService.GetTransfer(getTransferModel); + + } + #endregion } diff --git a/LMS.service/Controllers/OptionsController.cs b/LMS.service/Controllers/OptionsController.cs index 7027675..9ae222b 100644 --- a/LMS.service/Controllers/OptionsController.cs +++ b/LMS.service/Controllers/OptionsController.cs @@ -61,12 +61,5 @@ namespace LMS.service.Controllers #endregion - //[HttpPost] - //[Authorize] - //public async Task>> GetOptions([FromBody] OptionsRequestModel request) - //{ - // return await _optionsService.GetOptions(request); - //} - } } diff --git a/LMS.service/Service/ForwardWordService.cs b/LMS.service/Service/ForwardWordService.cs index 674fdcf..219c22b 100644 --- a/LMS.service/Service/ForwardWordService.cs +++ b/LMS.service/Service/ForwardWordService.cs @@ -158,7 +158,7 @@ public class ForwardWordService(ApplicationDbContext context) #endregion - #region 简单的AI转发,OpenAI格式 + #region Post 转发接口 /// /// 简单的转发接口 /// @@ -171,20 +171,88 @@ public class ForwardWordService(ApplicationDbContext context) { // 开始拼接请求体 using HttpClient client = new(); - client.DefaultRequestHeaders.Add("Authorization", "Bearer " + request.APIKey); + if (request.url.StartsWith("https://api.laitool.net")) + { + //client.DefaultRequestHeaders.Add("Authorization", "Bearer " + request.APIKey); + client.DefaultRequestHeaders.Add("mj-api-secret", request.APIKey); + } + else + { + client.DefaultRequestHeaders.Add("Authorization", "Bearer " + request.APIKey); + } // 判断请求的url是不是满足条件 if (string.IsNullOrEmpty(request.url)) { throw new Exception("请求的url为空"); } - if (!request.url.StartsWith("https://ark.cn-beijing.volces.com") && !request.url.StartsWith("https://api.moonshot.cn") && !request.url.StartsWith("https://laitool.net") && !request.url.StartsWith("https://api.laitool.cc") && !request.url.StartsWith("https://laitool.cc")) + if (!request.url.StartsWith("https://ark.cn-beijing.volces.com") + && !request.url.StartsWith("https://api.moonshot.cn") + && !request.url.StartsWith("https://laitool.net") + && !request.url.StartsWith("https://api.laitool.net") + && !request.url.StartsWith("https://api.laitool.cc") + && !request.url.StartsWith("https://laitool.cc")) { - throw new Exception("请求的url不合法"); + throw new Exception("请求的url不支持转发"); } client.Timeout = Timeout.InfiniteTimeSpan; var response = await client.PostAsync(request.url, new StringContent(request.dataString, Encoding.UTF8, "application/json")); + // 判断返回的状态码 + if (response.StatusCode != HttpStatusCode.OK) + { + if (response.StatusCode == HttpStatusCode.Unauthorized) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ForwardWordFail, "responseCode: 401", "请求失败"); + } + // 读取响应体 + string responseContent = await response.Content.ReadAsStringAsync(); + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ForwardWordFail, responseContent, "请求失败"); + } + var content = await response.Content.ReadAsStringAsync(); + return APIResponseModel.CreateSuccessResponseModel(content); + } + catch (Exception e) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.SystemError, e.Message); + } + } + + #endregion + + #region Get直接转发接口 + internal async Task>> GetTransfer(GetTransferModel getTransferModel) + { + try + { + // 开始拼接请求体 + using HttpClient client = new(); + + if (getTransferModel.url.StartsWith("https://api.laitool.net")) + { + client.DefaultRequestHeaders.Add("mj-api-secret", getTransferModel.APIKey); + } + else + { + client.DefaultRequestHeaders.Add("Authorization", "Bearer " + getTransferModel.APIKey); + } + + // 判断请求的url是不是满足条件 + if (string.IsNullOrEmpty(getTransferModel.url)) + { + throw new Exception("请求的url为空"); + } + if (!getTransferModel.url.StartsWith("https://ark.cn-beijing.volces.com") + && !getTransferModel.url.StartsWith("https://api.moonshot.cn") + && !getTransferModel.url.StartsWith("https://laitool.net") + && !getTransferModel.url.StartsWith("https://api.laitool.net") + && !getTransferModel.url.StartsWith("https://api.laitool.cc") + && !getTransferModel.url.StartsWith("https://laitool.cc")) + { + throw new Exception("请求的url不支持转发"); + } + client.Timeout = Timeout.InfiniteTimeSpan; + var response = await client.GetAsync(getTransferModel.url); // 判断返回的状态码 if (response.StatusCode != HttpStatusCode.OK) { @@ -201,6 +269,5 @@ public class ForwardWordService(ApplicationDbContext context) return APIResponseModel.CreateErrorResponseModel(ResponseCode.SystemError, e.Message); } } - #endregion }