新增get转发接口

新增Laitool 设置中的绘图设置
This commit is contained in:
lq1405 2025-02-22 15:00:25 +08:00
parent 01055762ca
commit 76b57c5717
7 changed files with 118 additions and 20 deletions

View File

@ -1,4 +1,7 @@
namespace LMS.Common.Dictionary; 
using LMS.Common.Enum;
namespace LMS.Common.Dictionary;
public class AllOptions public class AllOptions
{ {
@ -7,6 +10,7 @@ public class AllOptions
{ "all", [] }, { "all", [] },
{ "tts", ["EdgeTTsRoles"] }, { "tts", ["EdgeTTsRoles"] },
{ "software", ["LaitoolHomePage", "LaitoolNotice", "LaitoolUpdateContent","LaitoolVersion"]}, { "software", ["LaitoolHomePage", "LaitoolNotice", "LaitoolUpdateContent","LaitoolVersion"]},
{ "trial" , ["LaiToolTrialDays"] } { "trial" , ["LaiToolTrialDays"] },
{ "image", [OptionKeyName.LaitoolFluxApiModelList] }
}; };
} }

View File

@ -6,3 +6,11 @@ public enum OptionTypeEnum
JSON = 2, JSON = 2,
Number = 3, Number = 3,
} }
public static class OptionKeyName
{
/// <summary>
/// LaiTool Flux API 模型列表的Option Key
/// </summary>
public const string LaitoolFluxApiModelList = "LaitoolFluxApiModelList";
}

View File

@ -17,3 +17,16 @@ public class SimpleTransferModel
/// </summary> /// </summary>
public string dataString { get; set; } public string dataString { get; set; }
} }
public class GetTransferModel
{
/// <summary>
/// GPT的完整地址
/// </summary>
public string url { get; set; }
/// <summary>
/// 对应的API站的使用的APIkey
/// </summary>
public string? APIKey { get; set; }
}

View File

@ -1,12 +1,10 @@
using LMS.DAO; using LMS.Common.Enum;
using LMS.DAO;
using LMS.Repository.DB; using LMS.Repository.DB;
using LMS.Repository.Models.DB; using LMS.Repository.Models.DB;
using LMS.Tools.Extensions; using LMS.Tools.Extensions;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using LMS.Common.Enum;
using System.Threading;
using static LMS.Common.Enums.ResponseCodeEnum;
namespace LMS.service.Configuration.InitConfiguration; 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 = "LaitoolUpdateContent", Value = string.Empty, Type = OptionTypeEnum.String },
new Options { Key = "LaitoolNotice", 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 = "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 }
]; ];
// 遍历所有的配置项,如果没有则添加 // 遍历所有的配置项,如果没有则添加

View File

@ -82,7 +82,7 @@ public class ForwardController(ForwardWordService forwardWordService) : Controll
#endregion #endregion
#region #region Post
/// <summary> /// <summary>
/// 转发所有的OpenAI格式的AI请求 /// 转发所有的OpenAI格式的AI请求
@ -100,4 +100,18 @@ public class ForwardController(ForwardWordService forwardWordService) : Controll
} }
#endregion #endregion
#region Get直接转发接口
[HttpPost]
public async Task<ActionResult<APIResponseModel<object>>> GetTransfer([FromBody] GetTransferModel getTransferModel)
{
if (!ModelState.IsValid)
{
return APIResponseModel<object>.CreateErrorResponseModel(ResponseCode.ParameterError);
}
return await _forwardWordService.GetTransfer(getTransferModel);
}
#endregion
} }

View File

@ -61,12 +61,5 @@ namespace LMS.service.Controllers
#endregion #endregion
//[HttpPost]
//[Authorize]
//public async Task<ActionResult<APIResponseModel<Options>>> GetOptions([FromBody] OptionsRequestModel request)
//{
// return await _optionsService.GetOptions(request);
//}
} }
} }

View File

@ -158,7 +158,7 @@ public class ForwardWordService(ApplicationDbContext context)
#endregion #endregion
#region AI转发OpenAI格式 #region Post
/// <summary> /// <summary>
/// 简单的转发接口 /// 简单的转发接口
/// </summary> /// </summary>
@ -171,20 +171,88 @@ public class ForwardWordService(ApplicationDbContext context)
{ {
// 开始拼接请求体 // 开始拼接请求体
using HttpClient client = new(); using HttpClient client = new();
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); client.DefaultRequestHeaders.Add("Authorization", "Bearer " + request.APIKey);
}
// 判断请求的url是不是满足条件 // 判断请求的url是不是满足条件
if (string.IsNullOrEmpty(request.url)) if (string.IsNullOrEmpty(request.url))
{ {
throw new Exception("请求的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; client.Timeout = Timeout.InfiniteTimeSpan;
var response = await client.PostAsync(request.url, new StringContent(request.dataString, Encoding.UTF8, "application/json")); 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<object>.CreateErrorResponseModel(ResponseCode.ForwardWordFail, "responseCode: 401", "请求失败");
}
// 读取响应体
string responseContent = await response.Content.ReadAsStringAsync();
return APIResponseModel<object>.CreateErrorResponseModel(ResponseCode.ForwardWordFail, responseContent, "请求失败");
}
var content = await response.Content.ReadAsStringAsync();
return APIResponseModel<object>.CreateSuccessResponseModel(content);
}
catch (Exception e)
{
return APIResponseModel<object>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
}
}
#endregion
#region Get直接转发接口
internal async Task<ActionResult<APIResponseModel<object>>> 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) if (response.StatusCode != HttpStatusCode.OK)
{ {
@ -201,6 +269,5 @@ public class ForwardWordService(ApplicationDbContext context)
return APIResponseModel<object>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message); return APIResponseModel<object>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
} }
} }
#endregion #endregion
} }