mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-04-16 22:26:37 +08:00
Compare commits
159 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8dcbfcad33 | ||
|
|
f64fd43951 | ||
|
|
551597765c | ||
|
|
bfda33280a | ||
|
|
8d0411f1f4 | ||
|
|
3995d4acab | ||
|
|
6ff5727156 | ||
|
|
f654386dfe | ||
|
|
c03ef82643 | ||
|
|
525545329b | ||
|
|
755cb6f509 | ||
|
|
55469708f0 | ||
|
|
94c52c62fe | ||
|
|
37b4709d76 | ||
|
|
86555af6ce | ||
|
|
ddb00879f4 | ||
|
|
2d0ca08314 | ||
|
|
b78ecf27d5 | ||
|
|
02a5f69958 | ||
|
|
cf5bf746ef | ||
|
|
0a5e40ee25 | ||
|
|
51a266ef58 | ||
|
|
1f0901c90c | ||
|
|
a725c06396 | ||
|
|
54547f0d7c | ||
|
|
afe9c8bcae | ||
|
|
688d93e5c1 | ||
|
|
4c65b2398d | ||
|
|
41435f1aa3 | ||
|
|
20206bbc44 | ||
|
|
f2dc0d1825 | ||
|
|
51b4d1b072 | ||
|
|
9180799e4e | ||
|
|
9788b9182b | ||
|
|
260b9a4795 | ||
|
|
9380e3daa8 | ||
|
|
bb894e14a4 | ||
|
|
b492d82442 | ||
|
|
5eaffe2ec2 | ||
|
|
d7bcad9da7 | ||
|
|
04e11d15e2 | ||
|
|
97e3dc5eed | ||
|
|
695bd56a27 | ||
|
|
7919383be3 | ||
|
|
d6cc3c5d96 | ||
|
|
19f0d05a69 | ||
|
|
3c5e575e9b | ||
|
|
f875617de1 | ||
|
|
9976e8a6e2 | ||
|
|
38e8cbc5ca | ||
|
|
d95c14c903 | ||
|
|
ffb2f2fb4c | ||
|
|
4bd2fc357d | ||
|
|
da23d17af8 | ||
|
|
c1a6046107 | ||
|
|
2ec7b5f4fd | ||
|
|
d21f61646a | ||
|
|
eecdf442fb | ||
|
|
8e8338743d | ||
|
|
1b4c3cbb8d | ||
|
|
b7756e2112 | ||
|
|
cb49059e84 | ||
|
|
690cabfd96 | ||
|
|
4521212a90 | ||
|
|
771ecd9d81 | ||
|
|
94834f45c3 | ||
|
|
a9b2979a21 | ||
|
|
22ac150acd | ||
|
|
17337b8d78 | ||
|
|
09fb43ee14 | ||
|
|
477c0e3f2c | ||
|
|
2e4f520dac | ||
|
|
067b25b9af | ||
|
|
36370c215d | ||
|
|
e24731acfe | ||
|
|
927e9df7de | ||
|
|
114b41144e | ||
|
|
5019a36138 | ||
|
|
e15eb6149b | ||
|
|
9d401a9c93 | ||
|
|
eacf86e118 | ||
|
|
c4b631c815 | ||
|
|
fb25e75a3a | ||
|
|
e9099bbe04 | ||
|
|
f02fb91175 | ||
|
|
5beef22269 | ||
|
|
933cbb91d8 | ||
|
|
efd917d184 | ||
|
|
e906208f4a | ||
|
|
cf137f6307 | ||
|
|
e6b991fe86 | ||
|
|
3e75792e43 | ||
|
|
dd3f6325bb | ||
|
|
108ba348f6 | ||
|
|
bcdcec40e0 | ||
|
|
2ce8baea42 | ||
|
|
c6425ca206 | ||
|
|
acb359ec33 | ||
|
|
a1395d9a33 | ||
|
|
609de29e71 | ||
|
|
2efed4f4a5 | ||
|
|
aec90ec9d6 | ||
|
|
1aaff2942d | ||
|
|
cdbfc5383d | ||
|
|
f302555e0c | ||
|
|
86c5890476 | ||
|
|
a13ee395c7 | ||
|
|
9abcd72aca | ||
|
|
4ddea6d468 | ||
|
|
867a2dc861 | ||
|
|
4a72e3fa0d | ||
|
|
8b4371aabb | ||
|
|
799dd08ec0 | ||
|
|
c5c22224cf | ||
|
|
2dae47e85c | ||
|
|
375dd4f797 | ||
|
|
acb2db8397 | ||
|
|
b7a3e76d0b | ||
|
|
48150b712a | ||
|
|
6db9dfc308 | ||
|
|
2d6c1f3c46 | ||
|
|
161e10d2d1 | ||
|
|
a9a2a91183 | ||
|
|
1c9a6f108e | ||
|
|
d6adf9b736 | ||
|
|
959eb3f782 | ||
|
|
7a53e0c90c | ||
|
|
533b87fc5b | ||
|
|
15713cf7fe | ||
|
|
31dc756868 | ||
|
|
52f6b6130f | ||
|
|
16945b3d5b | ||
|
|
bdc664fc44 | ||
|
|
9555ef10e0 | ||
|
|
49e6cb26fc | ||
|
|
3ace29e692 | ||
|
|
aa9dd0129b | ||
|
|
1464271fbd | ||
|
|
754f145559 | ||
|
|
6afd0cb955 | ||
|
|
d32906702a | ||
|
|
9bcdaf6bd8 | ||
|
|
db82a8cf08 | ||
|
|
85bd1ce8d6 | ||
|
|
1cc5f2a14f | ||
|
|
d9997eeb28 | ||
|
|
06e77aa8fd | ||
|
|
e9e2228f6e | ||
|
|
d516a381d0 | ||
|
|
4e792ba976 | ||
|
|
f90d3871fa | ||
|
|
6005b9329d | ||
|
|
9d4b3e7d0c | ||
|
|
72795382a1 | ||
|
|
35cdff2afa | ||
|
|
dcf547f513 | ||
|
|
8660d45f36 | ||
|
|
63dd55e7a4 | ||
|
|
40cd89f90c |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -265,6 +265,7 @@ src/Acme.BookStore.Blazor.Server.Tiered/Logs/*
|
|||||||
**/wwwroot/libs/*
|
**/wwwroot/libs/*
|
||||||
public
|
public
|
||||||
dist
|
dist
|
||||||
|
dist - 副本
|
||||||
.vscode
|
.vscode
|
||||||
/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.Development.json
|
/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.Development.json
|
||||||
/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.Production.json
|
/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.Production.json
|
||||||
|
|||||||
10
README.md
10
README.md
@@ -7,6 +7,12 @@
|
|||||||
[](https://gitee.com/ccnetcore/Yi)
|
[](https://gitee.com/ccnetcore/Yi)
|
||||||
[](https://gitee.com/ccnetcore/Yi)
|
[](https://gitee.com/ccnetcore/Yi)
|
||||||
|
|
||||||
|
本项目 CDN 加速及安全防护由 Tencent EdgeOne 赞助
|
||||||
|
|
||||||
|
[亚洲最佳CDN、边缘和安全解决方案 - Tencent EdgeOne](https://edgeone.ai/zh?from=github)
|
||||||
|
|
||||||
|
<img src="readme/edgeone.png"/>
|
||||||
|
|
||||||
[English](README-en.md) | 简体中文
|
[English](README-en.md) | 简体中文
|
||||||
****
|
****
|
||||||
## 🍍 简介:
|
## 🍍 简介:
|
||||||
@@ -60,9 +66,9 @@ bbs前端:`docker run -d --name yi.bbs -p 18001:18001 -v /home/Yi/Yi.Bbs.Vue3/
|
|||||||
|
|
||||||
Yi社区官网网址(Bbs社区正式):[ccnetcore.com](https://ccnetcore.com) (已上线,欢迎加入)
|
Yi社区官网网址(Bbs社区正式):[ccnetcore.com](https://ccnetcore.com) (已上线,欢迎加入)
|
||||||
|
|
||||||
Rbac后台演示地址:https://ccnetcore.com:1000 (用户cc、密码123456)
|
Rbac后台演示地址:https://data.ccnetcore.com:1000 (用户cc、密码123456)
|
||||||
|
|
||||||
Pure后台演示地址:https://ccnetcore.com:1001 (用户cc、密码123456)
|
Pure后台演示地址:https://data.ccnetcore.com:1001 (用户cc、密码123456)
|
||||||
|
|
||||||
## 🍏 支持:
|
## 🍏 支持:
|
||||||
|
|
||||||
|
|||||||
10
Yi.Abp.Net8/.claude/settings.local.json
Normal file
10
Yi.Abp.Net8/.claude/settings.local.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"permissions": {
|
||||||
|
"allow": [
|
||||||
|
"Bash(dotnet build \"E:\\code\\github\\Yi\\Yi.Abp.Net8\\module\\ai-hub\\Yi.Framework.AiHub.Application\\Yi.Framework.AiHub.Application.csproj\" --no-restore)",
|
||||||
|
"Read(//e/code/github/Yi/Yi.Ai.Vue3/**)"
|
||||||
|
],
|
||||||
|
"deny": [],
|
||||||
|
"ask": []
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
// MIT 许可证
|
// MIT 许可证
|
||||||
//
|
//
|
||||||
// 版权 © 2020-present 百小僧, 百签科技(广东)有限公司 和所有贡献者
|
// 版权 © 2020-present 百小僧, 百签科技(广东)有限公司 和所有贡献者
|
||||||
//
|
//
|
||||||
@@ -17,8 +17,9 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using Volo.Abp.AspNetCore.Mvc;
|
using Volo.Abp.AspNetCore.Mvc;
|
||||||
using Volo.Abp.DependencyInjection;
|
using Volo.Abp.Validation;
|
||||||
using Yi.Framework.Core.Extensions;
|
using Yi.Framework.Core.Extensions;
|
||||||
|
|
||||||
namespace Yi.Framework.AspNetCore.UnifyResult.Fiters;
|
namespace Yi.Framework.AspNetCore.UnifyResult.Fiters;
|
||||||
@@ -35,7 +36,6 @@ public sealed class FriendlyExceptionFilter : IAsyncExceptionFilter
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task OnExceptionAsync(ExceptionContext context)
|
public async Task OnExceptionAsync(ExceptionContext context)
|
||||||
{
|
{
|
||||||
|
|
||||||
// 排除 WebSocket 请求处理
|
// 排除 WebSocket 请求处理
|
||||||
if (context.HttpContext.IsWebSocketRequest()) return;
|
if (context.HttpContext.IsWebSocketRequest()) return;
|
||||||
|
|
||||||
@@ -44,16 +44,19 @@ public sealed class FriendlyExceptionFilter : IAsyncExceptionFilter
|
|||||||
|
|
||||||
// 解析异常信息
|
// 解析异常信息
|
||||||
var exceptionMetadata = GetExceptionMetadata(context);
|
var exceptionMetadata = GetExceptionMetadata(context);
|
||||||
|
var unifyResult = context.GetRequiredService<IUnifyResultProvider>();
|
||||||
IUnifyResultProvider unifyResult = context.GetRequiredService<IUnifyResultProvider>();
|
|
||||||
// 执行规范化异常处理
|
// 执行规范化异常处理
|
||||||
context.Result = unifyResult.OnException(context, exceptionMetadata);
|
context.Result = unifyResult.OnException(context, exceptionMetadata);
|
||||||
|
|
||||||
// 创建日志记录器
|
// 创建日志记录器
|
||||||
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<FriendlyExceptionFilter>>();
|
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<FriendlyExceptionFilter>>();
|
||||||
|
|
||||||
|
var errorMsg = "";
|
||||||
|
if (exceptionMetadata.Errors != null) errorMsg = "\n" + JsonConvert.SerializeObject(exceptionMetadata.Errors);
|
||||||
|
|
||||||
|
|
||||||
// 记录拦截日常
|
// 记录拦截日常
|
||||||
logger.LogError(context.Exception, context.Exception.Message);
|
logger.LogError(context.Exception, context.Exception.Message + errorMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -74,22 +77,25 @@ public sealed class FriendlyExceptionFilter : IAsyncExceptionFilter
|
|||||||
// 判断是否是 ExceptionContext 或者 ActionExecutedContext
|
// 判断是否是 ExceptionContext 或者 ActionExecutedContext
|
||||||
var exception = context is ExceptionContext exContext
|
var exception = context is ExceptionContext exContext
|
||||||
? exContext.Exception
|
? exContext.Exception
|
||||||
: (
|
: context is ActionExecutedContext edContext
|
||||||
context is ActionExecutedContext edContext
|
|
||||||
? edContext.Exception
|
? edContext.Exception
|
||||||
: default
|
: default;
|
||||||
);
|
|
||||||
|
if (exception is AbpValidationException validationException)
|
||||||
|
{
|
||||||
|
errors = validationException.ValidationErrors;
|
||||||
|
isValidationException = true;
|
||||||
|
}
|
||||||
|
|
||||||
// 判断是否是友好异常
|
// 判断是否是友好异常
|
||||||
if (exception is UserFriendlyException friendlyException)
|
if (exception is UserFriendlyException friendlyException)
|
||||||
{
|
{
|
||||||
int statusCode2 = 500;
|
var statusCode2 = 500;
|
||||||
int.TryParse(friendlyException.Code, out statusCode2);
|
int.TryParse(friendlyException.Code, out statusCode2);
|
||||||
isFriendlyException = true;
|
isFriendlyException = true;
|
||||||
errorCode = friendlyException.Code;
|
errorCode = friendlyException.Code;
|
||||||
originErrorCode = friendlyException.Code;
|
originErrorCode = friendlyException.Code;
|
||||||
statusCode = statusCode2 == 0 ? 403 : statusCode2;
|
statusCode = statusCode2 == 0 ? 403 : statusCode2;
|
||||||
isValidationException = false;
|
|
||||||
errors = friendlyException.Message;
|
errors = friendlyException.Message;
|
||||||
data = friendlyException.Data;
|
data = friendlyException.Data;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
using Volo.Abp.AspNetCore.Mvc.ExceptionHandling;
|
using Volo.Abp.AspNetCore.Mvc.ExceptionHandling;
|
||||||
|
using Volo.Abp.AspNetCore.Mvc.Response;
|
||||||
using Yi.Framework.AspNetCore.UnifyResult.Fiters;
|
using Yi.Framework.AspNetCore.UnifyResult.Fiters;
|
||||||
|
|
||||||
namespace Yi.Framework.AspNetCore.UnifyResult;
|
namespace Yi.Framework.AspNetCore.UnifyResult;
|
||||||
@@ -20,9 +21,10 @@ public static class UnifyResultExtensions
|
|||||||
services.AddTransient<FriendlyExceptionFilter>();
|
services.AddTransient<FriendlyExceptionFilter>();
|
||||||
services.AddMvc(options =>
|
services.AddMvc(options =>
|
||||||
{
|
{
|
||||||
|
options.Filters.RemoveAll(x => (x as ServiceFilterAttribute)?.ServiceType == typeof(AbpExceptionFilter));
|
||||||
|
options.Filters.RemoveAll(x => (x as ServiceFilterAttribute)?.ServiceType == typeof(AbpNoContentActionFilter));
|
||||||
options.Filters.AddService<SucceededUnifyResultFilter>(99);
|
options.Filters.AddService<SucceededUnifyResultFilter>(99);
|
||||||
options.Filters.AddService<FriendlyExceptionFilter>(100);
|
options.Filters.AddService<FriendlyExceptionFilter>(100);
|
||||||
options.Filters.RemoveAll(x => (x as ServiceFilterAttribute)?.ServiceType == typeof(AbpExceptionFilter));
|
|
||||||
});
|
});
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -199,11 +199,11 @@ namespace Yi.Framework.Ddd.Application
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 批量删除实体
|
/// 批量删除实体
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="ids">实体ID集合</param>
|
/// <param name="id">实体ID集合</param>
|
||||||
[RemoteService(isEnabled: true)]
|
[RemoteService(isEnabled: true)]
|
||||||
public virtual async Task DeleteAsync(IEnumerable<TKey> ids)
|
public virtual async Task DeleteAsync(IEnumerable<TKey> id)
|
||||||
{
|
{
|
||||||
await Repository.DeleteManyAsync(ids);
|
await Repository.DeleteManyAsync(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Volo.Abp.Modularity;
|
using Volo.Abp.Modularity;
|
||||||
using Volo.Abp.ObjectMapping;
|
using Volo.Abp.ObjectMapping;
|
||||||
using Yi.Framework.Core;
|
using Yi.Framework.Core;
|
||||||
|
using Mapster;
|
||||||
|
|
||||||
namespace Yi.Framework.Mapster
|
namespace Yi.Framework.Mapster
|
||||||
{
|
{
|
||||||
@@ -22,7 +23,8 @@ namespace Yi.Framework.Mapster
|
|||||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||||
{
|
{
|
||||||
var services = context.Services;
|
var services = context.Services;
|
||||||
|
// 扫描并注册所有映射配置
|
||||||
|
TypeAdapterConfig.GlobalSettings.Scan(AppDomain.CurrentDomain.GetAssemblies());
|
||||||
// 注册Mapster相关服务
|
// 注册Mapster相关服务
|
||||||
services.AddTransient<IAutoObjectMappingProvider, MapsterAutoObjectMappingProvider>();
|
services.AddTransient<IAutoObjectMappingProvider, MapsterAutoObjectMappingProvider>();
|
||||||
services.AddTransient<IObjectMapper, MapsterObjectMapper>();
|
services.AddTransient<IObjectMapper, MapsterObjectMapper>();
|
||||||
|
|||||||
@@ -60,8 +60,8 @@ namespace Yi.Framework.SqlSugarCore.Abstractions
|
|||||||
public bool EnabledSaasMultiTenancy { get; set; } = false;
|
public bool EnabledSaasMultiTenancy { get; set; } = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 并发乐观锁异常,否则不处理
|
/// 是否开启更新并发乐观锁
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool EnabledConcurrencyException { get; set; } = true;
|
public bool EnabledConcurrencyException { get;set; } = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,7 @@
|
|||||||
using System.Linq;
|
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Text;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Nito.AsyncEx;
|
using Nito.AsyncEx;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
using Volo.Abp;
|
|
||||||
using Volo.Abp.Auditing;
|
|
||||||
using Volo.Abp.Data;
|
using Volo.Abp.Data;
|
||||||
using Volo.Abp.DependencyInjection;
|
using Volo.Abp.DependencyInjection;
|
||||||
using Volo.Abp.Domain.Entities;
|
using Volo.Abp.Domain.Entities;
|
||||||
@@ -17,18 +12,19 @@ using Yi.Framework.SqlSugarCore.Abstractions;
|
|||||||
|
|
||||||
namespace Yi.Framework.SqlSugarCore.Repositories
|
namespace Yi.Framework.SqlSugarCore.Repositories
|
||||||
{
|
{
|
||||||
public class SqlSugarRepository<TEntity> : ISqlSugarRepository<TEntity>, IRepository<TEntity>
|
public class SqlSugarRepository<TEntity> : ISqlSugarRepository<TEntity>, IRepository<TEntity> where TEntity : class, IEntity, new()
|
||||||
where TEntity : class, IEntity, new()
|
|
||||||
{
|
{
|
||||||
|
[Obsolete("使用GetDbContextAsync()")]
|
||||||
public ISqlSugarClient _Db => AsyncContext.Run(async () => await GetDbContextAsync());
|
public ISqlSugarClient _Db => AsyncContext.Run(async () => await GetDbContextAsync());
|
||||||
|
|
||||||
|
[Obsolete("使用AsQueryable()")]
|
||||||
public ISugarQueryable<TEntity> _DbQueryable => _Db.Queryable<TEntity>();
|
public ISugarQueryable<TEntity> _DbQueryable => _Db.Queryable<TEntity>();
|
||||||
|
|
||||||
private readonly ISugarDbContextProvider<ISqlSugarDbContext> _dbContextProvider;
|
private readonly ISugarDbContextProvider<ISqlSugarDbContext> _dbContextProvider;
|
||||||
|
|
||||||
public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
|
public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
|
||||||
protected DbConnOptions? Options => LazyServiceProvider?.LazyGetService<IOptions<DbConnOptions>>().Value;
|
|
||||||
|
|
||||||
|
protected DbConnOptions? Options => LazyServiceProvider?.LazyGetService<IOptions<DbConnOptions>>().Value;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步查询执行器
|
/// 异步查询执行器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -64,26 +60,22 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
|
|
||||||
#region Abp模块
|
#region Abp模块
|
||||||
|
|
||||||
public virtual async Task<TEntity?> FindAsync(Expression<Func<TEntity, bool>> predicate,
|
public virtual async Task<TEntity?> FindAsync(Expression<Func<TEntity, bool>> predicate, bool includeDetails = true, CancellationToken cancellationToken = default)
|
||||||
bool includeDetails = true, CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
return await GetFirstAsync(predicate);
|
return await GetFirstAsync(predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate,
|
public virtual async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate, bool includeDetails = true, CancellationToken cancellationToken = default)
|
||||||
bool includeDetails = true, CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
return await GetFirstAsync(predicate);
|
return await GetFirstAsync(predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool autoSave = false,
|
public virtual async Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
await this.DeleteAsync(predicate);
|
await this.DeleteAsync(predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate,
|
public virtual async Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
await this.DeleteAsync(predicate);
|
await this.DeleteAsync(predicate);
|
||||||
}
|
}
|
||||||
@@ -113,51 +105,43 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate,
|
public virtual async Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate, bool includeDetails = false, CancellationToken cancellationToken = default)
|
||||||
bool includeDetails = false, CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
return await GetListAsync(predicate);
|
return await GetListAsync(predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false,
|
public virtual async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
return await InsertReturnEntityAsync(entity);
|
return await InsertReturnEntityAsync(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false,
|
public virtual async Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
await InsertRangeAsync(entities.ToList());
|
await InsertRangeAsync(entities.ToList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = false,
|
public virtual async Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
await UpdateAsync(entity);
|
await UpdateAsync(entity);
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task UpdateManyAsync(IEnumerable<TEntity> entities, bool autoSave = false,
|
public virtual async Task UpdateManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
await UpdateRangeAsync(entities.ToList());
|
await UpdateRangeAsync(entities.ToList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task DeleteAsync(TEntity entity, bool autoSave = false,
|
public virtual async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
await DeleteAsync(entity);
|
await DeleteAsync(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task DeleteManyAsync(IEnumerable<TEntity> entities, bool autoSave = false,
|
public virtual async Task DeleteManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
await DeleteAsync(entities.ToList());
|
await DeleteAsync(entities.ToList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<List<TEntity>> GetListAsync(bool includeDetails = false,
|
public virtual async Task<List<TEntity>> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
return await GetListAsync();
|
return await GetListAsync();
|
||||||
}
|
}
|
||||||
@@ -167,17 +151,14 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
return await this.CountAsync(_=>true);
|
return await this.CountAsync(_=>true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<List<TEntity>> GetPagedListAsync(int skipCount, int maxResultCount, string sorting,
|
public virtual async Task<List<TEntity>> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, bool includeDetails = false, CancellationToken cancellationToken = default)
|
||||||
bool includeDetails = false, CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
return await GetPageListAsync(_ => true, skipCount, maxResultCount);
|
return await GetPageListAsync(_ => true, skipCount, maxResultCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region 内置DB快捷操作
|
#region 内置DB快捷操作
|
||||||
|
|
||||||
public virtual async Task<IDeleteable<TEntity>> AsDeleteable()
|
public virtual async Task<IDeleteable<TEntity>> AsDeleteable()
|
||||||
{
|
{
|
||||||
return (await GetDbSimpleClientAsync()).AsDeleteable();
|
return (await GetDbSimpleClientAsync()).AsDeleteable();
|
||||||
@@ -232,11 +213,9 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
{
|
{
|
||||||
return (await GetDbSimpleClientAsync()).AsUpdateable(updateObjs);
|
return (await GetDbSimpleClientAsync()).AsUpdateable(updateObjs);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region SimpleClient模块
|
#region SimpleClient模块
|
||||||
|
|
||||||
public virtual async Task<int> CountAsync(Expression<Func<TEntity, bool>> whereExpression)
|
public virtual async Task<int> CountAsync(Expression<Func<TEntity, bool>> whereExpression)
|
||||||
{
|
{
|
||||||
return await (await GetDbSimpleClientAsync()).CountAsync(whereExpression);
|
return await (await GetDbSimpleClientAsync()).CountAsync(whereExpression);
|
||||||
@@ -253,6 +232,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
{
|
{
|
||||||
return await (await GetDbSimpleClientAsync()).DeleteAsync(deleteObj);
|
return await (await GetDbSimpleClientAsync()).DeleteAsync(deleteObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<bool> DeleteAsync(List<TEntity> deleteObjs)
|
public virtual async Task<bool> DeleteAsync(List<TEntity> deleteObjs)
|
||||||
@@ -272,13 +252,13 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
{
|
{
|
||||||
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
|
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
|
||||||
{
|
{
|
||||||
return await (await GetDbSimpleClientAsync()).AsUpdateable()
|
return await (await GetDbSimpleClientAsync()).AsUpdateable().SetColumns(nameof(ISoftDelete.IsDeleted), true).Where(whereExpression).ExecuteCommandAsync() > 0;
|
||||||
.SetColumns(nameof(ISoftDelete.IsDeleted), true).Where(whereExpression).ExecuteCommandAsync() > 0;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return await (await GetDbSimpleClientAsync()).DeleteAsync(whereExpression);
|
return await (await GetDbSimpleClientAsync()).DeleteAsync(whereExpression);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<bool> DeleteByIdAsync(dynamic id)
|
public virtual async Task<bool> DeleteByIdAsync(dynamic id)
|
||||||
@@ -286,11 +266,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
|
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
|
||||||
{
|
{
|
||||||
var entity = await GetByIdAsync(id);
|
var entity = await GetByIdAsync(id);
|
||||||
if (entity is null)
|
if (entity == null) return false;
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//反射赋值
|
//反射赋值
|
||||||
ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, entity);
|
ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, entity);
|
||||||
return await UpdateAsync(entity);
|
return await UpdateAsync(entity);
|
||||||
@@ -311,7 +287,6 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//反射赋值
|
//反射赋值
|
||||||
entities.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e));
|
entities.ForEach(e => ReflexHelper.SetModelValue(nameof(ISoftDelete.IsDeleted), true, e));
|
||||||
return await UpdateRangeAsync(entities);
|
return await UpdateRangeAsync(entities);
|
||||||
@@ -320,6 +295,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
{
|
{
|
||||||
return await (await GetDbSimpleClientAsync()).DeleteByIdAsync(ids);
|
return await (await GetDbSimpleClientAsync()).DeleteByIdAsync(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<TEntity> GetByIdAsync(dynamic id)
|
public virtual async Task<TEntity> GetByIdAsync(dynamic id)
|
||||||
@@ -328,6 +304,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public virtual async Task<TEntity> GetFirstAsync(Expression<Func<TEntity, bool>> whereExpression)
|
public virtual async Task<TEntity> GetFirstAsync(Expression<Func<TEntity, bool>> whereExpression)
|
||||||
{
|
{
|
||||||
return await (await GetDbSimpleClientAsync()).GetFirstAsync(whereExpression);
|
return await (await GetDbSimpleClientAsync()).GetFirstAsync(whereExpression);
|
||||||
@@ -343,19 +320,14 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
return await (await GetDbSimpleClientAsync()).GetListAsync(whereExpression);
|
return await (await GetDbSimpleClientAsync()).GetListAsync(whereExpression);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<List<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression,
|
public virtual async Task<List<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression, int pageNum, int pageSize)
|
||||||
int pageNum, int pageSize)
|
|
||||||
{
|
{
|
||||||
return await (await GetDbSimpleClientAsync()).GetPageListAsync(whereExpression,
|
return await (await AsQueryable()).Where(whereExpression).ToPageListAsync(pageNum, pageSize);
|
||||||
new PageModel() { PageIndex = pageNum, PageSize = pageSize });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<List<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression,
|
public virtual async Task<List<TEntity>> GetPageListAsync(Expression<Func<TEntity, bool>> whereExpression, int pageNum, int pageSize, Expression<Func<TEntity, object>>? orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
|
||||||
int pageNum, int pageSize, Expression<Func<TEntity, object>>? orderByExpression = null,
|
|
||||||
OrderByType orderByType = OrderByType.Asc)
|
|
||||||
{
|
{
|
||||||
return await (await GetDbSimpleClientAsync()).GetPageListAsync(whereExpression,
|
return await (await AsQueryable()).Where(whereExpression) .OrderBy( orderByExpression,orderByType).ToPageListAsync(pageNum, pageSize);
|
||||||
new PageModel { PageIndex = pageNum, PageSize = pageSize }, orderByExpression, orderByType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<TEntity> GetSingleAsync(Expression<Func<TEntity, bool>> whereExpression)
|
public virtual async Task<TEntity> GetSingleAsync(Expression<Func<TEntity, bool>> whereExpression)
|
||||||
@@ -409,10 +381,10 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<bool> UpdateAsync(TEntity updateObj)
|
public virtual async Task<bool> UpdateAsync(TEntity updateObj)
|
||||||
{
|
|
||||||
if (typeof(TEntity).IsAssignableTo<IHasConcurrencyStamp>()) //带版本号乐观锁更新
|
|
||||||
{
|
{
|
||||||
if (Options is not null && Options.EnabledConcurrencyException)
|
if (Options is not null && Options.EnabledConcurrencyException)
|
||||||
|
{
|
||||||
|
if (typeof(TEntity).IsAssignableTo<IHasConcurrencyStamp>()) //带版本号乐观锁更新
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -426,24 +398,18 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
$"{ex.Message}[更新失败:ConcurrencyStamp不是最新版本],entityInfo:{updateObj}", ex);
|
$"{ex.Message}[更新失败:ConcurrencyStamp不是最新版本],entityInfo:{updateObj}", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
int num = await (await GetDbSimpleClientAsync())
|
|
||||||
.Context.Updateable(updateObj).ExecuteCommandAsync();
|
|
||||||
return num > 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return await (await GetDbSimpleClientAsync()).UpdateAsync(updateObj);
|
return await (await GetDbSimpleClientAsync()).UpdateAsync(updateObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<bool> UpdateAsync(Expression<Func<TEntity, TEntity>> columns,
|
public virtual async Task<bool> UpdateAsync(Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> whereExpression)
|
||||||
Expression<Func<TEntity, bool>> whereExpression)
|
|
||||||
{
|
{
|
||||||
return await (await GetDbSimpleClientAsync()).UpdateAsync(columns, whereExpression);
|
return await (await GetDbSimpleClientAsync()).UpdateAsync(columns, whereExpression);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public virtual async Task<bool> UpdateRangeAsync(List<TEntity> updateObjs)
|
public virtual async Task<bool> UpdateRangeAsync(List<TEntity> updateObjs)
|
||||||
{
|
{
|
||||||
return await (await GetDbSimpleClientAsync()).UpdateRangeAsync(updateObjs);
|
return await (await GetDbSimpleClientAsync()).UpdateRangeAsync(updateObjs);
|
||||||
@@ -452,34 +418,28 @@ namespace Yi.Framework.SqlSugarCore.Repositories
|
|||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SqlSugarRepository<TEntity, TKey> : SqlSugarRepository<TEntity>, ISqlSugarRepository<TEntity, TKey>,
|
public class SqlSugarRepository<TEntity, TKey> : SqlSugarRepository<TEntity>, ISqlSugarRepository<TEntity, TKey>, IRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
|
||||||
IRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
|
|
||||||
{
|
{
|
||||||
public SqlSugarRepository(ISugarDbContextProvider<ISqlSugarDbContext> dbContextProvider) : base(
|
public SqlSugarRepository(ISugarDbContextProvider<ISqlSugarDbContext> sugarDbContextProvider) : base(sugarDbContextProvider)
|
||||||
dbContextProvider)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task DeleteAsync(TKey id, bool autoSave = false,
|
public virtual async Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
await DeleteByIdAsync(id);
|
await DeleteByIdAsync(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task DeleteManyAsync(IEnumerable<TKey> ids, bool autoSave = false,
|
public virtual async Task DeleteManyAsync(IEnumerable<TKey> ids, bool autoSave = false, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
await DeleteByIdsAsync(ids.Select(x => (object)x).ToArray());
|
await DeleteByIdsAsync(ids.Select(x => (object)x).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<TEntity?> FindAsync(TKey id, bool includeDetails = true,
|
public virtual async Task<TEntity?> FindAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
return await GetByIdAsync(id);
|
return await GetByIdAsync(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<TEntity> GetAsync(TKey id, bool includeDetails = true,
|
public virtual async Task<TEntity> GetAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default)
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
{
|
||||||
return await GetByIdAsync(id);
|
return await GetByIdAsync(id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,19 +18,4 @@ public class AiUserRoleMenuDto:UserRoleMenuDto
|
|||||||
/// VIP到期时间
|
/// VIP到期时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DateTime? VipExpireTime { get; set; }
|
public DateTime? VipExpireTime { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 尊享包总Token数
|
|
||||||
/// </summary>
|
|
||||||
public long PremiumTotalTokens { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 尊享包已使用Token数
|
|
||||||
/// </summary>
|
|
||||||
public long PremiumUsedTokens { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 尊享包剩余Token数
|
|
||||||
/// </summary>
|
|
||||||
public long PremiumRemainingTokens { get; set; }
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 公告缓存 DTO
|
||||||
|
/// </summary>
|
||||||
|
public class AnnouncementCacheDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 版本号
|
||||||
|
/// </summary>
|
||||||
|
public string Version { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 公告日志列表
|
||||||
|
/// </summary>
|
||||||
|
public List<AnnouncementLogDto> Logs { get; set; } = new List<AnnouncementLogDto>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 跳转链接
|
||||||
|
/// </summary>
|
||||||
|
public string? Url { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 公告日志 DTO
|
||||||
|
/// </summary>
|
||||||
|
public class AnnouncementLogDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 标题
|
||||||
|
/// </summary>
|
||||||
|
public string Title { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 内容列表
|
||||||
|
/// </summary>
|
||||||
|
public List<string> Content { get; set; } = new List<string>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 图片url
|
||||||
|
/// </summary>
|
||||||
|
public string? ImageUrl { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开始时间(系统公告时间、活动开始时间)
|
||||||
|
/// </summary>
|
||||||
|
public DateTime StartTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 活动结束时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? EndTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 公告类型(系统、活动)
|
||||||
|
/// </summary>
|
||||||
|
public AnnouncementTypeEnum Type{ get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 跳转链接
|
||||||
|
/// </summary>
|
||||||
|
public string? Url { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 公告输出 DTO
|
||||||
|
/// </summary>
|
||||||
|
public class AnnouncementOutput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 版本号
|
||||||
|
/// </summary>
|
||||||
|
public string Version { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 公告日志列表
|
||||||
|
/// </summary>
|
||||||
|
public List<AnnouncementLogDto> Logs { get; set; } = new List<AnnouncementLogDto>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.CardFlip;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌任务状态输出
|
||||||
|
/// </summary>
|
||||||
|
public class CardFlipStatusOutput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 本周总翻牌次数
|
||||||
|
/// </summary>
|
||||||
|
public int TotalFlips { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 剩余免费次数
|
||||||
|
/// </summary>
|
||||||
|
public int RemainingFreeFlips { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 剩余赠送次数
|
||||||
|
/// </summary>
|
||||||
|
public int RemainingBonusFlips { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 剩余邀请解锁次数
|
||||||
|
/// </summary>
|
||||||
|
public int RemainingInviteFlips { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否可以翻牌
|
||||||
|
/// </summary>
|
||||||
|
public bool CanFlip { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用户的邀请码
|
||||||
|
/// </summary>
|
||||||
|
public string? MyInviteCode { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 本周邀请人数
|
||||||
|
/// </summary>
|
||||||
|
public int InvitedCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌记录
|
||||||
|
/// </summary>
|
||||||
|
public List<CardFlipRecord> FlipRecords { get; set; } = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 下次可翻牌提示
|
||||||
|
/// </summary>
|
||||||
|
public string? NextFlipTip { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 当前用户是否已经填写过邀请码
|
||||||
|
/// </summary>
|
||||||
|
public bool IsFilledInviteCode { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌记录
|
||||||
|
/// </summary>
|
||||||
|
public class CardFlipRecord
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌序号(1-10)
|
||||||
|
/// </summary>
|
||||||
|
public int FlipNumber { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否已翻
|
||||||
|
/// </summary>
|
||||||
|
public bool IsFlipped { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否中奖
|
||||||
|
/// </summary>
|
||||||
|
public bool IsWin { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 奖励金额(token数)
|
||||||
|
/// </summary>
|
||||||
|
public long? RewardAmount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌类型描述
|
||||||
|
/// </summary>
|
||||||
|
public string? FlipTypeDesc { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 在翻牌顺序中的位置(1-10,表示第几个翻)
|
||||||
|
/// </summary>
|
||||||
|
public int FlipOrderIndex { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.CardFlip;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌输入
|
||||||
|
/// </summary>
|
||||||
|
public class FlipCardInput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌序号(1-10)
|
||||||
|
/// </summary>
|
||||||
|
public int FlipNumber { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.CardFlip;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌输出
|
||||||
|
/// </summary>
|
||||||
|
public class FlipCardOutput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌序号(1-10)
|
||||||
|
/// </summary>
|
||||||
|
public int FlipNumber { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否中奖
|
||||||
|
/// </summary>
|
||||||
|
public bool IsWin { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 奖励金额(token数)
|
||||||
|
/// </summary>
|
||||||
|
public long? RewardAmount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 奖励描述
|
||||||
|
/// </summary>
|
||||||
|
public string? RewardDesc { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 剩余可翻次数
|
||||||
|
/// </summary>
|
||||||
|
public int RemainingFlips { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.CardFlip;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请码信息输出
|
||||||
|
/// </summary>
|
||||||
|
public class InviteCodeOutput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 我的邀请码
|
||||||
|
/// </summary>
|
||||||
|
public string? MyInviteCode { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 本周邀请人数
|
||||||
|
/// </summary>
|
||||||
|
public int InvitedCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否已被邀请
|
||||||
|
/// </summary>
|
||||||
|
public bool IsInvited { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请历史记录
|
||||||
|
/// </summary>
|
||||||
|
public List<InvitationHistoryItem> InvitationHistory { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请历史记录项
|
||||||
|
/// </summary>
|
||||||
|
public class InvitationHistoryItem
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 被邀请人昵称(脱敏)
|
||||||
|
/// </summary>
|
||||||
|
public string InvitedUserName { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime InvitationTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 本周所在
|
||||||
|
/// </summary>
|
||||||
|
public string WeekDescription { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.CardFlip;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用邀请码输入
|
||||||
|
/// </summary>
|
||||||
|
public class UseInviteCodeInput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请码
|
||||||
|
/// </summary>
|
||||||
|
public string InviteCode { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.DailyTask;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 领取任务奖励输入
|
||||||
|
/// </summary>
|
||||||
|
public class ClaimTaskRewardInput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 任务等级(1=1000w任务,2=3000w任务)
|
||||||
|
/// </summary>
|
||||||
|
public int TaskLevel { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.DailyTask;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 每日任务状态输出
|
||||||
|
/// </summary>
|
||||||
|
public class DailyTaskStatusOutput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 今日消耗的尊享包Token数
|
||||||
|
/// </summary>
|
||||||
|
public long TodayConsumedTokens { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 任务列表
|
||||||
|
/// </summary>
|
||||||
|
public List<DailyTaskItem> Tasks { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 每日任务项
|
||||||
|
/// </summary>
|
||||||
|
public class DailyTaskItem
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 任务等级(1=1000w任务,2=3000w任务)
|
||||||
|
/// </summary>
|
||||||
|
public int Level { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 任务名称
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 任务描述
|
||||||
|
/// </summary>
|
||||||
|
public string Description { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 任务要求的Token消耗量
|
||||||
|
/// </summary>
|
||||||
|
public long RequiredTokens { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 奖励的Token数量
|
||||||
|
/// </summary>
|
||||||
|
public long RewardTokens { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 任务状态:0=未完成,1=可领取,2=已领取
|
||||||
|
/// </summary>
|
||||||
|
public int Status { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 任务进度百分比(0-100)
|
||||||
|
/// </summary>
|
||||||
|
public decimal Progress { get; set; }
|
||||||
|
}
|
||||||
@@ -62,4 +62,9 @@ public class ModelGetListOutput
|
|||||||
/// 备注信息
|
/// 备注信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? Remark { get; set; }
|
public string? Remark { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否为尊享包
|
||||||
|
/// </summary>
|
||||||
|
public bool IsPremiumPackage { get; set; }
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Pay;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取商品列表输入DTO
|
||||||
|
/// </summary>
|
||||||
|
public class GetGoodsListInput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 商品类别(可选)
|
||||||
|
/// 如果不传,则返回所有商品
|
||||||
|
/// 如果传了,则只返回指定类别的商品(VIP服务或尊享包)
|
||||||
|
/// </summary>
|
||||||
|
public GoodsCategoryType? GoodsCategoryType { get; set; }
|
||||||
|
}
|
||||||
@@ -17,28 +17,39 @@ public class GoodsListOutput
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public decimal OriginalPrice { get; set; }
|
public decimal OriginalPrice { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 商品参考价格
|
||||||
|
/// </summary>
|
||||||
|
public decimal ReferencePrice { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 商品实际价格(折扣后的价格)
|
/// 商品实际价格(折扣后的价格)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public decimal GoodsPrice { get; set; }
|
public decimal GoodsPrice { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 商品类型
|
|
||||||
/// </summary>
|
|
||||||
public GoodsTypeEnum GoodsType { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 商品备注
|
|
||||||
/// </summary>
|
|
||||||
public string Remark { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 折扣金额(仅尊享包)
|
/// 折扣金额(仅尊享包)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public decimal? DiscountAmount { get; set; }
|
public decimal? DiscountAmount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 商品类别
|
||||||
|
/// </summary>
|
||||||
|
public string GoodsCategory { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 商品备注
|
||||||
|
/// </summary>
|
||||||
|
public string Remark { get; set; }
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 折扣说明(仅尊享包)
|
/// 折扣说明(仅尊享包)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? DiscountDescription { get; set; }
|
public string? DiscountDescription { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 商品类型
|
||||||
|
/// </summary>
|
||||||
|
public GoodsTypeEnum GoodsType { get; set; }
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Token;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建Token输入
|
||||||
|
/// </summary>
|
||||||
|
public class TokenCreateInput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 名称(同一用户不能重复)
|
||||||
|
/// </summary>
|
||||||
|
[Required(ErrorMessage = "名称不能为空")]
|
||||||
|
[StringLength(100, ErrorMessage = "名称长度不能超过100个字符")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 过期时间(空为永不过期)
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? ExpireTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 尊享包额度限制(空为不限制)
|
||||||
|
/// </summary>
|
||||||
|
public long? PremiumQuotaLimit { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Token;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Token列表输出
|
||||||
|
/// </summary>
|
||||||
|
public class TokenGetListOutputDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Token Id
|
||||||
|
/// </summary>
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 名称
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Token密钥
|
||||||
|
/// </summary>
|
||||||
|
public string ApiKey { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 过期时间(空为永不过期)
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? ExpireTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 尊享包额度限制(空为不限制)
|
||||||
|
/// </summary>
|
||||||
|
public long? PremiumQuotaLimit { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 尊享包已使用额度
|
||||||
|
/// </summary>
|
||||||
|
public long PremiumUsedQuota { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否禁用
|
||||||
|
/// </summary>
|
||||||
|
public bool IsDisabled { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime CreationTime { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Token;
|
||||||
|
|
||||||
|
public class TokenSelectListOutputDto
|
||||||
|
{
|
||||||
|
public Guid TokenId { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public bool IsDisabled { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Token;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 编辑Token输入
|
||||||
|
/// </summary>
|
||||||
|
public class TokenUpdateInput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Token Id
|
||||||
|
/// </summary>
|
||||||
|
[Required(ErrorMessage = "Id不能为空")]
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 名称(同一用户不能重复)
|
||||||
|
/// </summary>
|
||||||
|
[Required(ErrorMessage = "名称不能为空")]
|
||||||
|
[StringLength(100, ErrorMessage = "名称长度不能超过100个字符")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 过期时间(空为永不过期)
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? ExpireTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 尊享包额度限制(空为不限制)
|
||||||
|
/// </summary>
|
||||||
|
public long? PremiumQuotaLimit { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 尊享服务Token用量统计DTO
|
||||||
|
/// </summary>
|
||||||
|
public class PremiumTokenUsageDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 总Token数
|
||||||
|
/// </summary>
|
||||||
|
public long PremiumTotalTokens { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 已使用Token数
|
||||||
|
/// </summary>
|
||||||
|
public long PremiumUsedTokens { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 剩余Token数
|
||||||
|
/// </summary>
|
||||||
|
public long PremiumRemainingTokens { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using Volo.Abp.Application.Dtos;
|
||||||
|
using Yi.Framework.Ddd.Application.Contracts;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
|
||||||
|
|
||||||
|
public class PremiumTokenUsageGetListInput : PagedAllResultRequestDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 是否免费
|
||||||
|
/// </summary>
|
||||||
|
public bool? IsFree { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
using Volo.Abp.Application.Dtos;
|
||||||
|
using Yi.Framework.Ddd.Application.Contracts;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
|
||||||
|
|
||||||
|
public class PremiumTokenUsageGetListOutput : CreationAuditedEntityDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// id
|
||||||
|
/// </summary>
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用户ID
|
||||||
|
/// </summary>
|
||||||
|
public Guid UserId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 包名称
|
||||||
|
/// </summary>
|
||||||
|
public string PackageName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 总用量(总token数)
|
||||||
|
/// </summary>
|
||||||
|
public long TotalTokens { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 剩余用量(剩余token数)
|
||||||
|
/// </summary>
|
||||||
|
public long RemainingTokens { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 已使用token数
|
||||||
|
/// </summary>
|
||||||
|
public long UsedTokens { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 到期时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? ExpireDateTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否激活
|
||||||
|
/// </summary>
|
||||||
|
public bool IsActive { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 购买金额
|
||||||
|
/// </summary>
|
||||||
|
public decimal PurchaseAmount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 备注
|
||||||
|
/// </summary>
|
||||||
|
public string? Remark { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 尊享包不同Token用量占比DTO(饼图)
|
||||||
|
/// </summary>
|
||||||
|
public class TokenPremiumUsageDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Token Id
|
||||||
|
/// </summary>
|
||||||
|
public Guid TokenId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Token名称
|
||||||
|
/// </summary>
|
||||||
|
public string TokenName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Token消耗量
|
||||||
|
/// </summary>
|
||||||
|
public long Tokens { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 占比(百分比)
|
||||||
|
/// </summary>
|
||||||
|
public decimal Percentage { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
|
||||||
|
|
||||||
|
public class UsageStatisticsGetInput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// tokenId
|
||||||
|
/// </summary>
|
||||||
|
public Guid? TokenId { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.IServices;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 公告服务接口
|
||||||
|
/// </summary>
|
||||||
|
public interface IAnnouncementService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取公告信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>公告信息</returns>
|
||||||
|
Task<List<AnnouncementLogDto>> GetAsync();
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
using Yi.Framework.AiHub.Application.Contracts.Dtos.CardFlip;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Contracts.IServices;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌服务接口
|
||||||
|
/// </summary>
|
||||||
|
public interface ICardFlipService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取本周翻牌任务状态
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<CardFlipStatusOutput> GetWeeklyTaskStatusAsync();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">翻牌输入</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<FlipCardOutput> FlipCardAsync(FlipCardInput input);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用邀请码解锁翻牌次数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">邀请码输入</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task UseInviteCodeAsync(UseInviteCodeInput input);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取我的邀请码信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<InviteCodeOutput> GetMyInviteCodeAsync();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成我的邀请码(如果没有)
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<string> GenerateMyInviteCodeAsync();
|
||||||
|
}
|
||||||
@@ -34,6 +34,7 @@ public interface IPayService : IApplicationService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取商品列表
|
/// 获取商品列表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="input">获取商品列表输入</param>
|
||||||
/// <returns>商品列表</returns>
|
/// <returns>商品列表</returns>
|
||||||
Task<List<GoodsListOutput>> GetGoodsListAsync();
|
Task<List<GoodsListOutput>> GetGoodsListAsync([FromQuery] GetGoodsListInput input);
|
||||||
}
|
}
|
||||||
@@ -11,11 +11,17 @@ public interface IUsageStatisticsService
|
|||||||
/// 获取当前用户近7天的Token消耗统计
|
/// 获取当前用户近7天的Token消耗统计
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>每日Token使用量列表</returns>
|
/// <returns>每日Token使用量列表</returns>
|
||||||
Task<List<DailyTokenUsageDto>> GetLast7DaysTokenUsageAsync();
|
Task<List<DailyTokenUsageDto>> GetLast7DaysTokenUsageAsync(UsageStatisticsGetInput input);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取当前用户各个模型的Token消耗量及占比
|
/// 获取当前用户各个模型的Token消耗量及占比
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>模型Token使用量列表</returns>
|
/// <returns>模型Token使用量列表</returns>
|
||||||
Task<List<ModelTokenUsageDto>> GetModelTokenUsageAsync();
|
Task<List<ModelTokenUsageDto>> GetModelTokenUsageAsync(UsageStatisticsGetInput input);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前用户尊享服务Token用量统计
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>尊享服务Token用量统计</returns>
|
||||||
|
Task<PremiumTokenUsageDto> GetPremiumTokenUsageAsync();
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,6 @@ using Yi.Framework.Rbac.Application.Contracts.IServices;
|
|||||||
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
||||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
using Yi.Framework.AiHub.Domain.Extensions;
|
using Yi.Framework.AiHub.Domain.Extensions;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
|
||||||
|
|
||||||
namespace Yi.Framework.AiHub.Application.Services;
|
namespace Yi.Framework.AiHub.Application.Services;
|
||||||
|
|
||||||
@@ -74,23 +73,79 @@ public class AiAccountService : ApplicationService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取尊享包Token信息
|
|
||||||
var premiumPackages = await _premiumPackageRepository._DbQueryable
|
|
||||||
.Where(x => x.UserId == userId && x.IsActive)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
if (premiumPackages.Any())
|
|
||||||
{
|
|
||||||
// 过滤掉已过期的包
|
|
||||||
var validPackages = premiumPackages
|
|
||||||
.Where(p => p.IsAvailable())
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
output.PremiumTotalTokens = validPackages.Sum(x => x.TotalTokens);
|
|
||||||
output.PremiumUsedTokens = validPackages.Sum(x => x.UsedTokens);
|
|
||||||
output.PremiumRemainingTokens = validPackages.Sum(x => x.RemainingTokens);
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取利润统计数据
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="currentCost">当前成本(RMB)</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[Authorize]
|
||||||
|
[HttpGet("account/profit-statistics")]
|
||||||
|
public async Task<string> GetProfitStatisticsAsync([FromQuery] decimal currentCost)
|
||||||
|
{
|
||||||
|
if (CurrentUser.UserName != "Guo" && CurrentUser.UserName != "cc")
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("您暂无权限访问");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. 获取尊享包总消耗和剩余库存
|
||||||
|
var premiumPackages = await _premiumPackageRepository._DbQueryable.ToListAsync();
|
||||||
|
long totalUsedTokens = premiumPackages.Sum(p => p.UsedTokens);
|
||||||
|
long totalRemainingTokens = premiumPackages.Sum(p => p.RemainingTokens);
|
||||||
|
|
||||||
|
// 2. 计算1亿Token成本
|
||||||
|
decimal costPerHundredMillion = totalUsedTokens > 0
|
||||||
|
? currentCost / (totalUsedTokens / 100000000m)
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
// 3. 计算总成本(剩余+已使用的总成本)
|
||||||
|
long totalTokens = totalUsedTokens + totalRemainingTokens;
|
||||||
|
decimal totalCost = totalTokens > 0
|
||||||
|
? (totalTokens / 100000000m) * costPerHundredMillion
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
// 4. 获取总收益(RechargeType=PremiumPackage的充值金额总和)
|
||||||
|
decimal totalRevenue = await _rechargeRepository._DbQueryable
|
||||||
|
.Where(x => x.RechargeType == Domain.Shared.Enums.RechargeTypeEnum.PremiumPackage)
|
||||||
|
.SumAsync(x => x.RechargeAmount);
|
||||||
|
|
||||||
|
// 5. 计算利润率
|
||||||
|
decimal profitRate = totalCost > 0
|
||||||
|
? (totalRevenue / totalCost - 1) * 100
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
// 6. 按200售价计算成本
|
||||||
|
decimal costAt200Price = totalRevenue > 0
|
||||||
|
? (totalCost / totalRevenue) * 200
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
// 7. 格式化输出
|
||||||
|
var today = DateTime.Now;
|
||||||
|
string dayOfWeek = today.ToString("dddd", new System.Globalization.CultureInfo("zh-CN"));
|
||||||
|
string weekDay = dayOfWeek switch
|
||||||
|
{
|
||||||
|
"星期一" => "周1",
|
||||||
|
"星期二" => "周2",
|
||||||
|
"星期三" => "周3",
|
||||||
|
"星期四" => "周4",
|
||||||
|
"星期五" => "周5",
|
||||||
|
"星期六" => "周6",
|
||||||
|
"星期日" => "周日",
|
||||||
|
_ => dayOfWeek
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = $@"{today:M月d日} {weekDay}
|
||||||
|
尊享包已消耗({totalUsedTokens / 100000000m:F2}亿){totalUsedTokens}
|
||||||
|
尊享包剩余库存({totalRemainingTokens / 100000000m:F2}亿){totalRemainingTokens}
|
||||||
|
当前成本:{currentCost:F2}RMB
|
||||||
|
1亿Token成本:{costPerHundredMillion:F2} RMB=1亿 Token
|
||||||
|
总成本:{totalCost:F2} RMB
|
||||||
|
总收益:{totalRevenue:F2}RMB
|
||||||
|
利润率: {profitRate:F1}%
|
||||||
|
按200售价来算,成本在{costAt200Price:F2}";
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
using Mapster;
|
||||||
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Volo.Abp.Application.Services;
|
||||||
|
using Volo.Abp.Caching;
|
||||||
|
using Yi.Framework.AiHub.Application.Contracts.Dtos.Announcement;
|
||||||
|
using Yi.Framework.AiHub.Application.Contracts.IServices;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 公告服务
|
||||||
|
/// </summary>
|
||||||
|
public class AnnouncementService : ApplicationService, IAnnouncementService
|
||||||
|
{
|
||||||
|
private readonly ISqlSugarRepository<AnnouncementAggregateRoot> _announcementRepository;
|
||||||
|
private readonly IConfiguration _configuration;
|
||||||
|
private readonly IDistributedCache<AnnouncementCacheDto> _announcementCache;
|
||||||
|
private const string AnnouncementCacheKey = "AiHub:Announcement";
|
||||||
|
|
||||||
|
public AnnouncementService(
|
||||||
|
ISqlSugarRepository<AnnouncementAggregateRoot> announcementRepository,
|
||||||
|
IConfiguration configuration,
|
||||||
|
IDistributedCache<AnnouncementCacheDto> announcementCache)
|
||||||
|
{
|
||||||
|
_announcementRepository = announcementRepository;
|
||||||
|
_configuration = configuration;
|
||||||
|
_announcementCache = announcementCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取公告信息
|
||||||
|
/// </summary>
|
||||||
|
public async Task<List<AnnouncementLogDto>> GetAsync()
|
||||||
|
{
|
||||||
|
// 使用 GetOrAddAsync 从缓存获取或添加数据,缓存1小时
|
||||||
|
var cacheData = await _announcementCache.GetOrAddAsync(
|
||||||
|
AnnouncementCacheKey,
|
||||||
|
async () => await LoadAnnouncementDataAsync(),
|
||||||
|
() => new DistributedCacheEntryOptions
|
||||||
|
{
|
||||||
|
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return cacheData?.Logs ?? new List<AnnouncementLogDto>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从数据库加载公告数据
|
||||||
|
/// </summary>
|
||||||
|
private async Task<AnnouncementCacheDto> LoadAnnouncementDataAsync()
|
||||||
|
{
|
||||||
|
// 查询所有公告日志,按日期降序排列
|
||||||
|
var logs = await _announcementRepository._DbQueryable
|
||||||
|
.OrderByDescending(x => x.StartTime)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
// 转换为 DTO
|
||||||
|
var logDtos = logs.Adapt<List<AnnouncementLogDto>>();
|
||||||
|
return new AnnouncementCacheDto
|
||||||
|
{
|
||||||
|
Logs = logDtos
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,275 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Volo.Abp.Application.Services;
|
||||||
|
using Volo.Abp.Users;
|
||||||
|
using Yi.Framework.AiHub.Application.Contracts.Dtos.CardFlip;
|
||||||
|
using Yi.Framework.AiHub.Application.Contracts.IServices;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
using Yi.Framework.AiHub.Domain.Extensions;
|
||||||
|
using Yi.Framework.AiHub.Domain.Managers;
|
||||||
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌服务 - 应用层组合服务
|
||||||
|
/// </summary>
|
||||||
|
[Authorize]
|
||||||
|
public class CardFlipService : ApplicationService, ICardFlipService
|
||||||
|
{
|
||||||
|
private readonly CardFlipManager _cardFlipManager;
|
||||||
|
private readonly InviteCodeManager _inviteCodeManager;
|
||||||
|
private readonly ISqlSugarRepository<PremiumPackageAggregateRoot> _premiumPackageRepository;
|
||||||
|
private readonly ILogger<CardFlipService> _logger;
|
||||||
|
|
||||||
|
public CardFlipService(
|
||||||
|
CardFlipManager cardFlipManager,
|
||||||
|
InviteCodeManager inviteCodeManager,
|
||||||
|
ISqlSugarRepository<PremiumPackageAggregateRoot> premiumPackageRepository,
|
||||||
|
ILogger<CardFlipService> logger)
|
||||||
|
{
|
||||||
|
_cardFlipManager = cardFlipManager;
|
||||||
|
_inviteCodeManager = inviteCodeManager;
|
||||||
|
_premiumPackageRepository = premiumPackageRepository;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取本周翻牌任务状态
|
||||||
|
/// </summary>
|
||||||
|
public async Task<CardFlipStatusOutput> GetWeeklyTaskStatusAsync()
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
var weekStart = CardFlipManager.GetWeekStartDate(DateTime.Now);
|
||||||
|
|
||||||
|
// 获取本周任务
|
||||||
|
var task = await _cardFlipManager.GetOrCreateWeeklyTaskAsync(userId, weekStart, createIfNotExists: false);
|
||||||
|
|
||||||
|
// 获取邀请码信息
|
||||||
|
var inviteCode = await _inviteCodeManager.GetUserInviteCodeAsync(userId);
|
||||||
|
|
||||||
|
// 统计本周邀请人数
|
||||||
|
var invitedCount = await _inviteCodeManager.GetWeeklyInvitationCountAsync(userId, weekStart);
|
||||||
|
|
||||||
|
//当前用户是否已填写过邀请码
|
||||||
|
var isFilledInviteCode = await _inviteCodeManager.IsFilledInviteCodeAsync(userId);
|
||||||
|
var output = new CardFlipStatusOutput
|
||||||
|
{
|
||||||
|
TotalFlips = task?.TotalFlips ?? 0,
|
||||||
|
RemainingFreeFlips = CardFlipManager.MAX_FREE_FLIPS - (task?.FreeFlipsUsed ?? 0),
|
||||||
|
RemainingBonusFlips = 0, // 已废弃
|
||||||
|
RemainingInviteFlips = CardFlipManager.MAX_INVITE_FLIPS - (task?.InviteFlipsUsed ?? 0),
|
||||||
|
CanFlip = _cardFlipManager.CanFlipCard(task),
|
||||||
|
MyInviteCode = inviteCode?.Code,
|
||||||
|
InvitedCount = invitedCount,
|
||||||
|
FlipRecords = BuildFlipRecords(task),
|
||||||
|
IsFilledInviteCode = isFilledInviteCode
|
||||||
|
};
|
||||||
|
|
||||||
|
// 生成提示信息
|
||||||
|
output.NextFlipTip = GenerateNextFlipTip(output);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌
|
||||||
|
/// </summary>
|
||||||
|
public async Task<FlipCardOutput> FlipCardAsync(FlipCardInput input)
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
var weekStart = CardFlipManager.GetWeekStartDate(DateTime.Now);
|
||||||
|
|
||||||
|
// 执行翻牌逻辑(由Manager处理验证和翻牌)
|
||||||
|
var result = await _cardFlipManager.ExecuteFlipAsync(userId, input.FlipNumber, weekStart);
|
||||||
|
|
||||||
|
// 如果中奖,发放奖励
|
||||||
|
if (result.IsWin)
|
||||||
|
{
|
||||||
|
await GrantRewardAsync(userId, result.RewardAmount, $"翻牌活动-序号{input.FlipNumber}中奖");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建输出
|
||||||
|
var output = new FlipCardOutput
|
||||||
|
{
|
||||||
|
FlipNumber = result.FlipNumber,
|
||||||
|
IsWin = result.IsWin,
|
||||||
|
RewardAmount = result.RewardAmount,
|
||||||
|
RewardDesc = result.RewardDesc,
|
||||||
|
RemainingFlips = CardFlipManager.TOTAL_MAX_FLIPS - input.FlipNumber
|
||||||
|
};
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用邀请码解锁翻牌次数
|
||||||
|
/// </summary>
|
||||||
|
public async Task UseInviteCodeAsync(UseInviteCodeInput input)
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
var weekStart = CardFlipManager.GetWeekStartDate(DateTime.Now);
|
||||||
|
|
||||||
|
// 获取本周任务
|
||||||
|
var task = await _cardFlipManager.GetOrCreateWeeklyTaskAsync(userId, weekStart, createIfNotExists: true);
|
||||||
|
|
||||||
|
// 验证是否已经使用了所有邀请解锁次数
|
||||||
|
if (task.InviteFlipsUsed >= CardFlipManager.MAX_INVITE_FLIPS)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("本周邀请解锁次数已用完");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用邀请码(由Manager处理验证和邀请逻辑)
|
||||||
|
await _inviteCodeManager.UseInviteCodeAsync(userId, input.InviteCode);
|
||||||
|
|
||||||
|
_logger.LogInformation($"用户 {userId} 使用邀请码 {input.InviteCode} 解锁翻牌次数成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取我的邀请码信息
|
||||||
|
/// </summary>
|
||||||
|
public async Task<InviteCodeOutput> GetMyInviteCodeAsync()
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
var weekStart = CardFlipManager.GetWeekStartDate(DateTime.Now);
|
||||||
|
|
||||||
|
// 获取我的邀请码
|
||||||
|
var inviteCode = await _inviteCodeManager.GetUserInviteCodeAsync(userId);
|
||||||
|
|
||||||
|
// 统计本周邀请人数
|
||||||
|
var invitedCount = await _inviteCodeManager.GetWeeklyInvitationCountAsync(userId, weekStart);
|
||||||
|
|
||||||
|
// 获取邀请历史
|
||||||
|
var invitationHistory = await _inviteCodeManager.GetInvitationHistoryAsync(userId, 10);
|
||||||
|
|
||||||
|
return new InviteCodeOutput
|
||||||
|
{
|
||||||
|
MyInviteCode = inviteCode?.Code,
|
||||||
|
InvitedCount = invitedCount,
|
||||||
|
InvitationHistory = invitationHistory.Select(x => new InvitationHistoryItem
|
||||||
|
{
|
||||||
|
InvitedUserName = x.InvitedUserName,
|
||||||
|
InvitationTime = x.InvitationTime,
|
||||||
|
WeekDescription = GetWeekDescription(x.InvitationTime)
|
||||||
|
}).ToList()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成我的邀请码
|
||||||
|
/// </summary>
|
||||||
|
public async Task<string> GenerateMyInviteCodeAsync()
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
|
||||||
|
// 生成邀请码(由Manager处理)
|
||||||
|
var code = await _inviteCodeManager.GenerateInviteCodeForUserAsync(userId);
|
||||||
|
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
#region 私有辅助方法
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构建翻牌记录列表
|
||||||
|
/// </summary>
|
||||||
|
private List<CardFlipRecord> BuildFlipRecords(CardFlipTaskAggregateRoot? task)
|
||||||
|
{
|
||||||
|
var records = new List<CardFlipRecord>();
|
||||||
|
|
||||||
|
// 获取已翻牌的顺序
|
||||||
|
var flippedOrder = task != null ? _cardFlipManager.GetFlippedOrder(task) : new List<int>();
|
||||||
|
var flippedNumbers = new HashSet<int>(flippedOrder);
|
||||||
|
|
||||||
|
// 获取中奖记录
|
||||||
|
var winRecords = task?.WinRecords ?? new Dictionary<int, long>();
|
||||||
|
|
||||||
|
// 构建记录,按照原始序号1-10排列
|
||||||
|
for (int i = 1; i <= CardFlipManager.TOTAL_MAX_FLIPS; i++)
|
||||||
|
{
|
||||||
|
var record = new CardFlipRecord
|
||||||
|
{
|
||||||
|
FlipNumber = i,
|
||||||
|
IsFlipped = flippedNumbers.Contains(i),
|
||||||
|
IsWin = false,
|
||||||
|
FlipTypeDesc = CardFlipManager.GetFlipTypeDesc(i),
|
||||||
|
// 设置在翻牌顺序中的位置(0表示未翻,>0表示第几个翻的)
|
||||||
|
FlipOrderIndex = flippedOrder.IndexOf(i) >= 0 ? flippedOrder.IndexOf(i) + 1 : 0
|
||||||
|
};
|
||||||
|
|
||||||
|
// 设置中奖信息
|
||||||
|
// 判断这张卡是第几次翻的
|
||||||
|
if (task != null && flippedNumbers.Contains(i))
|
||||||
|
{
|
||||||
|
var flipOrderIndex = flippedOrder.IndexOf(i) + 1; // 第几次翻的(1-based)
|
||||||
|
|
||||||
|
// 检查这次翻牌是否中奖
|
||||||
|
if (winRecords.TryGetValue(flipOrderIndex, out var rewardAmount))
|
||||||
|
{
|
||||||
|
record.IsWin = true;
|
||||||
|
record.RewardAmount = rewardAmount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
records.Add(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
return records;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成下次翻牌提示
|
||||||
|
/// </summary>
|
||||||
|
private string GenerateNextFlipTip(CardFlipStatusOutput status)
|
||||||
|
{
|
||||||
|
if (status.TotalFlips >= CardFlipManager.TOTAL_MAX_FLIPS)
|
||||||
|
{
|
||||||
|
return "本周翻牌次数已用完,请下周再来!";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status.RemainingFreeFlips > 0)
|
||||||
|
{
|
||||||
|
return $"本周您还有{status.RemainingFreeFlips}次免费翻牌机会";
|
||||||
|
}
|
||||||
|
else if (status.RemainingInviteFlips > 0)
|
||||||
|
{
|
||||||
|
if (status.TotalFlips >= 7)
|
||||||
|
{
|
||||||
|
return $"本周使用他人邀请码或他人使用你的邀请码,可解锁{status.RemainingInviteFlips}次翻牌,且必中大奖!每次中奖最大额度将翻倍!";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $"本周使用他人邀请码或他人使用你的邀请码,可解锁{status.RemainingInviteFlips}次翻牌,必中大奖!每次中奖最大额度将翻倍!";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "继续加油!";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发放奖励
|
||||||
|
/// </summary>
|
||||||
|
private async Task GrantRewardAsync(Guid userId, long amount, string description)
|
||||||
|
{
|
||||||
|
var premiumPackage = new PremiumPackageAggregateRoot(userId, amount, description)
|
||||||
|
{
|
||||||
|
PurchaseAmount = 0, // 奖励不需要付费
|
||||||
|
Remark = $"翻牌活动奖励:{amount / 10000}w tokens"
|
||||||
|
};
|
||||||
|
|
||||||
|
await _premiumPackageRepository.InsertAsync(premiumPackage);
|
||||||
|
|
||||||
|
_logger.LogInformation($"用户 {userId} 获得翻牌奖励 {amount / 10000}w tokens");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取周描述
|
||||||
|
/// </summary>
|
||||||
|
private string GetWeekDescription(DateTime date)
|
||||||
|
{
|
||||||
|
var weekStart = CardFlipManager.GetWeekStartDate(date);
|
||||||
|
var weekEnd = weekStart.AddDays(6);
|
||||||
|
|
||||||
|
return $"{weekStart:MM-dd} 至 {weekEnd:MM-dd}";
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@ using Yi.Framework.AiHub.Domain.Entities;
|
|||||||
using Yi.Framework.AiHub.Domain.Entities.Model;
|
using Yi.Framework.AiHub.Domain.Entities.Model;
|
||||||
using Yi.Framework.AiHub.Domain.Extensions;
|
using Yi.Framework.AiHub.Domain.Extensions;
|
||||||
using Yi.Framework.AiHub.Domain.Managers;
|
using Yi.Framework.AiHub.Domain.Managers;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||||
@@ -35,17 +36,19 @@ public class AiChatService : ApplicationService
|
|||||||
private readonly AiBlacklistManager _aiBlacklistManager;
|
private readonly AiBlacklistManager _aiBlacklistManager;
|
||||||
private readonly ILogger<AiChatService> _logger;
|
private readonly ILogger<AiChatService> _logger;
|
||||||
private readonly AiGateWayManager _aiGateWayManager;
|
private readonly AiGateWayManager _aiGateWayManager;
|
||||||
|
private readonly PremiumPackageManager _premiumPackageManager;
|
||||||
|
|
||||||
public AiChatService(IHttpContextAccessor httpContextAccessor,
|
public AiChatService(IHttpContextAccessor httpContextAccessor,
|
||||||
AiBlacklistManager aiBlacklistManager,
|
AiBlacklistManager aiBlacklistManager,
|
||||||
ISqlSugarRepository<AiModelEntity> aiModelRepository,
|
ISqlSugarRepository<AiModelEntity> aiModelRepository,
|
||||||
ILogger<AiChatService> logger, AiGateWayManager aiGateWayManager)
|
ILogger<AiChatService> logger, AiGateWayManager aiGateWayManager, PremiumPackageManager premiumPackageManager)
|
||||||
{
|
{
|
||||||
_httpContextAccessor = httpContextAccessor;
|
_httpContextAccessor = httpContextAccessor;
|
||||||
_aiBlacklistManager = aiBlacklistManager;
|
_aiBlacklistManager = aiBlacklistManager;
|
||||||
_aiModelRepository = aiModelRepository;
|
_aiModelRepository = aiModelRepository;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_aiGateWayManager = aiGateWayManager;
|
_aiGateWayManager = aiGateWayManager;
|
||||||
|
_premiumPackageManager = premiumPackageManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -84,7 +87,8 @@ public class AiChatService : ApplicationService
|
|||||||
SystemPrompt = null,
|
SystemPrompt = null,
|
||||||
ApiHost = null,
|
ApiHost = null,
|
||||||
ApiKey = null,
|
ApiKey = null,
|
||||||
Remark = x.Description
|
Remark = x.Description,
|
||||||
|
IsPremiumPackage = PremiumPackageConst.ModeIds.Contains(x.ModelId)
|
||||||
}).ToListAsync();
|
}).ToListAsync();
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
@@ -118,9 +122,20 @@ public class AiChatService : ApplicationService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//如果是尊享包服务,需要校验是是否尊享包足够
|
||||||
|
if (CurrentUser.IsAuthenticated && PremiumPackageConst.ModeIds.Contains(input.Model))
|
||||||
|
{
|
||||||
|
// 检查尊享token包用量
|
||||||
|
var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(CurrentUser.GetId());
|
||||||
|
if (availableTokens <= 0)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("尊享token包用量不足,请先购买尊享token包");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//ai网关代理httpcontext
|
//ai网关代理httpcontext
|
||||||
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
||||||
CurrentUser.Id, sessionId, cancellationToken);
|
CurrentUser.Id, sessionId, null, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -157,6 +172,6 @@ public class AiChatService : ApplicationService
|
|||||||
|
|
||||||
//ai网关代理httpcontext
|
//ai网关代理httpcontext
|
||||||
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
||||||
CurrentUser.Id, null, cancellationToken);
|
CurrentUser.Id, null, null, cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,245 @@
|
|||||||
|
using Dm.util;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using SqlSugar;
|
||||||
|
using Volo.Abp.Application.Dtos;
|
||||||
|
using Volo.Abp.Application.Services;
|
||||||
|
using Volo.Abp.Users;
|
||||||
|
using Yi.Framework.AiHub.Application.Contracts.Dtos.Token;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
||||||
|
using Yi.Framework.AiHub.Domain.Extensions;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
||||||
|
using Yi.Framework.Ddd.Application.Contracts;
|
||||||
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Token服务
|
||||||
|
/// </summary>
|
||||||
|
[Authorize]
|
||||||
|
public class TokenService : ApplicationService
|
||||||
|
{
|
||||||
|
private readonly ISqlSugarRepository<TokenAggregateRoot> _tokenRepository;
|
||||||
|
private readonly ISqlSugarRepository<UsageStatisticsAggregateRoot> _usageStatisticsRepository;
|
||||||
|
|
||||||
|
public TokenService(
|
||||||
|
ISqlSugarRepository<TokenAggregateRoot> tokenRepository,
|
||||||
|
ISqlSugarRepository<UsageStatisticsAggregateRoot> usageStatisticsRepository)
|
||||||
|
{
|
||||||
|
_tokenRepository = tokenRepository;
|
||||||
|
_usageStatisticsRepository = usageStatisticsRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前用户的Token列表
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("token/list")]
|
||||||
|
public async Task<PagedResultDto<TokenGetListOutputDto>> GetListAsync([FromQuery] PagedAllResultRequestDto input)
|
||||||
|
{
|
||||||
|
RefAsync<int> total = 0;
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
|
||||||
|
var tokens = await _tokenRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId)
|
||||||
|
.OrderByDescending(x => x.CreationTime)
|
||||||
|
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
|
||||||
|
|
||||||
|
if (!tokens.Any())
|
||||||
|
{
|
||||||
|
return new PagedResultDto<TokenGetListOutputDto>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取尊享包模型ID列表
|
||||||
|
var premiumModelIds = PremiumPackageConst.ModeIds;
|
||||||
|
|
||||||
|
// 批量查询所有Token的尊享包已使用额度
|
||||||
|
var tokenIds = tokens.Select(t => t.Id).ToList();
|
||||||
|
var usageStats = await _usageStatisticsRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId && tokenIds.Contains(x.TokenId) && premiumModelIds.Contains(x.ModelId))
|
||||||
|
.GroupBy(x => x.TokenId)
|
||||||
|
.Select(g => new
|
||||||
|
{
|
||||||
|
TokenId = g.TokenId,
|
||||||
|
UsedQuota = SqlFunc.AggregateSum(g.TotalTokenCount)
|
||||||
|
})
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
var result = tokens.Select(t =>
|
||||||
|
{
|
||||||
|
var usedQuota = usageStats.FirstOrDefault(u => u.TokenId == t.Id)?.UsedQuota ?? 0;
|
||||||
|
return new TokenGetListOutputDto
|
||||||
|
{
|
||||||
|
Id = t.Id,
|
||||||
|
Name = t.Name,
|
||||||
|
ApiKey = t.Token,
|
||||||
|
ExpireTime = t.ExpireTime,
|
||||||
|
PremiumQuotaLimit = t.PremiumQuotaLimit,
|
||||||
|
PremiumUsedQuota = usedQuota,
|
||||||
|
IsDisabled = t.IsDisabled,
|
||||||
|
CreationTime = t.CreationTime
|
||||||
|
};
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
return new PagedResultDto<TokenGetListOutputDto>(total, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("token/select-list")]
|
||||||
|
public async Task<List<TokenSelectListOutputDto>> GetSelectListAsync()
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
var tokens = await _tokenRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId)
|
||||||
|
.OrderBy(x => x.IsDisabled)
|
||||||
|
.OrderByDescending(x => x.CreationTime)
|
||||||
|
.Select(x => new TokenSelectListOutputDto
|
||||||
|
{
|
||||||
|
TokenId = x.Id,
|
||||||
|
Name = x.Name,
|
||||||
|
IsDisabled = x.IsDisabled
|
||||||
|
}).ToListAsync();
|
||||||
|
|
||||||
|
tokens.Insert(0,new TokenSelectListOutputDto
|
||||||
|
{
|
||||||
|
TokenId = Guid.Empty,
|
||||||
|
Name = "默认",
|
||||||
|
IsDisabled = false
|
||||||
|
});
|
||||||
|
return tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建Token
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("token")]
|
||||||
|
public async Task<TokenGetListOutputDto> CreateAsync([FromBody] TokenCreateInput input)
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
|
||||||
|
// 检查用户是否为VIP
|
||||||
|
if (!CurrentUser.IsAiVip())
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("充值成为Vip,畅享第三方token服务");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查名称是否重复
|
||||||
|
var exists = await _tokenRepository._DbQueryable
|
||||||
|
.AnyAsync(x => x.UserId == userId && x.Name == input.Name);
|
||||||
|
if (exists)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException($"名称【{input.Name}】已存在,请使用其他名称");
|
||||||
|
}
|
||||||
|
|
||||||
|
var token = new TokenAggregateRoot(userId, input.Name)
|
||||||
|
{
|
||||||
|
ExpireTime = input.ExpireTime,
|
||||||
|
PremiumQuotaLimit = input.PremiumQuotaLimit
|
||||||
|
};
|
||||||
|
|
||||||
|
await _tokenRepository.InsertAsync(token);
|
||||||
|
|
||||||
|
return new TokenGetListOutputDto
|
||||||
|
{
|
||||||
|
Id = token.Id,
|
||||||
|
Name = token.Name,
|
||||||
|
ApiKey = token.Token,
|
||||||
|
ExpireTime = token.ExpireTime,
|
||||||
|
PremiumQuotaLimit = token.PremiumQuotaLimit,
|
||||||
|
PremiumUsedQuota = 0,
|
||||||
|
IsDisabled = token.IsDisabled,
|
||||||
|
CreationTime = token.CreationTime
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 编辑Token
|
||||||
|
/// </summary>
|
||||||
|
[HttpPut("token")]
|
||||||
|
public async Task UpdateAsync([FromBody] TokenUpdateInput input)
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
|
||||||
|
var token = await _tokenRepository._DbQueryable
|
||||||
|
.FirstAsync(x => x.Id == input.Id && x.UserId == userId);
|
||||||
|
|
||||||
|
if (token is null)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("Token不存在或无权限操作");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查名称是否重复(排除自己)
|
||||||
|
var exists = await _tokenRepository._DbQueryable
|
||||||
|
.AnyAsync(x => x.UserId == userId && x.Name == input.Name && x.Id != input.Id);
|
||||||
|
if (exists)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException($"名称【{input.Name}】已存在,请使用其他名称");
|
||||||
|
}
|
||||||
|
|
||||||
|
token.Name = input.Name;
|
||||||
|
token.ExpireTime = input.ExpireTime;
|
||||||
|
token.PremiumQuotaLimit = input.PremiumQuotaLimit;
|
||||||
|
|
||||||
|
await _tokenRepository.UpdateAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除Token
|
||||||
|
/// </summary>
|
||||||
|
[HttpDelete("token/{id}")]
|
||||||
|
public async Task DeleteAsync(Guid id)
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
|
||||||
|
var token = await _tokenRepository._DbQueryable
|
||||||
|
.FirstAsync(x => x.Id == id && x.UserId == userId);
|
||||||
|
|
||||||
|
if (token is null)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("Token不存在或无权限操作");
|
||||||
|
}
|
||||||
|
|
||||||
|
await _tokenRepository.DeleteAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启用Token
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("token/{id}/enable")]
|
||||||
|
public async Task EnableAsync(Guid id)
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
|
||||||
|
var token = await _tokenRepository._DbQueryable
|
||||||
|
.FirstAsync(x => x.Id == id && x.UserId == userId);
|
||||||
|
|
||||||
|
if (token is null)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("Token不存在或无权限操作");
|
||||||
|
}
|
||||||
|
|
||||||
|
token.Enable();
|
||||||
|
await _tokenRepository.UpdateAsync(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 禁用Token
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("token/{id}/disable")]
|
||||||
|
public async Task DisableAsync(Guid id)
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
|
||||||
|
var token = await _tokenRepository._DbQueryable
|
||||||
|
.FirstAsync(x => x.Id == id && x.UserId == userId);
|
||||||
|
|
||||||
|
if (token is null)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("Token不存在或无权限操作");
|
||||||
|
}
|
||||||
|
|
||||||
|
token.Disable();
|
||||||
|
await _tokenRepository.UpdateAsync(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,185 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using SqlSugar;
|
||||||
|
using Volo.Abp.Application.Services;
|
||||||
|
using Volo.Abp.Users;
|
||||||
|
using Yi.Framework.AiHub.Application.Contracts.Dtos.DailyTask;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
||||||
|
using Yi.Framework.AiHub.Domain.Extensions;
|
||||||
|
using Yi.Framework.AiHub.Domain.Managers;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
||||||
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Application.Services;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 每日任务服务
|
||||||
|
/// </summary>
|
||||||
|
[Authorize]
|
||||||
|
public class DailyTaskService : ApplicationService
|
||||||
|
{
|
||||||
|
private readonly ISqlSugarRepository<DailyTaskRewardRecordAggregateRoot> _dailyTaskRepository;
|
||||||
|
private readonly ISqlSugarRepository<MessageAggregateRoot> _messageRepository;
|
||||||
|
private readonly ISqlSugarRepository<PremiumPackageAggregateRoot> _premiumPackageRepository;
|
||||||
|
private readonly ILogger<DailyTaskService> _logger;
|
||||||
|
|
||||||
|
// 任务配置
|
||||||
|
private readonly Dictionary<int, (long RequiredTokens, long RewardTokens, string Name, string Description)>
|
||||||
|
_taskConfigs = new()
|
||||||
|
{
|
||||||
|
{ 1, (10000000, 1000000, "尊享包1000w token任务", "累积使用尊享包 1000w token") }, // 1000w消耗 -> 100w奖励
|
||||||
|
{ 2, (30000000, 2000000, "尊享包3000w token任务", "累积使用尊享包 3000w token") } // 3000w消耗 -> 200w奖励
|
||||||
|
};
|
||||||
|
|
||||||
|
public DailyTaskService(
|
||||||
|
ISqlSugarRepository<DailyTaskRewardRecordAggregateRoot> dailyTaskRepository,
|
||||||
|
ISqlSugarRepository<MessageAggregateRoot> messageRepository,
|
||||||
|
ISqlSugarRepository<PremiumPackageAggregateRoot> premiumPackageRepository,
|
||||||
|
ILogger<DailyTaskService> logger)
|
||||||
|
{
|
||||||
|
_dailyTaskRepository = dailyTaskRepository;
|
||||||
|
_messageRepository = messageRepository;
|
||||||
|
_premiumPackageRepository = premiumPackageRepository;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取今日任务状态
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<DailyTaskStatusOutput> GetTodayTaskStatusAsync()
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
var today = DateTime.Today;
|
||||||
|
|
||||||
|
// 1. 统计今日尊享包Token消耗量
|
||||||
|
var todayConsumed = await GetTodayPremiumTokenConsumptionAsync(userId, today);
|
||||||
|
|
||||||
|
// 2. 查询今日已领取的任务
|
||||||
|
var claimedTasks = await _dailyTaskRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId && x.TaskDate == today)
|
||||||
|
.Select(x => new { x.TaskLevel, x.IsRewarded })
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
// 3. 构建任务列表
|
||||||
|
var tasks = new List<DailyTaskItem>();
|
||||||
|
foreach (var (level, config) in _taskConfigs)
|
||||||
|
{
|
||||||
|
var claimed = claimedTasks.FirstOrDefault(x => x.TaskLevel == level);
|
||||||
|
int status;
|
||||||
|
|
||||||
|
if (claimed != null && claimed.IsRewarded)
|
||||||
|
{
|
||||||
|
status = 2; // 已领取
|
||||||
|
}
|
||||||
|
else if (todayConsumed >= config.RequiredTokens)
|
||||||
|
{
|
||||||
|
status = 1; // 可领取
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
status = 0; // 未完成
|
||||||
|
}
|
||||||
|
|
||||||
|
var progress = todayConsumed >= config.RequiredTokens
|
||||||
|
? 100
|
||||||
|
: Math.Round((decimal)todayConsumed / config.RequiredTokens * 100, 2);
|
||||||
|
|
||||||
|
tasks.Add(new DailyTaskItem
|
||||||
|
{
|
||||||
|
Level = level,
|
||||||
|
Name = config.Name,
|
||||||
|
Description = config.Description,
|
||||||
|
RequiredTokens = config.RequiredTokens,
|
||||||
|
RewardTokens = config.RewardTokens,
|
||||||
|
Status = status,
|
||||||
|
Progress = progress
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return new DailyTaskStatusOutput
|
||||||
|
{
|
||||||
|
TodayConsumedTokens = todayConsumed,
|
||||||
|
Tasks = tasks
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 领取任务奖励
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task ClaimTaskRewardAsync(ClaimTaskRewardInput input)
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
var today = DateTime.Today;
|
||||||
|
|
||||||
|
// 1. 验证任务等级
|
||||||
|
if (!_taskConfigs.TryGetValue(input.TaskLevel, out var taskConfig))
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException($"无效的任务等级: {input.TaskLevel}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 检查是否已领取
|
||||||
|
var existingRecord = await _dailyTaskRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId && x.TaskDate == today && x.TaskLevel == input.TaskLevel)
|
||||||
|
.FirstAsync();
|
||||||
|
|
||||||
|
if (existingRecord != null)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("今日该任务奖励已领取,请明天再来!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 验证今日Token消耗是否达标
|
||||||
|
var todayConsumed = await GetTodayPremiumTokenConsumptionAsync(userId, today);
|
||||||
|
if (todayConsumed < taskConfig.RequiredTokens)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException(
|
||||||
|
$"Token消耗未达标!需要 {taskConfig.RequiredTokens / 10000}w,当前 {todayConsumed / 10000}w");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 创建奖励包(使用 PremiumPackageManager)
|
||||||
|
|
||||||
|
var premiumPackage =
|
||||||
|
new PremiumPackageAggregateRoot(userId, taskConfig.RewardTokens, $"每日任务:{taskConfig.Name}")
|
||||||
|
{
|
||||||
|
PurchaseAmount = 0, // 奖励不需要付费
|
||||||
|
Remark = $"{today:yyyy-MM-dd} 每日任务奖励"
|
||||||
|
};
|
||||||
|
|
||||||
|
await _premiumPackageRepository.InsertAsync(premiumPackage);
|
||||||
|
|
||||||
|
// 5. 记录领取记录
|
||||||
|
var record = new DailyTaskRewardRecordAggregateRoot(userId, input.TaskLevel, today, taskConfig.RewardTokens)
|
||||||
|
{
|
||||||
|
Remark = $"完成任务{input.TaskLevel},名称:{taskConfig.Name},消耗 {todayConsumed / 10000}w token"
|
||||||
|
};
|
||||||
|
|
||||||
|
await _dailyTaskRepository.InsertAsync(record);
|
||||||
|
|
||||||
|
_logger.LogInformation(
|
||||||
|
$"用户 {userId} 领取每日任务 {input.TaskLevel} 奖励成功,获得 {taskConfig.RewardTokens / 10000}w tokens");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取今日尊享包Token消耗量
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId">用户ID</param>
|
||||||
|
/// <param name="today">今日日期</param>
|
||||||
|
/// <returns>消耗的Token总数</returns>
|
||||||
|
private async Task<long> GetTodayPremiumTokenConsumptionAsync(Guid userId, DateTime today)
|
||||||
|
{
|
||||||
|
var tomorrow = today.AddDays(1);
|
||||||
|
|
||||||
|
// 查询今日所有使用尊享包模型的消息(role=system 表示消耗)
|
||||||
|
var totalTokens = await _messageRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId)
|
||||||
|
.Where(x => x.Role == "system") // system角色表示实际消耗
|
||||||
|
.Where(x => PremiumPackageConst.ModeIds.Contains(x.ModelId)) // 尊享包模型
|
||||||
|
.Where(x => x.CreationTime >= today && x.CreationTime < tomorrow)
|
||||||
|
.SumAsync(x => x.TokenUsage.TotalTokenCount);
|
||||||
|
|
||||||
|
return totalTokens;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -76,12 +76,12 @@ public class FileMasterService : ApplicationService
|
|||||||
if (input.Stream == true)
|
if (input.Stream == true)
|
||||||
{
|
{
|
||||||
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
||||||
userId, null, cancellationToken);
|
userId, null, null, cancellationToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await _aiGateWayManager.CompleteChatForStatisticsAsync(_httpContextAccessor.HttpContext, input, userId,
|
await _aiGateWayManager.CompleteChatForStatisticsAsync(_httpContextAccessor.HttpContext, input, userId,
|
||||||
null,
|
null, null,
|
||||||
cancellationToken);
|
cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Medallion.Threading;
|
using Medallion.Threading;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
@@ -18,7 +17,7 @@ using Yi.Framework.Rbac.Application.Contracts.Dtos.Account;
|
|||||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
namespace Yi.Framework.AiHub.Application.Services;
|
namespace Yi.Framework.AiHub.Application.Services.Fuwuhao;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 服务号服务
|
/// 服务号服务
|
||||||
@@ -55,18 +55,32 @@ public class OpenApiService : ApplicationService
|
|||||||
{
|
{
|
||||||
//前面都是校验,后面才是真正的调用
|
//前面都是校验,后面才是真正的调用
|
||||||
var httpContext = this._httpContextAccessor.HttpContext;
|
var httpContext = this._httpContextAccessor.HttpContext;
|
||||||
var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext));
|
var tokenValidation = await _tokenManager.ValidateTokenAsync(GetTokenByHttpContext(httpContext), input.Model);
|
||||||
|
var userId = tokenValidation.UserId;
|
||||||
|
var tokenId = tokenValidation.TokenId;
|
||||||
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
|
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
|
||||||
|
|
||||||
|
//如果是尊享包服务,需要校验是是否尊享包足够
|
||||||
|
if (PremiumPackageConst.ModeIds.Contains(input.Model))
|
||||||
|
{
|
||||||
|
// 检查尊享token包用量
|
||||||
|
var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(userId);
|
||||||
|
if (availableTokens <= 0)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("尊享token包用量不足,请先购买尊享token包");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//ai网关代理httpcontext
|
//ai网关代理httpcontext
|
||||||
if (input.Stream == true)
|
if (input.Stream == true)
|
||||||
{
|
{
|
||||||
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
||||||
userId, null, cancellationToken);
|
userId, null, tokenId, cancellationToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await _aiGateWayManager.CompleteChatForStatisticsAsync(_httpContextAccessor.HttpContext, input, userId,
|
await _aiGateWayManager.CompleteChatForStatisticsAsync(_httpContextAccessor.HttpContext, input, userId,
|
||||||
null,
|
null, tokenId,
|
||||||
cancellationToken);
|
cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,9 +95,11 @@ public class OpenApiService : ApplicationService
|
|||||||
{
|
{
|
||||||
var httpContext = this._httpContextAccessor.HttpContext;
|
var httpContext = this._httpContextAccessor.HttpContext;
|
||||||
Intercept(httpContext);
|
Intercept(httpContext);
|
||||||
var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext));
|
var tokenValidation = await _tokenManager.ValidateTokenAsync(GetTokenByHttpContext(httpContext), input.Model);
|
||||||
|
var userId = tokenValidation.UserId;
|
||||||
|
var tokenId = tokenValidation.TokenId;
|
||||||
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
|
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
|
||||||
await _aiGateWayManager.CreateImageForStatisticsAsync(httpContext, userId, null, input);
|
await _aiGateWayManager.CreateImageForStatisticsAsync(httpContext, userId, null, input, tokenId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -96,9 +112,11 @@ public class OpenApiService : ApplicationService
|
|||||||
{
|
{
|
||||||
var httpContext = this._httpContextAccessor.HttpContext;
|
var httpContext = this._httpContextAccessor.HttpContext;
|
||||||
Intercept(httpContext);
|
Intercept(httpContext);
|
||||||
var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext));
|
var tokenValidation = await _tokenManager.ValidateTokenAsync(GetTokenByHttpContext(httpContext), input.Model);
|
||||||
|
var userId = tokenValidation.UserId;
|
||||||
|
var tokenId = tokenValidation.TokenId;
|
||||||
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
|
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
|
||||||
await _aiGateWayManager.EmbeddingForStatisticsAsync(httpContext, userId, null, input);
|
await _aiGateWayManager.EmbeddingForStatisticsAsync(httpContext, userId, null, input, tokenId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -139,7 +157,9 @@ public class OpenApiService : ApplicationService
|
|||||||
{
|
{
|
||||||
//前面都是校验,后面才是真正的调用
|
//前面都是校验,后面才是真正的调用
|
||||||
var httpContext = this._httpContextAccessor.HttpContext;
|
var httpContext = this._httpContextAccessor.HttpContext;
|
||||||
var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext));
|
var tokenValidation = await _tokenManager.ValidateTokenAsync(GetTokenByHttpContext(httpContext), input.Model);
|
||||||
|
var userId = tokenValidation.UserId;
|
||||||
|
var tokenId = tokenValidation.TokenId;
|
||||||
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
|
await _aiBlacklistManager.VerifiyAiBlacklist(userId);
|
||||||
|
|
||||||
// 验证用户是否为VIP
|
// 验证用户是否为VIP
|
||||||
@@ -166,12 +186,12 @@ public class OpenApiService : ApplicationService
|
|||||||
if (input.Stream)
|
if (input.Stream)
|
||||||
{
|
{
|
||||||
await _aiGateWayManager.AnthropicCompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
await _aiGateWayManager.AnthropicCompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
||||||
userId, null, cancellationToken);
|
userId, null, tokenId, cancellationToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await _aiGateWayManager.AnthropicCompleteChatForStatisticsAsync(_httpContextAccessor.HttpContext, input, userId,
|
await _aiGateWayManager.AnthropicCompleteChatForStatisticsAsync(_httpContextAccessor.HttpContext, input, userId,
|
||||||
null,
|
null, tokenId,
|
||||||
cancellationToken);
|
cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,11 +201,16 @@ public class OpenApiService : ApplicationService
|
|||||||
|
|
||||||
private string? GetTokenByHttpContext(HttpContext httpContext)
|
private string? GetTokenByHttpContext(HttpContext httpContext)
|
||||||
{
|
{
|
||||||
// 获取Authorization头
|
// 优先从 x-api-key 获取
|
||||||
string authHeader = httpContext.Request.Headers["Authorization"];
|
string apiKeyHeader = httpContext.Request.Headers["x-api-key"];
|
||||||
|
if (!string.IsNullOrWhiteSpace(apiKeyHeader))
|
||||||
|
{
|
||||||
|
return apiKeyHeader.Trim();
|
||||||
|
}
|
||||||
|
|
||||||
// 检查是否有Bearer token
|
// 再检查 Authorization 头
|
||||||
if (authHeader != null && authHeader.StartsWith("Bearer "))
|
string authHeader = httpContext.Request.Headers["Authorization"];
|
||||||
|
if (!string.IsNullOrWhiteSpace(authHeader) && authHeader.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
return authHeader.Substring("Bearer ".Length).Trim();
|
return authHeader.Substring("Bearer ".Length).Trim();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,6 +112,13 @@ public class PayService : ApplicationService, IPayService
|
|||||||
|
|
||||||
_logger.LogInformation("订单状态更新成功,订单号:{OutTradeNo},状态:{TradeStatus}", outTradeNo, tradeStatus);
|
_logger.LogInformation("订单状态更新成功,订单号:{OutTradeNo},状态:{TradeStatus}", outTradeNo, tradeStatus);
|
||||||
|
|
||||||
|
// 验证交易状态,只有交易成功才执行充值逻辑
|
||||||
|
if (status != TradeStatusEnum.TRADE_SUCCESS)
|
||||||
|
{
|
||||||
|
_logger.LogError($"订单 {outTradeNo} 状态为 {tradeStatus},不执行充值逻辑");
|
||||||
|
return "success";
|
||||||
|
}
|
||||||
|
|
||||||
// 5. 根据商品类型进行不同的处理
|
// 5. 根据商品类型进行不同的处理
|
||||||
if (order.GoodsType.IsPremiumPackage())
|
if (order.GoodsType.IsPremiumPackage())
|
||||||
{
|
{
|
||||||
@@ -189,9 +196,10 @@ public class PayService : ApplicationService, IPayService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取商品列表
|
/// 获取商品列表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="input">获取商品列表输入</param>
|
||||||
/// <returns>商品列表</returns>
|
/// <returns>商品列表</returns>
|
||||||
[HttpGet("pay/GoodsList")]
|
[HttpGet("pay/GoodsList")]
|
||||||
public async Task<List<GoodsListOutput>> GetGoodsListAsync()
|
public async Task<List<GoodsListOutput>> GetGoodsListAsync([FromQuery] GetGoodsListInput input)
|
||||||
{
|
{
|
||||||
var goodsList = new List<GoodsListOutput>();
|
var goodsList = new List<GoodsListOutput>();
|
||||||
|
|
||||||
@@ -205,36 +213,56 @@ public class PayService : ApplicationService, IPayService
|
|||||||
// 遍历所有商品枚举
|
// 遍历所有商品枚举
|
||||||
foreach (GoodsTypeEnum goodsType in Enum.GetValues(typeof(GoodsTypeEnum)))
|
foreach (GoodsTypeEnum goodsType in Enum.GetValues(typeof(GoodsTypeEnum)))
|
||||||
{
|
{
|
||||||
|
// 如果指定了商品类别,则过滤
|
||||||
|
if (input.GoodsCategoryType.HasValue)
|
||||||
|
{
|
||||||
|
var goodsCategory = goodsType.GetGoodsCategory();
|
||||||
|
if (goodsCategory != input.GoodsCategoryType.Value)
|
||||||
|
{
|
||||||
|
continue; // 跳过不匹配的商品
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var originalPrice = goodsType.GetTotalAmount();
|
var originalPrice = goodsType.GetTotalAmount();
|
||||||
decimal actualPrice = originalPrice;
|
decimal actualPrice = originalPrice;
|
||||||
decimal? discountAmount = null;
|
decimal? discountAmount = null;
|
||||||
string? discountDescription = null;
|
string? discountDescription = null;
|
||||||
|
|
||||||
// 如果是尊享包商品,计算折扣
|
// 如果是尊享包商品,计算折扣
|
||||||
if (goodsType.IsPremiumPackage() && CurrentUser.IsAuthenticated)
|
if (goodsType.IsPremiumPackage())
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
if (CurrentUser.IsAuthenticated)
|
||||||
{
|
{
|
||||||
discountAmount = goodsType.CalculateDiscount(totalRechargeAmount);
|
discountAmount = goodsType.CalculateDiscount(totalRechargeAmount);
|
||||||
actualPrice = goodsType.GetDiscountedPrice(totalRechargeAmount);
|
actualPrice = goodsType.GetDiscountedPrice(totalRechargeAmount);
|
||||||
|
|
||||||
if (discountAmount > 0)
|
if (discountAmount > 0)
|
||||||
{
|
{
|
||||||
discountDescription = $"已优惠 ¥{discountAmount:F2}(累计充值每10元减1元,最多减20元)";
|
discountDescription = $"根据累积充值已优惠 ¥{discountAmount:F2}";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
discountDescription = "累计充值每10元可减1元,最多减20元";
|
discountDescription = $"累积充值过低,暂无优惠";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
discountDescription = $"登录后查看优惠";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var goodsItem = new GoodsListOutput
|
var goodsItem = new GoodsListOutput
|
||||||
{
|
{
|
||||||
GoodsName = goodsType.GetDisplayName(),
|
GoodsName = goodsType.GetChineseName(),
|
||||||
OriginalPrice = originalPrice,
|
OriginalPrice = originalPrice,
|
||||||
|
ReferencePrice = goodsType.GetReferencePrice(),
|
||||||
GoodsPrice = actualPrice,
|
GoodsPrice = actualPrice,
|
||||||
GoodsType = goodsType,
|
GoodsCategory = goodsType.GetGoodsCategory().ToString(),
|
||||||
Remark = GetGoodsRemark(goodsType),
|
Remark = goodsType.GetRemark(),
|
||||||
DiscountAmount = discountAmount,
|
DiscountAmount = discountAmount,
|
||||||
DiscountDescription = discountDescription
|
DiscountDescription = discountDescription,
|
||||||
|
GoodsType = goodsType
|
||||||
};
|
};
|
||||||
|
|
||||||
goodsList.Add(goodsItem);
|
goodsList.Add(goodsItem);
|
||||||
@@ -243,27 +271,6 @@ public class PayService : ApplicationService, IPayService
|
|||||||
return goodsList;
|
return goodsList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取商品备注信息
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="goodsType">商品类型</param>
|
|
||||||
/// <returns>商品备注</returns>
|
|
||||||
private string GetGoodsRemark(GoodsTypeEnum goodsType)
|
|
||||||
{
|
|
||||||
if (goodsType.IsPremiumPackage())
|
|
||||||
{
|
|
||||||
var tokenAmount = goodsType.GetTokenAmount();
|
|
||||||
return $"尊享包服务,提供 {tokenAmount:N0} Tokens(需要VIP资格)";
|
|
||||||
}
|
|
||||||
else if (goodsType.IsVipService())
|
|
||||||
{
|
|
||||||
var validMonths = goodsType.GetValidMonths();
|
|
||||||
var monthlyPrice = goodsType.GetMonthlyPrice();
|
|
||||||
return $"VIP服务,有效期 {validMonths} 个月,月均价 ¥{monthlyPrice:F2}";
|
|
||||||
}
|
|
||||||
|
|
||||||
return "未知商品类型";
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取交易状态描述
|
/// 获取交易状态描述
|
||||||
@@ -288,6 +295,7 @@ public class PayService : ApplicationService, IPayService
|
|||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return TradeStatusEnum.WAIT_TRADE;
|
return TradeStatusEnum.WAIT_TRADE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@ using Yi.Framework.AiHub.Application.Contracts.IServices;
|
|||||||
using Yi.Framework.AiHub.Domain.Entities;
|
using Yi.Framework.AiHub.Domain.Entities;
|
||||||
using Yi.Framework.AiHub.Domain.Managers;
|
using Yi.Framework.AiHub.Domain.Managers;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
@@ -64,6 +65,7 @@ namespace Yi.Framework.AiHub.Application.Services
|
|||||||
{
|
{
|
||||||
// 直接查询该用户最大的过期时间
|
// 直接查询该用户最大的过期时间
|
||||||
var maxExpireTime = await _repository._DbQueryable
|
var maxExpireTime = await _repository._DbQueryable
|
||||||
|
.Where(x => x.RechargeType == RechargeTypeEnum.Vip)
|
||||||
.Where(x => x.UserId == input.UserId && x.ExpireDateTime.HasValue)
|
.Where(x => x.UserId == input.UserId && x.ExpireDateTime.HasValue)
|
||||||
.MaxAsync(x => x.ExpireDateTime);
|
.MaxAsync(x => x.ExpireDateTime);
|
||||||
|
|
||||||
@@ -85,7 +87,8 @@ namespace Yi.Framework.AiHub.Application.Services
|
|||||||
Content = input.Content,
|
Content = input.Content,
|
||||||
ExpireDateTime = expireDateTime,
|
ExpireDateTime = expireDateTime,
|
||||||
Remark = input.Remark,
|
Remark = input.Remark,
|
||||||
ContactInfo = input.ContactInfo
|
ContactInfo = input.ContactInfo,
|
||||||
|
RechargeType = RechargeTypeEnum.Vip
|
||||||
};
|
};
|
||||||
|
|
||||||
// 保存充值记录到数据库
|
// 保存充值记录到数据库
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Volo.Abp.Application.Services;
|
|
||||||
using Volo.Abp.Users;
|
|
||||||
using Yi.Framework.AiHub.Application.Contracts.Dtos.Token;
|
|
||||||
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
|
||||||
using Yi.Framework.AiHub.Domain.Extensions;
|
|
||||||
using Yi.Framework.AiHub.Domain.Managers;
|
|
||||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
|
||||||
|
|
||||||
namespace Yi.Framework.AiHub.Application.Services;
|
|
||||||
|
|
||||||
public class TokenService : ApplicationService
|
|
||||||
{
|
|
||||||
private readonly ISqlSugarRepository<TokenAggregateRoot> _tokenRepository;
|
|
||||||
private readonly TokenManager _tokenManager;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 构造函数
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="tokenRepository"></param>
|
|
||||||
/// <param name="tokenManager"></param>
|
|
||||||
public TokenService(ISqlSugarRepository<TokenAggregateRoot> tokenRepository, TokenManager tokenManager)
|
|
||||||
{
|
|
||||||
_tokenRepository = tokenRepository;
|
|
||||||
_tokenManager = tokenManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取token
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
[Authorize]
|
|
||||||
public async Task<TokenOutput> GetAsync()
|
|
||||||
{
|
|
||||||
return new TokenOutput
|
|
||||||
{
|
|
||||||
ApiKey = await _tokenManager.GetAsync(CurrentUser.GetId())
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 创建token
|
|
||||||
/// </summary>
|
|
||||||
/// <exception cref="UserFriendlyException"></exception>
|
|
||||||
[Authorize]
|
|
||||||
public async Task CreateAsync()
|
|
||||||
{
|
|
||||||
if (!CurrentUser.IsAiVip())
|
|
||||||
{
|
|
||||||
throw new UserFriendlyException("充值成为Vip,畅享第三方token服务");
|
|
||||||
}
|
|
||||||
|
|
||||||
await _tokenManager.CreateAsync(CurrentUser.GetId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,18 @@
|
|||||||
|
using Mapster;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
|
using Volo.Abp.Application.Dtos;
|
||||||
using Volo.Abp.Application.Services;
|
using Volo.Abp.Application.Services;
|
||||||
using Volo.Abp.Users;
|
using Volo.Abp.Users;
|
||||||
using Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
|
using Yi.Framework.AiHub.Application.Contracts.Dtos.UsageStatistics;
|
||||||
using Yi.Framework.AiHub.Application.Contracts.IServices;
|
using Yi.Framework.AiHub.Application.Contracts.IServices;
|
||||||
using Yi.Framework.AiHub.Domain.Entities;
|
using Yi.Framework.AiHub.Domain.Entities;
|
||||||
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
||||||
|
using Yi.Framework.AiHub.Domain.Extensions;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
||||||
|
using Yi.Framework.Ddd.Application.Contracts;
|
||||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
namespace Yi.Framework.AiHub.Application.Services;
|
namespace Yi.Framework.AiHub.Application.Services;
|
||||||
@@ -18,20 +25,26 @@ public class UsageStatisticsService : ApplicationService, IUsageStatisticsServic
|
|||||||
{
|
{
|
||||||
private readonly ISqlSugarRepository<MessageAggregateRoot> _messageRepository;
|
private readonly ISqlSugarRepository<MessageAggregateRoot> _messageRepository;
|
||||||
private readonly ISqlSugarRepository<UsageStatisticsAggregateRoot> _usageStatisticsRepository;
|
private readonly ISqlSugarRepository<UsageStatisticsAggregateRoot> _usageStatisticsRepository;
|
||||||
|
private readonly ISqlSugarRepository<PremiumPackageAggregateRoot> _premiumPackageRepository;
|
||||||
|
private readonly ISqlSugarRepository<TokenAggregateRoot> _tokenRepository;
|
||||||
|
|
||||||
public UsageStatisticsService(
|
public UsageStatisticsService(
|
||||||
ISqlSugarRepository<MessageAggregateRoot> messageRepository,
|
ISqlSugarRepository<MessageAggregateRoot> messageRepository,
|
||||||
ISqlSugarRepository<UsageStatisticsAggregateRoot> usageStatisticsRepository)
|
ISqlSugarRepository<UsageStatisticsAggregateRoot> usageStatisticsRepository,
|
||||||
|
ISqlSugarRepository<PremiumPackageAggregateRoot> premiumPackageRepository,
|
||||||
|
ISqlSugarRepository<TokenAggregateRoot> tokenRepository)
|
||||||
{
|
{
|
||||||
_messageRepository = messageRepository;
|
_messageRepository = messageRepository;
|
||||||
_usageStatisticsRepository = usageStatisticsRepository;
|
_usageStatisticsRepository = usageStatisticsRepository;
|
||||||
|
_premiumPackageRepository = premiumPackageRepository;
|
||||||
|
_tokenRepository = tokenRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取当前用户近7天的Token消耗统计
|
/// 获取当前用户近7天的Token消耗统计
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>每日Token使用量列表</returns>
|
/// <returns>每日Token使用量列表</returns>
|
||||||
public async Task<List<DailyTokenUsageDto>> GetLast7DaysTokenUsageAsync()
|
public async Task<List<DailyTokenUsageDto>> GetLast7DaysTokenUsageAsync([FromQuery]UsageStatisticsGetInput input)
|
||||||
{
|
{
|
||||||
var userId = CurrentUser.GetId();
|
var userId = CurrentUser.GetId();
|
||||||
var endDate = DateTime.Today;
|
var endDate = DateTime.Today;
|
||||||
@@ -40,7 +53,9 @@ public class UsageStatisticsService : ApplicationService, IUsageStatisticsServic
|
|||||||
// 从Message表统计近7天的token消耗
|
// 从Message表统计近7天的token消耗
|
||||||
var dailyUsage = await _messageRepository._DbQueryable
|
var dailyUsage = await _messageRepository._DbQueryable
|
||||||
.Where(x => x.UserId == userId)
|
.Where(x => x.UserId == userId)
|
||||||
|
.Where(x => x.Role == "assistant" || x.Role == "system")
|
||||||
.Where(x => x.CreationTime >= startDate && x.CreationTime < endDate.AddDays(1))
|
.Where(x => x.CreationTime >= startDate && x.CreationTime < endDate.AddDays(1))
|
||||||
|
.WhereIF(input.TokenId.HasValue,x => x.TokenId == input.TokenId)
|
||||||
.GroupBy(x => x.CreationTime.Date)
|
.GroupBy(x => x.CreationTime.Date)
|
||||||
.Select(g => new
|
.Select(g => new
|
||||||
{
|
{
|
||||||
@@ -70,17 +85,19 @@ public class UsageStatisticsService : ApplicationService, IUsageStatisticsServic
|
|||||||
/// 获取当前用户各个模型的Token消耗量及占比
|
/// 获取当前用户各个模型的Token消耗量及占比
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>模型Token使用量列表</returns>
|
/// <returns>模型Token使用量列表</returns>
|
||||||
public async Task<List<ModelTokenUsageDto>> GetModelTokenUsageAsync()
|
public async Task<List<ModelTokenUsageDto>> GetModelTokenUsageAsync([FromQuery]UsageStatisticsGetInput input)
|
||||||
{
|
{
|
||||||
var userId = CurrentUser.GetId();
|
var userId = CurrentUser.GetId();
|
||||||
|
|
||||||
// 从UsageStatistics表获取各模型的token消耗统计
|
// 从UsageStatistics表获取各模型的token消耗统计(按ModelId聚合,因为同一模型可能有多个TokenId的记录)
|
||||||
var modelUsages = await _usageStatisticsRepository._DbQueryable
|
var modelUsages = await _usageStatisticsRepository._DbQueryable
|
||||||
.Where(x => x.UserId == userId)
|
.Where(x => x.UserId == userId)
|
||||||
|
.WhereIF(input.TokenId.HasValue,x => x.TokenId == input.TokenId)
|
||||||
|
.GroupBy(x => x.ModelId)
|
||||||
.Select(x => new
|
.Select(x => new
|
||||||
{
|
{
|
||||||
x.ModelId,
|
ModelId = x.ModelId,
|
||||||
x.TotalTokenCount
|
TotalTokenCount = SqlFunc.AggregateSum(x.TotalTokenCount)
|
||||||
})
|
})
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
@@ -102,4 +119,107 @@ public class UsageStatisticsService : ApplicationService, IUsageStatisticsServic
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前用户尊享服务Token用量统计
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>尊享服务Token用量统计</returns>
|
||||||
|
public async Task<PremiumTokenUsageDto> GetPremiumTokenUsageAsync()
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
|
||||||
|
// 获取尊享包Token信息
|
||||||
|
var premiumPackages = await _premiumPackageRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId && x.IsActive)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
var result = new PremiumTokenUsageDto();
|
||||||
|
|
||||||
|
if (premiumPackages.Any())
|
||||||
|
{
|
||||||
|
// 过滤掉已过期、禁用的包,不过滤用量负数的包
|
||||||
|
var validPackages = premiumPackages
|
||||||
|
.Where(p => p.IsAvailable(false))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
result.PremiumTotalTokens = validPackages.Sum(x => x.TotalTokens);
|
||||||
|
result.PremiumUsedTokens = validPackages.Sum(x => x.UsedTokens);
|
||||||
|
result.PremiumRemainingTokens = validPackages.Sum(x => x.RemainingTokens);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前用户尊享服务token用量统计列表
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("usage-statistics/premium-token-usage/list")]
|
||||||
|
public async Task<PagedResultDto<PremiumTokenUsageGetListOutput>> GetPremiumTokenUsageListAsync(
|
||||||
|
PremiumTokenUsageGetListInput input)
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
RefAsync<int> total = 0;
|
||||||
|
// 获取尊享包Token信息
|
||||||
|
var entities = await _premiumPackageRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId)
|
||||||
|
.WhereIF(input.IsFree == false, x => x.PurchaseAmount > 0)
|
||||||
|
.WhereIF(input.IsFree == true, x => x.PurchaseAmount == 0)
|
||||||
|
.WhereIF(input.StartTime is not null && input.EndTime is not null,
|
||||||
|
x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
|
||||||
|
.OrderByDescending(x => x.CreationTime)
|
||||||
|
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
|
||||||
|
return new PagedResultDto<PremiumTokenUsageGetListOutput>(total,
|
||||||
|
entities.Adapt<List<PremiumTokenUsageGetListOutput>>());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前用户尊享包不同Token用量占比(饼图)
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>各Token的尊享模型用量及占比</returns>
|
||||||
|
[HttpGet("usage-statistics/premium-token-usage/by-token")]
|
||||||
|
public async Task<List<TokenPremiumUsageDto>> GetPremiumTokenUsageByTokenAsync()
|
||||||
|
{
|
||||||
|
var userId = CurrentUser.GetId();
|
||||||
|
var premiumModelIds = PremiumPackageConst.ModeIds;
|
||||||
|
|
||||||
|
// 从UsageStatistics表获取尊享模型的token消耗统计(按TokenId聚合)
|
||||||
|
var tokenUsages = await _usageStatisticsRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId && premiumModelIds.Contains(x.ModelId))
|
||||||
|
.GroupBy(x => x.TokenId)
|
||||||
|
.Select(x => new
|
||||||
|
{
|
||||||
|
TokenId = x.TokenId,
|
||||||
|
TotalTokenCount = SqlFunc.AggregateSum(x.TotalTokenCount)
|
||||||
|
})
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
if (!tokenUsages.Any())
|
||||||
|
{
|
||||||
|
return new List<TokenPremiumUsageDto>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取用户的所有Token信息用于名称映射
|
||||||
|
var tokenIds = tokenUsages.Select(x => x.TokenId).ToList();
|
||||||
|
var tokens = await _tokenRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId && tokenIds.Contains(x.Id))
|
||||||
|
.Select(x => new { x.Id, x.Name })
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
var tokenNameDict = tokens.ToDictionary(x => x.Id, x => x.Name);
|
||||||
|
|
||||||
|
// 计算总token数
|
||||||
|
var totalTokens = tokenUsages.Sum(x => x.TotalTokenCount);
|
||||||
|
|
||||||
|
// 计算各Token占比
|
||||||
|
var result = tokenUsages.Select(x => new TokenPremiumUsageDto
|
||||||
|
{
|
||||||
|
TokenId = x.TokenId,
|
||||||
|
TokenName = x.TokenId == Guid.Empty ? "默认" : (tokenNameDict.TryGetValue(x.TokenId, out var name) ? name : "其他"),
|
||||||
|
Tokens = x.TotalTokenCount,
|
||||||
|
Percentage = totalTokens > 0 ? Math.Round((decimal)x.TotalTokenCount / totalTokens * 100, 2) : 0
|
||||||
|
}).OrderByDescending(x => x.Tokens).ToList();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Domain.Shared.Consts;
|
||||||
|
|
||||||
|
public class PremiumPackageConst
|
||||||
|
{
|
||||||
|
public static List<string> ModeIds =
|
||||||
|
[
|
||||||
|
"claude-sonnet-4-5-20250929",
|
||||||
|
"claude-haiku-4-5-20251001",
|
||||||
|
"claude-opus-4-5-20251101",
|
||||||
|
"gemini-3-pro-preview",
|
||||||
|
"gpt-5.1-codex-max"
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -56,4 +56,9 @@ public class AiModelDescribe
|
|||||||
/// 模型额外信息
|
/// 模型额外信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? ModelExtraInfo { get; set; }
|
public string? ModelExtraInfo { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模型倍率
|
||||||
|
/// </summary>
|
||||||
|
public decimal Multiplier { get; set; }
|
||||||
}
|
}
|
||||||
@@ -32,6 +32,25 @@ public class AnthropicStreamDto
|
|||||||
PromptTokensDetails = null,
|
PromptTokensDetails = null,
|
||||||
CompletionTokensDetails = null
|
CompletionTokensDetails = null
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
public void SupplementalMultiplier(decimal multiplier)
|
||||||
|
{
|
||||||
|
if (this.Usage is not null)
|
||||||
|
{
|
||||||
|
this.Usage.CacheCreationInputTokens =
|
||||||
|
(int)Math.Round((this.Usage.CacheCreationInputTokens ?? 0) * multiplier);
|
||||||
|
|
||||||
|
this.Usage.CacheReadInputTokens =
|
||||||
|
(int)Math.Round((this.Usage.CacheReadInputTokens ?? 0) * multiplier);
|
||||||
|
|
||||||
|
this.Usage.InputTokens =
|
||||||
|
(int)Math.Round((this.Usage.InputTokens ?? 0) * multiplier);
|
||||||
|
|
||||||
|
this.Usage.OutputTokens =
|
||||||
|
(int)Math.Round((this.Usage.OutputTokens ?? 0) * multiplier);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AnthropicStreamErrorDto
|
public class AnthropicStreamErrorDto
|
||||||
@@ -75,6 +94,8 @@ public class AnthropicChatCompletionDtoContentBlock
|
|||||||
[JsonPropertyName("tool_use_id")] public string? ToolUseId { get; set; }
|
[JsonPropertyName("tool_use_id")] public string? ToolUseId { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("content")] public object? Content { get; set; }
|
[JsonPropertyName("content")] public object? Content { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("text")] public string? Text { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AnthropicChatCompletionDto
|
public class AnthropicChatCompletionDto
|
||||||
@@ -93,7 +114,7 @@ public class AnthropicChatCompletionDto
|
|||||||
|
|
||||||
public object stop_sequence { get; set; }
|
public object stop_sequence { get; set; }
|
||||||
|
|
||||||
public AnthropicCompletionDtoUsage Usage { get; set; }
|
public AnthropicCompletionDtoUsage? Usage { get; set; }
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public ThorUsageResponse TokenUsage => new ThorUsageResponse
|
public ThorUsageResponse TokenUsage => new ThorUsageResponse
|
||||||
@@ -108,6 +129,24 @@ public class AnthropicChatCompletionDto
|
|||||||
PromptTokensDetails = null,
|
PromptTokensDetails = null,
|
||||||
CompletionTokensDetails = null
|
CompletionTokensDetails = null
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public void SupplementalMultiplier(decimal multiplier)
|
||||||
|
{
|
||||||
|
if (this.Usage is not null)
|
||||||
|
{
|
||||||
|
this.Usage.CacheCreationInputTokens =
|
||||||
|
(int)Math.Round((this.Usage?.CacheCreationInputTokens ?? 0) * multiplier);
|
||||||
|
|
||||||
|
this.Usage.CacheReadInputTokens =
|
||||||
|
(int)Math.Round((this.Usage?.CacheReadInputTokens ?? 0) * multiplier);
|
||||||
|
|
||||||
|
this.Usage.InputTokens =
|
||||||
|
(int)Math.Round((this.Usage?.InputTokens ?? 0) * multiplier);
|
||||||
|
|
||||||
|
this.Usage.OutputTokens =
|
||||||
|
(int)Math.Round((this.Usage?.OutputTokens ?? 0) * multiplier);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AnthropicChatCompletionDtoContent
|
public class AnthropicChatCompletionDtoContent
|
||||||
|
|||||||
@@ -108,9 +108,16 @@ public sealed class AnthropicInput
|
|||||||
|
|
||||||
public class AnthropicThinkingInput
|
public class AnthropicThinkingInput
|
||||||
{
|
{
|
||||||
[JsonPropertyName("type")] public string Type { get; set; }
|
[JsonPropertyName("type")] public string? Type { get; set; }
|
||||||
|
[JsonPropertyName("budget_tokens")] public int? BudgetTokens { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("budget_tokens")] public int BudgetTokens { get; set; }
|
[JsonPropertyName("signature")] public string? Signature { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("thinking")] public string? Thinking { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("data")] public string? Data { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("text")] public string? Text { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AnthropicTooChoiceInput
|
public class AnthropicTooChoiceInput
|
||||||
@@ -122,16 +129,16 @@ public class AnthropicTooChoiceInput
|
|||||||
|
|
||||||
public class AnthropicMessageTool
|
public class AnthropicMessageTool
|
||||||
{
|
{
|
||||||
[JsonPropertyName("name")] public string name { get; set; }
|
[JsonPropertyName("name")] public string? name { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("description")] public string? Description { get; set; }
|
[JsonPropertyName("description")] public string? Description { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("input_schema")] public Input_schema InputSchema { get; set; }
|
[JsonPropertyName("input_schema")] public Input_schema? InputSchema { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Input_schema
|
public class Input_schema
|
||||||
{
|
{
|
||||||
[JsonPropertyName("type")] public string Type { get; set; }
|
[JsonPropertyName("type")] public string? Type { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("properties")] public Dictionary<string, InputSchemaValue>? Properties { get; set; }
|
[JsonPropertyName("properties")] public Dictionary<string, InputSchemaValue>? Properties { get; set; }
|
||||||
|
|
||||||
@@ -140,9 +147,9 @@ public class Input_schema
|
|||||||
|
|
||||||
public class InputSchemaValue
|
public class InputSchemaValue
|
||||||
{
|
{
|
||||||
public string type { get; set; }
|
public string? type { get; set; }
|
||||||
|
|
||||||
public string description { get; set; }
|
public string? description { get; set; }
|
||||||
|
|
||||||
public InputSchemaValueItems? items { get; set; }
|
public InputSchemaValueItems? items { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ public class AnthropicMessageContent
|
|||||||
|
|
||||||
[JsonPropertyName("thinking")] public string? Thinking { get; set; }
|
[JsonPropertyName("thinking")] public string? Thinking { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("signature")] public string? Signature { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("input")] public object? Input { get; set; }
|
[JsonPropertyName("input")] public object? Input { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("content")]
|
[JsonPropertyName("content")]
|
||||||
|
|||||||
@@ -22,11 +22,15 @@ public class AnthropicMessageInput
|
|||||||
throw new ValidationException("Messages 中 Content 和 Contents 字段不能同时有值");
|
throw new ValidationException("Messages 中 Content 和 Contents 字段不能同时有值");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Content is not null)
|
if (!string.IsNullOrEmpty(Content))
|
||||||
{
|
{
|
||||||
return Content;
|
return Content;
|
||||||
}
|
}
|
||||||
|
// 如果 Contents 为空或 null,返回空字符串而不是 null
|
||||||
|
if (Contents == null || Contents.Count == 0)
|
||||||
|
{
|
||||||
|
return "_"; // 兼容客户端空值问题
|
||||||
|
}
|
||||||
return Contents!;
|
return Contents!;
|
||||||
}
|
}
|
||||||
set
|
set
|
||||||
|
|||||||
@@ -60,4 +60,22 @@ public record ThorChatCompletionsResponse
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[JsonPropertyName("error")]
|
[JsonPropertyName("error")]
|
||||||
public ThorError? Error { get; set; }
|
public ThorError? Error { get; set; }
|
||||||
|
|
||||||
|
public void SupplementalMultiplier(decimal multiplier)
|
||||||
|
{
|
||||||
|
if (this.Usage is not null)
|
||||||
|
{
|
||||||
|
this.Usage.InputTokens =
|
||||||
|
(int)Math.Round((this.Usage.InputTokens ?? 0) * multiplier);
|
||||||
|
this.Usage.OutputTokens =
|
||||||
|
(int)Math.Round((this.Usage.OutputTokens ?? 0) * multiplier);
|
||||||
|
this.Usage.CompletionTokens =
|
||||||
|
(int)Math.Round((this.Usage.CompletionTokens ?? 0) * multiplier);
|
||||||
|
this.Usage.PromptTokens =
|
||||||
|
(int)Math.Round((this.Usage.PromptTokens ?? 0) * multiplier);
|
||||||
|
|
||||||
|
this.Usage.TotalTokens =
|
||||||
|
(int)Math.Round((this.Usage.TotalTokens ?? 0) * multiplier);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||||
|
|
||||||
|
public enum AnnouncementTypeEnum
|
||||||
|
{
|
||||||
|
Activity=1,
|
||||||
|
System=2
|
||||||
|
}
|
||||||
@@ -10,12 +10,16 @@ namespace Yi.Framework.AiHub.Domain.Shared.Enums;
|
|||||||
public class PriceAttribute : Attribute
|
public class PriceAttribute : Attribute
|
||||||
{
|
{
|
||||||
public decimal Price { get; }
|
public decimal Price { get; }
|
||||||
|
|
||||||
|
public decimal ReferencePrice { get; }
|
||||||
|
|
||||||
public int ValidMonths { get; }
|
public int ValidMonths { get; }
|
||||||
|
|
||||||
public PriceAttribute(double price, int validMonths)
|
public PriceAttribute(double price, int validMonths, double referencePrice)
|
||||||
{
|
{
|
||||||
Price = (decimal)price;
|
Price = (decimal)price;
|
||||||
ValidMonths = validMonths;
|
ValidMonths = validMonths;
|
||||||
|
ReferencePrice = (decimal)referencePrice;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,10 +30,14 @@ public class PriceAttribute : Attribute
|
|||||||
public class DisplayNameAttribute : Attribute
|
public class DisplayNameAttribute : Attribute
|
||||||
{
|
{
|
||||||
public string DisplayName { get; }
|
public string DisplayName { get; }
|
||||||
|
public string ChineseName { get; }
|
||||||
|
public string Remark { get; }
|
||||||
|
|
||||||
public DisplayNameAttribute(string displayName)
|
public DisplayNameAttribute(string displayName, string chineseName = "", string remark = "")
|
||||||
{
|
{
|
||||||
DisplayName = displayName;
|
DisplayName = displayName;
|
||||||
|
ChineseName = chineseName;
|
||||||
|
Remark = remark;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +64,7 @@ public enum GoodsCategoryType
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// VIP服务
|
/// VIP服务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
VipService = 1,
|
Vip = 1,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 尊享包服务
|
/// 尊享包服务
|
||||||
@@ -85,39 +93,27 @@ public class TokenAmountAttribute : Attribute
|
|||||||
public enum GoodsTypeEnum
|
public enum GoodsTypeEnum
|
||||||
{
|
{
|
||||||
// VIP服务
|
// VIP服务
|
||||||
[Price(29.9, 1)]
|
[Price(29.9, 1, 29.9)] [DisplayName("YiXinVip 1 month", "1个月", "灵活选择")] [GoodsCategory(GoodsCategoryType.Vip)]
|
||||||
[DisplayName("YiXinVip 1 month")]
|
|
||||||
[GoodsCategory(GoodsCategoryType.VipService)]
|
|
||||||
YiXinVip1 = 1,
|
YiXinVip1 = 1,
|
||||||
|
|
||||||
[Price(83.7, 3)]
|
[Price(83.7, 3, 27.9)] [DisplayName("YiXinVip 3 month", "3个月", "短期体验")] [GoodsCategory(GoodsCategoryType.Vip)]
|
||||||
[DisplayName("YiXinVip 3 month")]
|
|
||||||
[GoodsCategory(GoodsCategoryType.VipService)]
|
|
||||||
YiXinVip3 = 3,
|
YiXinVip3 = 3,
|
||||||
|
|
||||||
[Price(155.4, 6)]
|
[Price(155.4, 6, 25.9)] [DisplayName("YiXinVip 6 month", "6个月", "年度热销")] [GoodsCategory(GoodsCategoryType.Vip)]
|
||||||
[DisplayName("YiXinVip 6 month")]
|
|
||||||
[GoodsCategory(GoodsCategoryType.VipService)]
|
|
||||||
YiXinVip6 = 6,
|
YiXinVip6 = 6,
|
||||||
|
|
||||||
[Price(183.2, 8)]
|
|
||||||
[DisplayName("YiXinVip 8 month")]
|
|
||||||
[GoodsCategory(GoodsCategoryType.VipService)]
|
|
||||||
YiXinVip8 = 8,
|
|
||||||
|
|
||||||
// 尊享包服务 - 需要VIP资格才能购买
|
// 尊享包服务 - 需要VIP资格才能购买
|
||||||
[Price(188.9, 0)]
|
[Price(188.9, 0, 1750)]
|
||||||
[DisplayName("Premium Package 5000W Tokens")]
|
[DisplayName("YiXinPremiumPackage 5000W Tokens", "5000万Tokens", "简单尝试")]
|
||||||
[GoodsCategory(GoodsCategoryType.PremiumPackage)]
|
[GoodsCategory(GoodsCategoryType.PremiumPackage)]
|
||||||
[TokenAmount(5000)]
|
[TokenAmount(50000000)]
|
||||||
PremiumPackage5000W = 101,
|
PremiumPackage5000W = 101,
|
||||||
|
|
||||||
[Price(248.9, 0)]
|
[Price(248.9, 0, 3500)]
|
||||||
[DisplayName("Premium Package 10000W Tokens")]
|
[DisplayName("YiXinPremiumPackage 10000W Tokens", "1亿Tokens(推荐)", "极致性价比")]
|
||||||
[GoodsCategory(GoodsCategoryType.PremiumPackage)]
|
[GoodsCategory(GoodsCategoryType.PremiumPackage)]
|
||||||
[TokenAmount(10000)]
|
[TokenAmount(100000000)]
|
||||||
PremiumPackage10000W = 102,
|
PremiumPackage10000W = 102,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class GoodsTypeEnumExtensions
|
public static class GoodsTypeEnumExtensions
|
||||||
@@ -181,6 +177,18 @@ public static class GoodsTypeEnumExtensions
|
|||||||
return validMonths > 0 ? totalPrice / validMonths : 0m;
|
return validMonths > 0 ? totalPrice / validMonths : 0m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取商品参考价格
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="goodsType">商品类型</param>
|
||||||
|
/// <returns>参考价格</returns>
|
||||||
|
public static decimal GetReferencePrice(this GoodsTypeEnum goodsType)
|
||||||
|
{
|
||||||
|
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
|
||||||
|
var priceAttribute = fieldInfo?.GetCustomAttribute<PriceAttribute>();
|
||||||
|
return priceAttribute?.ReferencePrice ?? 0m;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取商品类别
|
/// 获取商品类别
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -190,7 +198,7 @@ public static class GoodsTypeEnumExtensions
|
|||||||
{
|
{
|
||||||
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
|
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
|
||||||
var categoryAttribute = fieldInfo?.GetCustomAttribute<GoodsCategoryAttribute>();
|
var categoryAttribute = fieldInfo?.GetCustomAttribute<GoodsCategoryAttribute>();
|
||||||
return categoryAttribute?.Category ?? GoodsCategoryType.VipService;
|
return categoryAttribute?.Category ?? GoodsCategoryType.Vip;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -210,7 +218,7 @@ public static class GoodsTypeEnumExtensions
|
|||||||
/// <returns>是否为VIP服务</returns>
|
/// <returns>是否为VIP服务</returns>
|
||||||
public static bool IsVipService(this GoodsTypeEnum goodsType)
|
public static bool IsVipService(this GoodsTypeEnum goodsType)
|
||||||
{
|
{
|
||||||
return goodsType.GetGoodsCategory() == GoodsCategoryType.VipService;
|
return goodsType.GetGoodsCategory() == GoodsCategoryType.Vip;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -225,9 +233,33 @@ public static class GoodsTypeEnumExtensions
|
|||||||
return tokenAttribute?.TokenAmount ?? 0;
|
return tokenAttribute?.TokenAmount ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取商品中文名称
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="goodsType">商品类型</param>
|
||||||
|
/// <returns>中文名称</returns>
|
||||||
|
public static string GetChineseName(this GoodsTypeEnum goodsType)
|
||||||
|
{
|
||||||
|
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
|
||||||
|
var displayNameAttribute = fieldInfo?.GetCustomAttribute<DisplayNameAttribute>();
|
||||||
|
return displayNameAttribute?.ChineseName ?? goodsType.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取商品备注
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="goodsType">商品类型</param>
|
||||||
|
/// <returns>备注信息</returns>
|
||||||
|
public static string GetRemark(this GoodsTypeEnum goodsType)
|
||||||
|
{
|
||||||
|
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
|
||||||
|
var displayNameAttribute = fieldInfo?.GetCustomAttribute<DisplayNameAttribute>();
|
||||||
|
return displayNameAttribute?.Remark ?? string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 计算折扣金额(仅用于尊享包)
|
/// 计算折扣金额(仅用于尊享包)
|
||||||
/// 规则:每累加充值10元,减少1元,最多减少20元
|
/// 规则:每累加充值10元,减少2.5元,最多减少50元
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="goodsType">商品类型</param>
|
/// <param name="goodsType">商品类型</param>
|
||||||
/// <param name="totalRechargeAmount">用户累加充值金额</param>
|
/// <param name="totalRechargeAmount">用户累加充值金额</param>
|
||||||
@@ -240,11 +272,11 @@ public static class GoodsTypeEnumExtensions
|
|||||||
return 0m;
|
return 0m;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 每10元减1元
|
// 每10元减2.5元
|
||||||
var discountAmount = Math.Floor(totalRechargeAmount / 10m);
|
var discountAmount = Math.Floor(totalRechargeAmount / 2.5m);
|
||||||
|
|
||||||
// 最多减少20元
|
// 最多减少50元
|
||||||
return Math.Min(discountAmount, 20m);
|
return Math.Min(discountAmount, 50m);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -260,6 +292,6 @@ public static class GoodsTypeEnumExtensions
|
|||||||
var discountedPrice = originalPrice - discount;
|
var discountedPrice = originalPrice - discount;
|
||||||
|
|
||||||
// 确保价格不为负数,至少为0.01元
|
// 确保价格不为负数,至少为0.01元
|
||||||
return Math.Max(discountedPrice, 0.01m);
|
return Math.Round(discountedPrice, 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||||
|
|
||||||
|
public enum ModelApiTypeEnum
|
||||||
|
{
|
||||||
|
OpenAi,
|
||||||
|
Claude
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||||
|
|
||||||
|
public enum RechargeTypeEnum
|
||||||
|
{
|
||||||
|
Vip = 10,
|
||||||
|
PremiumPackage = 20
|
||||||
|
}
|
||||||
@@ -9,7 +9,9 @@ using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
|||||||
|
|
||||||
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorClaude.Chats;
|
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorClaude.Chats;
|
||||||
|
|
||||||
public class AnthropicChatCompletionsService(IHttpClientFactory httpClientFactory,ILogger<AnthropicChatCompletionsService> logger)
|
public class AnthropicChatCompletionsService(
|
||||||
|
IHttpClientFactory httpClientFactory,
|
||||||
|
ILogger<AnthropicChatCompletionsService> logger)
|
||||||
: IAnthropicChatCompletionService
|
: IAnthropicChatCompletionService
|
||||||
{
|
{
|
||||||
public async Task<AnthropicChatCompletionDto> ChatCompletionsAsync(AiModelDescribe options, AnthropicInput input,
|
public async Task<AnthropicChatCompletionDto> ChatCompletionsAsync(AiModelDescribe options, AnthropicInput input,
|
||||||
@@ -22,6 +24,7 @@ public class AnthropicChatCompletionsService(IHttpClientFactory httpClientFactor
|
|||||||
{
|
{
|
||||||
options.Endpoint = "https://api.anthropic.com/";
|
options.Endpoint = "https://api.anthropic.com/";
|
||||||
}
|
}
|
||||||
|
|
||||||
var client = httpClientFactory.CreateClient();
|
var client = httpClientFactory.CreateClient();
|
||||||
|
|
||||||
var headers = new Dictionary<string, string>
|
var headers = new Dictionary<string, string>
|
||||||
@@ -71,10 +74,11 @@ public class AnthropicChatCompletionsService(IHttpClientFactory httpClientFactor
|
|||||||
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
||||||
{
|
{
|
||||||
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||||
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}", options.Endpoint,
|
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}",
|
||||||
|
options.Endpoint,
|
||||||
response.StatusCode, error);
|
response.StatusCode, error);
|
||||||
|
|
||||||
throw new Exception("OpenAI对话异常" + response.StatusCode.ToString());
|
throw new Exception( $"恭喜你运气爆棚遇到了错误,尊享包对话异常:StatusCode【{response.StatusCode}】,Response【{error}】");
|
||||||
}
|
}
|
||||||
|
|
||||||
var value =
|
var value =
|
||||||
@@ -84,7 +88,8 @@ public class AnthropicChatCompletionsService(IHttpClientFactory httpClientFactor
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async IAsyncEnumerable<(string, AnthropicStreamDto?)> StreamChatCompletionsAsync(AiModelDescribe options, AnthropicInput input,
|
public async IAsyncEnumerable<(string, AnthropicStreamDto?)> StreamChatCompletionsAsync(AiModelDescribe options,
|
||||||
|
AnthropicInput input,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
using var openai =
|
using var openai =
|
||||||
@@ -117,7 +122,8 @@ public class AnthropicChatCompletionsService(IHttpClientFactory httpClientFactor
|
|||||||
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
||||||
{
|
{
|
||||||
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||||
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}", options.Endpoint,
|
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}",
|
||||||
|
options.Endpoint,
|
||||||
response.StatusCode, error);
|
response.StatusCode, error);
|
||||||
|
|
||||||
throw new Exception("OpenAI对话异常" + response.StatusCode);
|
throw new Exception("OpenAI对话异常" + response.StatusCode);
|
||||||
|
|||||||
@@ -0,0 +1,878 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
using System.Text.Json;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorClaude.Chats;
|
||||||
|
|
||||||
|
public sealed class ClaudiaChatCompletionsService(
|
||||||
|
IHttpClientFactory httpClientFactory,
|
||||||
|
ILogger<ClaudiaChatCompletionsService> logger)
|
||||||
|
: IChatCompletionService
|
||||||
|
{
|
||||||
|
public List<ThorChatChoiceResponse> CreateResponse(AnthropicChatCompletionDto completionDto)
|
||||||
|
{
|
||||||
|
var response = new ThorChatChoiceResponse();
|
||||||
|
var chatMessage = new ThorChatMessage();
|
||||||
|
if (completionDto == null)
|
||||||
|
{
|
||||||
|
return new List<ThorChatChoiceResponse>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (completionDto.content.Any(x => x.type.Equals("thinking", StringComparison.OrdinalIgnoreCase)))
|
||||||
|
{
|
||||||
|
// 将推理字段合并到返回对象去
|
||||||
|
chatMessage.ReasoningContent = completionDto.content
|
||||||
|
.First(x => x.type.Equals("thinking", StringComparison.OrdinalIgnoreCase)).Thinking;
|
||||||
|
|
||||||
|
chatMessage.Role = completionDto.role;
|
||||||
|
chatMessage.Content = completionDto.content
|
||||||
|
.First(x => x.type.Equals("text", StringComparison.OrdinalIgnoreCase)).text;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
chatMessage.Role = completionDto.role;
|
||||||
|
chatMessage.Content = completionDto.content
|
||||||
|
.FirstOrDefault()?.text;
|
||||||
|
}
|
||||||
|
|
||||||
|
response.Delta = chatMessage;
|
||||||
|
response.Message = chatMessage;
|
||||||
|
|
||||||
|
if (completionDto.content.Any(x => x.type.Equals("tool_use", StringComparison.OrdinalIgnoreCase)))
|
||||||
|
{
|
||||||
|
var toolUse = completionDto.content
|
||||||
|
.First(x => x.type.Equals("tool_use", StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
|
chatMessage.ToolCalls =
|
||||||
|
[
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Id = toolUse.id,
|
||||||
|
Function = new ThorChatMessageFunction()
|
||||||
|
{
|
||||||
|
Name = toolUse.name,
|
||||||
|
Arguments = JsonSerializer.Serialize(toolUse.input,
|
||||||
|
ThorJsonSerializer.DefaultOptions),
|
||||||
|
},
|
||||||
|
Index = 0,
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
return
|
||||||
|
[
|
||||||
|
response
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return new List<ThorChatChoiceResponse> { response };
|
||||||
|
}
|
||||||
|
|
||||||
|
private object CreateMessage(List<ThorChatMessage> messages, AiModelDescribe options)
|
||||||
|
{
|
||||||
|
var list = new List<object>();
|
||||||
|
|
||||||
|
foreach (var message in messages)
|
||||||
|
{
|
||||||
|
// 如果是图片
|
||||||
|
if (message.ContentCalculated is IList<ThorChatMessageContent> contentCalculated)
|
||||||
|
{
|
||||||
|
list.Add(new
|
||||||
|
{
|
||||||
|
role = message.Role,
|
||||||
|
content = (List<object>)contentCalculated.Select<ThorChatMessageContent, object>(x =>
|
||||||
|
{
|
||||||
|
if (x.Type == "text")
|
||||||
|
{
|
||||||
|
if ("true".Equals(options.ModelExtraInfo, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
type = "text",
|
||||||
|
text = x.Text,
|
||||||
|
cache_control = new
|
||||||
|
{
|
||||||
|
type = "ephemeral"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
type = "text",
|
||||||
|
text = x.Text
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
var isBase64 = x.ImageUrl?.Url.StartsWith("http") == true;
|
||||||
|
|
||||||
|
if ("true".Equals(options.ModelExtraInfo, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
type = "image",
|
||||||
|
source = new
|
||||||
|
{
|
||||||
|
type = isBase64 ? "base64" : "url",
|
||||||
|
media_type = "image/png",
|
||||||
|
data = x.ImageUrl?.Url,
|
||||||
|
},
|
||||||
|
cache_control = new
|
||||||
|
{
|
||||||
|
type = "ephemeral"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
type = "image",
|
||||||
|
source = new
|
||||||
|
{
|
||||||
|
type = isBase64 ? "base64" : "url",
|
||||||
|
media_type = "image/png",
|
||||||
|
data = x.ImageUrl?.Url,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ("true".Equals(options.ModelExtraInfo, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
if (message.Role == "system")
|
||||||
|
{
|
||||||
|
list.Add(new
|
||||||
|
{
|
||||||
|
type = "text",
|
||||||
|
text = message.Content,
|
||||||
|
cache_control = new
|
||||||
|
{
|
||||||
|
type = "ephemeral"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
list.Add(new
|
||||||
|
{
|
||||||
|
role = message.Role,
|
||||||
|
content = message.Content
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (message.Role == "system")
|
||||||
|
{
|
||||||
|
list.Add(new
|
||||||
|
{
|
||||||
|
type = "text",
|
||||||
|
text = message.Content
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (message.Role == "tool")
|
||||||
|
{
|
||||||
|
list.Add(new
|
||||||
|
{
|
||||||
|
role = "user",
|
||||||
|
content = new List<object>
|
||||||
|
{
|
||||||
|
new
|
||||||
|
{
|
||||||
|
type = "tool_result",
|
||||||
|
tool_use_id = message.ToolCallId,
|
||||||
|
content = message.Content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (message.Role == "assistant")
|
||||||
|
{
|
||||||
|
// {
|
||||||
|
// "role": "assistant",
|
||||||
|
// "content": [
|
||||||
|
// {
|
||||||
|
// "type": "text",
|
||||||
|
// "text": "<thinking>I need to use get_weather, and the user wants SF, which is likely San Francisco, CA.</thinking>"
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// "type": "tool_use",
|
||||||
|
// "id": "toolu_01A09q90qw90lq917835lq9",
|
||||||
|
// "name": "get_weather",
|
||||||
|
// "input": {
|
||||||
|
// "location": "San Francisco, CA",
|
||||||
|
// "unit": "celsius"
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
|
// },
|
||||||
|
if (message.ToolCalls?.Count > 0)
|
||||||
|
{
|
||||||
|
var content = new List<object>();
|
||||||
|
if (!string.IsNullOrEmpty(message.Content))
|
||||||
|
{
|
||||||
|
content.Add(new
|
||||||
|
{
|
||||||
|
type = "text",
|
||||||
|
text = message.Content
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var toolCall in message.ToolCalls)
|
||||||
|
{
|
||||||
|
content.Add(new
|
||||||
|
{
|
||||||
|
type = "tool_use",
|
||||||
|
id = toolCall.Id,
|
||||||
|
name = toolCall.Function?.Name,
|
||||||
|
input = JsonSerializer.Deserialize<Dictionary<string, object>>(
|
||||||
|
toolCall.Function?.Arguments, ThorJsonSerializer.DefaultOptions)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
list.Add(new
|
||||||
|
{
|
||||||
|
role = "assistant",
|
||||||
|
content
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
list.Add(new
|
||||||
|
{
|
||||||
|
role = "assistant",
|
||||||
|
content = new List<object>
|
||||||
|
{
|
||||||
|
new
|
||||||
|
{
|
||||||
|
type = "text",
|
||||||
|
text = message.Content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
list.Add(new
|
||||||
|
{
|
||||||
|
role = message.Role,
|
||||||
|
content = message.Content
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async IAsyncEnumerable<ThorChatCompletionsResponse> CompleteChatStreamAsync(AiModelDescribe options,
|
||||||
|
ThorChatCompletionsRequest input,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
using var openai =
|
||||||
|
Activity.Current?.Source.StartActivity("Claudia 对话补全");
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(options.Endpoint))
|
||||||
|
{
|
||||||
|
options.Endpoint = "https://api.anthropic.com/";
|
||||||
|
}
|
||||||
|
|
||||||
|
var client = httpClientFactory.CreateClient();
|
||||||
|
|
||||||
|
var headers = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "x-api-key", options.ApiKey },
|
||||||
|
{ "anthropic-version", "2023-06-01" }
|
||||||
|
};
|
||||||
|
|
||||||
|
var isThinking = input.Model.EndsWith("thinking");
|
||||||
|
input.Model = input.Model.Replace("-thinking", string.Empty);
|
||||||
|
var budgetTokens = 1024;
|
||||||
|
|
||||||
|
if (input.MaxTokens is < 2048)
|
||||||
|
{
|
||||||
|
input.MaxTokens = 2048;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.MaxTokens != null && input.MaxTokens / 2 < 1024)
|
||||||
|
{
|
||||||
|
budgetTokens = input.MaxTokens.Value / (4 * 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
// budgetTokens最大4096
|
||||||
|
budgetTokens = Math.Min(budgetTokens, 4096);
|
||||||
|
|
||||||
|
object tool_choice;
|
||||||
|
if (input.ToolChoice is not null && input.ToolChoice.Type == "auto")
|
||||||
|
{
|
||||||
|
tool_choice = new
|
||||||
|
{
|
||||||
|
type = "auto",
|
||||||
|
disable_parallel_tool_use = false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (input.ToolChoice is not null && input.ToolChoice.Type == "any")
|
||||||
|
{
|
||||||
|
tool_choice = new
|
||||||
|
{
|
||||||
|
type = "any",
|
||||||
|
disable_parallel_tool_use = false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (input.ToolChoice is not null && input.ToolChoice.Type == "tool")
|
||||||
|
{
|
||||||
|
tool_choice = new
|
||||||
|
{
|
||||||
|
type = "tool",
|
||||||
|
name = input.ToolChoice.Function?.Name,
|
||||||
|
disable_parallel_tool_use = false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tool_choice = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var response = await client.HttpRequestRaw(options.Endpoint.TrimEnd('/') + "/v1/messages", new
|
||||||
|
{
|
||||||
|
model = input.Model,
|
||||||
|
max_tokens = input.MaxTokens ?? 64000,
|
||||||
|
stream = true,
|
||||||
|
tool_choice,
|
||||||
|
system = CreateMessage(input.Messages.Where(x => x.Role == "system").ToList(), options),
|
||||||
|
messages = CreateMessage(input.Messages.Where(x => x.Role != "system").ToList(), options),
|
||||||
|
top_p = isThinking ? null : input.TopP,
|
||||||
|
thinking = isThinking
|
||||||
|
? new
|
||||||
|
{
|
||||||
|
type = "enabled",
|
||||||
|
budget_tokens = budgetTokens,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
tools = input.Tools?.Select(x => new
|
||||||
|
{
|
||||||
|
name = x.Function?.Name,
|
||||||
|
description = x.Function?.Description,
|
||||||
|
input_schema = new
|
||||||
|
{
|
||||||
|
type = x.Function?.Parameters?.Type,
|
||||||
|
required = x.Function?.Parameters?.Required,
|
||||||
|
properties = x.Function?.Parameters?.Properties?.ToDictionary(y => y.Key, y => new
|
||||||
|
{
|
||||||
|
description = y.Value.Description,
|
||||||
|
type = y.Value.Type,
|
||||||
|
@enum = y.Value.Enum
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}).ToArray(),
|
||||||
|
temperature = isThinking ? null : input.Temperature
|
||||||
|
}, string.Empty, headers);
|
||||||
|
|
||||||
|
openai?.SetTag("Model", input.Model);
|
||||||
|
openai?.SetTag("Response", response.StatusCode.ToString());
|
||||||
|
|
||||||
|
// 大于等于400的状态码都认为是异常
|
||||||
|
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
||||||
|
{
|
||||||
|
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}",
|
||||||
|
options.Endpoint,
|
||||||
|
response.StatusCode, error);
|
||||||
|
|
||||||
|
throw new Exception("OpenAI对话异常" + response.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
using var stream = new StreamReader(await response.Content.ReadAsStreamAsync(cancellationToken));
|
||||||
|
|
||||||
|
using StreamReader reader = new(await response.Content.ReadAsStreamAsync(cancellationToken));
|
||||||
|
string? line = string.Empty;
|
||||||
|
var first = true;
|
||||||
|
var isThink = false;
|
||||||
|
|
||||||
|
string? toolId = null;
|
||||||
|
string? toolName = null;
|
||||||
|
while ((line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false)) != null)
|
||||||
|
{
|
||||||
|
line += Environment.NewLine;
|
||||||
|
|
||||||
|
if (line.StartsWith('{'))
|
||||||
|
{
|
||||||
|
logger.LogInformation("OpenAI对话异常 , StatusCode: {StatusCode} Response: {Response}", response.StatusCode,
|
||||||
|
line);
|
||||||
|
|
||||||
|
throw new Exception("OpenAI对话异常" + line);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.StartsWith(OpenAIConstant.Data))
|
||||||
|
line = line[OpenAIConstant.Data.Length..];
|
||||||
|
|
||||||
|
line = line.Trim();
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(line)) continue;
|
||||||
|
|
||||||
|
if (line == OpenAIConstant.Done)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.StartsWith(':'))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (line.StartsWith("event: ", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = JsonSerializer.Deserialize<AnthropicStreamDto>(line,
|
||||||
|
ThorJsonSerializer.DefaultOptions);
|
||||||
|
|
||||||
|
if (result?.Type == "content_block_delta")
|
||||||
|
{
|
||||||
|
if (result.Delta.Type is "text" or "text_delta")
|
||||||
|
{
|
||||||
|
yield return new ThorChatCompletionsResponse()
|
||||||
|
{
|
||||||
|
Choices =
|
||||||
|
[
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Message = new ThorChatMessage()
|
||||||
|
{
|
||||||
|
Content = result.Delta.Text,
|
||||||
|
Role = "assistant",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Model = input.Model,
|
||||||
|
Id = result?.Message?.id,
|
||||||
|
Usage = new ThorUsageResponse()
|
||||||
|
{
|
||||||
|
CompletionTokens = result?.Message?.Usage?.OutputTokens,
|
||||||
|
PromptTokens = result?.Message?.Usage?.InputTokens,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (result.Delta.Type == "input_json_delta")
|
||||||
|
{
|
||||||
|
yield return new ThorChatCompletionsResponse()
|
||||||
|
{
|
||||||
|
Choices =
|
||||||
|
[
|
||||||
|
new ThorChatChoiceResponse()
|
||||||
|
{
|
||||||
|
Message = new ThorChatMessage()
|
||||||
|
{
|
||||||
|
ToolCalls =
|
||||||
|
[
|
||||||
|
new ThorToolCall()
|
||||||
|
{
|
||||||
|
Id = toolId,
|
||||||
|
Function = new ThorChatMessageFunction()
|
||||||
|
{
|
||||||
|
Name = toolName,
|
||||||
|
Arguments = result.Delta.PartialJson
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Role = "tool",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Model = input.Model,
|
||||||
|
Usage = new ThorUsageResponse()
|
||||||
|
{
|
||||||
|
PromptTokens = result?.Message?.Usage?.InputTokens,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
yield return new ThorChatCompletionsResponse()
|
||||||
|
{
|
||||||
|
Choices = new List<ThorChatChoiceResponse>()
|
||||||
|
{
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Message = new ThorChatMessage()
|
||||||
|
{
|
||||||
|
ReasoningContent = result.Delta.Thinking,
|
||||||
|
Role = "assistant",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Model = input.Model,
|
||||||
|
Id = result?.Message?.id,
|
||||||
|
Usage = new ThorUsageResponse()
|
||||||
|
{
|
||||||
|
CompletionTokens = result?.Message?.Usage?.OutputTokens,
|
||||||
|
PromptTokens = result?.Message?.Usage?.InputTokens
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result?.Type == "content_block_start")
|
||||||
|
{
|
||||||
|
if (result?.ContentBlock?.Id is not null)
|
||||||
|
{
|
||||||
|
toolId = result.ContentBlock.Id;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result?.ContentBlock?.Name is not null)
|
||||||
|
{
|
||||||
|
toolName = result.ContentBlock.Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toolId is null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
yield return new ThorChatCompletionsResponse()
|
||||||
|
{
|
||||||
|
Choices =
|
||||||
|
[
|
||||||
|
new ThorChatChoiceResponse()
|
||||||
|
{
|
||||||
|
Message = new ThorChatMessage()
|
||||||
|
{
|
||||||
|
ToolCalls =
|
||||||
|
[
|
||||||
|
new ThorToolCall()
|
||||||
|
{
|
||||||
|
Id = toolId,
|
||||||
|
Function = new ThorChatMessageFunction()
|
||||||
|
{
|
||||||
|
Name = toolName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Role = "tool",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Model = input.Model,
|
||||||
|
Usage = new ThorUsageResponse()
|
||||||
|
{
|
||||||
|
PromptTokens = result?.Message?.Usage?.InputTokens,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.Type == "content_block_delta")
|
||||||
|
{
|
||||||
|
yield return new ThorChatCompletionsResponse()
|
||||||
|
{
|
||||||
|
Choices =
|
||||||
|
[
|
||||||
|
new ThorChatChoiceResponse()
|
||||||
|
{
|
||||||
|
Message = new ThorChatMessage()
|
||||||
|
{
|
||||||
|
ToolCallId = result?.ContentBlock?.Id,
|
||||||
|
FunctionCall = new ThorChatMessageFunction()
|
||||||
|
{
|
||||||
|
Name = result?.ContentBlock?.Name,
|
||||||
|
Arguments = result?.Delta?.PartialJson
|
||||||
|
},
|
||||||
|
Role = "tool",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Model = input.Model,
|
||||||
|
Usage = new ThorUsageResponse()
|
||||||
|
{
|
||||||
|
PromptTokens = result?.Message?.Usage?.InputTokens
|
||||||
|
}
|
||||||
|
};
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.Type == "message_start")
|
||||||
|
{
|
||||||
|
yield return new ThorChatCompletionsResponse()
|
||||||
|
{
|
||||||
|
Choices =
|
||||||
|
[
|
||||||
|
new ThorChatChoiceResponse()
|
||||||
|
{
|
||||||
|
Message = new ThorChatMessage()
|
||||||
|
{
|
||||||
|
Content = result?.Delta?.Text,
|
||||||
|
Role = "assistant",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Model = input.Model,
|
||||||
|
Usage = new ThorUsageResponse()
|
||||||
|
{
|
||||||
|
PromptTokens = result?.Message?.Usage?.InputTokens,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.Type == "message_delta")
|
||||||
|
{
|
||||||
|
var deltaOutput = new ThorChatCompletionsResponse()
|
||||||
|
{
|
||||||
|
Choices =
|
||||||
|
[
|
||||||
|
new ThorChatChoiceResponse()
|
||||||
|
{
|
||||||
|
Message = new ThorChatMessage()
|
||||||
|
{
|
||||||
|
Content = result.Delta?.Text,
|
||||||
|
Role = "assistant",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Model = input.Model,
|
||||||
|
Usage = new ThorUsageResponse
|
||||||
|
{
|
||||||
|
InputTokens = result.Usage?.InputTokens + result.Usage?.CacheCreationInputTokens +
|
||||||
|
result.Usage?.CacheReadInputTokens,
|
||||||
|
OutputTokens = result.Usage?.OutputTokens,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
deltaOutput.Usage.PromptTokens = deltaOutput.Usage.InputTokens;
|
||||||
|
deltaOutput.Usage.CompletionTokens = deltaOutput.Usage.OutputTokens;
|
||||||
|
|
||||||
|
deltaOutput.Usage.TotalTokens = deltaOutput.Usage.InputTokens + deltaOutput.Usage.OutputTokens;
|
||||||
|
|
||||||
|
yield return deltaOutput;
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.Message == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var chat = CreateResponse(result.Message);
|
||||||
|
|
||||||
|
var content = chat?.FirstOrDefault()?.Delta;
|
||||||
|
|
||||||
|
if (first && string.IsNullOrWhiteSpace(content?.Content) && string.IsNullOrEmpty(content?.ReasoningContent))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (first && content.Content == OpenAIConstant.ThinkStart)
|
||||||
|
{
|
||||||
|
isThink = true;
|
||||||
|
continue;
|
||||||
|
// 需要将content的内容转换到其他字段
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isThink && content.Content.Contains(OpenAIConstant.ThinkEnd))
|
||||||
|
{
|
||||||
|
isThink = false;
|
||||||
|
// 需要将content的内容转换到其他字段
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isThink)
|
||||||
|
{
|
||||||
|
// 需要将content的内容转换到其他字段
|
||||||
|
foreach (var choice in chat)
|
||||||
|
{
|
||||||
|
choice.Delta.ReasoningContent = choice.Delta.Content;
|
||||||
|
choice.Delta.Content = string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
first = false;
|
||||||
|
|
||||||
|
var output = new ThorChatCompletionsResponse()
|
||||||
|
{
|
||||||
|
Choices = chat,
|
||||||
|
Model = input.Model,
|
||||||
|
Id = result.Message.id,
|
||||||
|
Usage = new ThorUsageResponse()
|
||||||
|
{
|
||||||
|
InputTokens = result.Message.Usage?.InputTokens + result.Message.Usage?.CacheCreationInputTokens +
|
||||||
|
result.Message.Usage?.CacheReadInputTokens,
|
||||||
|
OutputTokens = result.Message.Usage?.OutputTokens,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
output.Usage.PromptTokens = output.Usage.InputTokens;
|
||||||
|
output.Usage.CompletionTokens = output.Usage.OutputTokens;
|
||||||
|
output.Usage.TotalTokens = output.Usage.InputTokens + output.Usage.OutputTokens;
|
||||||
|
|
||||||
|
yield return output;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ThorChatCompletionsResponse> CompleteChatAsync(AiModelDescribe options,
|
||||||
|
ThorChatCompletionsRequest input,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
using var openai =
|
||||||
|
Activity.Current?.Source.StartActivity("Claudia 对话补全");
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(options.Endpoint))
|
||||||
|
{
|
||||||
|
options.Endpoint = "https://api.anthropic.com/";
|
||||||
|
}
|
||||||
|
|
||||||
|
var client = httpClientFactory.CreateClient();
|
||||||
|
|
||||||
|
var headers = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "x-api-key", options.ApiKey },
|
||||||
|
{ "anthropic-version", "2023-06-01" }
|
||||||
|
};
|
||||||
|
|
||||||
|
bool isThink = input.Model.EndsWith("-thinking");
|
||||||
|
input.Model = input.Model.Replace("-thinking", string.Empty);
|
||||||
|
|
||||||
|
var budgetTokens = 1024;
|
||||||
|
if (input.MaxTokens is < 2048)
|
||||||
|
{
|
||||||
|
input.MaxTokens = 2048;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.MaxTokens != null && input.MaxTokens / 2 < 1024)
|
||||||
|
{
|
||||||
|
budgetTokens = input.MaxTokens.Value / (4 * 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
object tool_choice;
|
||||||
|
if (input.ToolChoice is not null && input.ToolChoice.Type == "auto")
|
||||||
|
{
|
||||||
|
tool_choice = new
|
||||||
|
{
|
||||||
|
type = "auto",
|
||||||
|
disable_parallel_tool_use = false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (input.ToolChoice is not null && input.ToolChoice.Type == "any")
|
||||||
|
{
|
||||||
|
tool_choice = new
|
||||||
|
{
|
||||||
|
type = "any",
|
||||||
|
disable_parallel_tool_use = false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (input.ToolChoice is not null && input.ToolChoice.Type == "tool")
|
||||||
|
{
|
||||||
|
tool_choice = new
|
||||||
|
{
|
||||||
|
type = "tool",
|
||||||
|
name = input.ToolChoice.Function?.Name,
|
||||||
|
disable_parallel_tool_use = false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tool_choice = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// budgetTokens最大4096
|
||||||
|
budgetTokens = Math.Min(budgetTokens, 4096);
|
||||||
|
|
||||||
|
var response = await client.PostJsonAsync(options.Endpoint.TrimEnd('/') + "/v1/messages", new
|
||||||
|
{
|
||||||
|
model = input.Model,
|
||||||
|
max_tokens = input.MaxTokens ?? 2000,
|
||||||
|
system = CreateMessage(input.Messages.Where(x => x.Role == "system").ToList(), options),
|
||||||
|
messages = CreateMessage(input.Messages.Where(x => x.Role != "system").ToList(), options),
|
||||||
|
top_p = isThink ? null : input.TopP,
|
||||||
|
tool_choice,
|
||||||
|
thinking = isThink
|
||||||
|
? new
|
||||||
|
{
|
||||||
|
type = "enabled",
|
||||||
|
budget_tokens = budgetTokens,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
tools = input.Tools?.Select(x => new
|
||||||
|
{
|
||||||
|
name = x.Function?.Name,
|
||||||
|
description = x.Function?.Description,
|
||||||
|
input_schema = new
|
||||||
|
{
|
||||||
|
type = x.Function?.Parameters?.Type,
|
||||||
|
required = x.Function?.Parameters?.Required,
|
||||||
|
properties = x.Function?.Parameters?.Properties?.ToDictionary(y => y.Key, y => new
|
||||||
|
{
|
||||||
|
description = y.Value.Description,
|
||||||
|
type = y.Value.Type,
|
||||||
|
@enum = y.Value.Enum
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}).ToArray(),
|
||||||
|
temperature = isThink ? null : input.Temperature
|
||||||
|
}, string.Empty, headers);
|
||||||
|
|
||||||
|
openai?.SetTag("Model", input.Model);
|
||||||
|
openai?.SetTag("Response", response.StatusCode.ToString());
|
||||||
|
|
||||||
|
// 大于等于400的状态码都认为是异常
|
||||||
|
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
||||||
|
{
|
||||||
|
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}",
|
||||||
|
options.Endpoint,
|
||||||
|
response.StatusCode, error);
|
||||||
|
|
||||||
|
throw new Exception("OpenAI对话异常" + response.StatusCode.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
var value =
|
||||||
|
await response.Content.ReadFromJsonAsync<AnthropicChatCompletionDto>(ThorJsonSerializer.DefaultOptions,
|
||||||
|
cancellationToken: cancellationToken);
|
||||||
|
|
||||||
|
var thor = new ThorChatCompletionsResponse()
|
||||||
|
{
|
||||||
|
Choices = CreateResponse(value),
|
||||||
|
Model = input.Model,
|
||||||
|
Id = value.id,
|
||||||
|
Usage = new ThorUsageResponse()
|
||||||
|
{
|
||||||
|
CompletionTokens = value.Usage.OutputTokens,
|
||||||
|
PromptTokens = value.Usage.InputTokens
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (value.Usage.CacheReadInputTokens != null)
|
||||||
|
{
|
||||||
|
thor.Usage.PromptTokensDetails ??= new ThorUsageResponsePromptTokensDetails()
|
||||||
|
{
|
||||||
|
CachedTokens = value.Usage.CacheReadInputTokens.Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (value.Usage.InputTokens > 0)
|
||||||
|
{
|
||||||
|
thor.Usage.InputTokens = value.Usage.InputTokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.Usage.OutputTokens > 0)
|
||||||
|
{
|
||||||
|
thor.Usage.CompletionTokens = value.Usage.OutputTokens;
|
||||||
|
thor.Usage.OutputTokens = value.Usage.OutputTokens;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
thor.Usage.TotalTokens = thor.Usage.InputTokens + thor.Usage.OutputTokens;
|
||||||
|
return thor;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
using Volo.Abp.Domain.Entities.Auditing;
|
using Volo.Abp.Domain.Entities.Auditing;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||||
|
|
||||||
namespace Yi.Framework.AiHub.Domain.Entities;
|
namespace Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
|
||||||
@@ -38,4 +39,9 @@ public class AiRechargeAggregateRoot : FullAuditedAggregateRoot<Guid>
|
|||||||
/// 联系方式
|
/// 联系方式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? ContactInfo { get; set; }
|
public string? ContactInfo { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 订单类型
|
||||||
|
/// </summary>
|
||||||
|
public RechargeTypeEnum RechargeType { get; set; }
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using Volo.Abp.Domain.Entities.Auditing;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 公告日志
|
||||||
|
/// </summary>
|
||||||
|
[SugarTable("Ai_Announcement")]
|
||||||
|
public class AnnouncementAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||||
|
{
|
||||||
|
public AnnouncementAggregateRoot()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 标题
|
||||||
|
/// </summary>
|
||||||
|
public string Title { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 内容列表(JSON格式存储)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(IsJson = true, IsNullable = false)]
|
||||||
|
public List<string> Content { get; set; } = new List<string>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 备注
|
||||||
|
/// </summary>
|
||||||
|
public string? Remark { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 图片url
|
||||||
|
/// </summary>
|
||||||
|
public string? ImageUrl { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开始时间(系统公告时间、活动开始时间)
|
||||||
|
/// </summary>
|
||||||
|
public DateTime StartTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 活动结束时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? EndTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 公告类型(系统、活动)
|
||||||
|
/// </summary>
|
||||||
|
public AnnouncementTypeEnum Type{ get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 跳转链接
|
||||||
|
/// </summary>
|
||||||
|
public string? Url { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using Volo.Abp.Domain.Entities.Auditing;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌任务记录
|
||||||
|
/// </summary>
|
||||||
|
[SugarTable("Ai_CardFlipTask")]
|
||||||
|
[SugarIndex($"index_{nameof(UserId)}_{nameof(WeekStartDate)}",
|
||||||
|
nameof(UserId), OrderByType.Asc,
|
||||||
|
nameof(WeekStartDate), OrderByType.Desc)]
|
||||||
|
public class CardFlipTaskAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||||
|
{
|
||||||
|
public CardFlipTaskAggregateRoot()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public CardFlipTaskAggregateRoot(Guid userId, DateTime weekStartDate)
|
||||||
|
{
|
||||||
|
UserId = userId;
|
||||||
|
WeekStartDate = weekStartDate.Date; // 确保只存储日期部分
|
||||||
|
TotalFlips = 0;
|
||||||
|
FreeFlipsUsed = 0;
|
||||||
|
BonusFlipsUsed = 0;
|
||||||
|
InviteFlipsUsed = 0;
|
||||||
|
IsFirstFlipDone = false;
|
||||||
|
FlippedOrder = new List<int>();
|
||||||
|
WinRecords = new Dictionary<int, long>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用户ID
|
||||||
|
/// </summary>
|
||||||
|
public Guid UserId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 本周开始日期(每周一)
|
||||||
|
/// </summary>
|
||||||
|
public DateTime WeekStartDate { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 总共已翻牌次数
|
||||||
|
/// </summary>
|
||||||
|
public int TotalFlips { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 已使用的免费次数(最多7次)
|
||||||
|
/// </summary>
|
||||||
|
public int FreeFlipsUsed { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 已使用的赠送次数(已废弃,保持为0)
|
||||||
|
/// </summary>
|
||||||
|
public int BonusFlipsUsed { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 已使用的邀请解锁次数(最多3次)
|
||||||
|
/// </summary>
|
||||||
|
public int InviteFlipsUsed { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否已完成首次翻牌(用于判断是否创建任务)
|
||||||
|
/// </summary>
|
||||||
|
public bool IsFirstFlipDone { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 中奖记录(以翻牌顺序为key,例如第1次翻牌中奖则key为1,奖励金额为value)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(IsJson = true, IsNullable = true)]
|
||||||
|
public Dictionary<int, long>? WinRecords { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 备注信息
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(Length = 500, IsNullable = true)]
|
||||||
|
public string? Remark { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 已翻牌的顺序(存储用户实际翻牌的序号列表,如[3,7,1,5]表示依次翻了3号、7号、1号、5号牌)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(IsJson = true, IsNullable = true)]
|
||||||
|
public List<int>? FlippedOrder { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 增加翻牌次数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="flipType">翻牌类型</param>
|
||||||
|
public void IncrementFlip(FlipType flipType)
|
||||||
|
{
|
||||||
|
TotalFlips++;
|
||||||
|
|
||||||
|
switch (flipType)
|
||||||
|
{
|
||||||
|
case FlipType.Free:
|
||||||
|
FreeFlipsUsed++;
|
||||||
|
break;
|
||||||
|
case FlipType.Bonus:
|
||||||
|
BonusFlipsUsed++;
|
||||||
|
break;
|
||||||
|
case FlipType.Invite:
|
||||||
|
InviteFlipsUsed++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!IsFirstFlipDone)
|
||||||
|
{
|
||||||
|
IsFirstFlipDone = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录中奖
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="flipCount">第几次翻牌(1-10)</param>
|
||||||
|
/// <param name="amount">奖励金额</param>
|
||||||
|
public void SetWinReward(int flipCount, long amount)
|
||||||
|
{
|
||||||
|
if (WinRecords == null)
|
||||||
|
{
|
||||||
|
WinRecords = new Dictionary<int, long>();
|
||||||
|
}
|
||||||
|
WinRecords[flipCount] = amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌类型枚举
|
||||||
|
/// </summary>
|
||||||
|
public enum FlipType
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 免费翻牌(1-7次)
|
||||||
|
/// </summary>
|
||||||
|
Free = 0,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 赠送翻牌(已废弃)
|
||||||
|
/// </summary>
|
||||||
|
Bonus = 1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请解锁翻牌(8-10次)
|
||||||
|
/// </summary>
|
||||||
|
Invite = 2
|
||||||
|
}
|
||||||
@@ -20,11 +20,13 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot<Guid>
|
|||||||
}
|
}
|
||||||
|
|
||||||
public MessageAggregateRoot(Guid? userId, Guid? sessionId, string content, string role, string modelId,
|
public MessageAggregateRoot(Guid? userId, Guid? sessionId, string content, string role, string modelId,
|
||||||
ThorUsageResponse? tokenUsage)
|
ThorUsageResponse? tokenUsage, Guid? tokenId = null)
|
||||||
{
|
{
|
||||||
UserId = userId;
|
UserId = userId;
|
||||||
SessionId = sessionId;
|
SessionId = sessionId;
|
||||||
Content = content;
|
TokenId = tokenId ?? Guid.Empty;
|
||||||
|
//如果没有会话,不存储对话内容
|
||||||
|
Content = sessionId is null ? null : content;
|
||||||
Role = role;
|
Role = role;
|
||||||
ModelId = modelId;
|
ModelId = modelId;
|
||||||
if (tokenUsage is not null)
|
if (tokenUsage is not null)
|
||||||
@@ -58,6 +60,11 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot<Guid>
|
|||||||
public Guid? UserId { get; set; }
|
public Guid? UserId { get; set; }
|
||||||
public Guid? SessionId { get; set; }
|
public Guid? SessionId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Token密钥Id(通过API调用时记录,Web调用为Guid.Empty)
|
||||||
|
/// </summary>
|
||||||
|
public Guid TokenId { get; set; }
|
||||||
|
|
||||||
[SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)]
|
[SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)]
|
||||||
public string? Content { get; set; }
|
public string? Content { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using Volo.Abp.Domain.Entities.Auditing;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 每日任务奖励领取记录
|
||||||
|
/// </summary>
|
||||||
|
[SugarTable("Ai_DailyTaskRewardRecord")]
|
||||||
|
[SugarIndex($"index_{nameof(UserId)}_{nameof(TaskDate)}",
|
||||||
|
nameof(UserId), OrderByType.Asc,
|
||||||
|
nameof(TaskDate), OrderByType.Desc)]
|
||||||
|
public class DailyTaskRewardRecordAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||||
|
{
|
||||||
|
public DailyTaskRewardRecordAggregateRoot()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public DailyTaskRewardRecordAggregateRoot(Guid userId, int taskLevel, DateTime taskDate, long rewardTokens)
|
||||||
|
{
|
||||||
|
UserId = userId;
|
||||||
|
TaskLevel = taskLevel;
|
||||||
|
TaskDate = taskDate.Date; // 确保只存储日期部分
|
||||||
|
RewardTokens = rewardTokens;
|
||||||
|
IsRewarded = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用户ID
|
||||||
|
/// </summary>
|
||||||
|
public Guid UserId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 任务等级(1=1000w任务,2=3000w任务)
|
||||||
|
/// </summary>
|
||||||
|
public int TaskLevel { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 任务日期(只包含日期,不包含时间)
|
||||||
|
/// </summary>
|
||||||
|
public DateTime TaskDate { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 奖励的Token数量
|
||||||
|
/// </summary>
|
||||||
|
public long RewardTokens { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否已发放奖励
|
||||||
|
/// </summary>
|
||||||
|
public bool IsRewarded { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 备注信息
|
||||||
|
/// </summary>
|
||||||
|
public string? Remark { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using Volo.Abp.Domain.Entities.Auditing;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请记录
|
||||||
|
/// </summary>
|
||||||
|
[SugarTable("Ai_InvitationRecord")]
|
||||||
|
[SugarIndex($"index_{nameof(InviterId)}_{nameof(InvitedUserId)}",
|
||||||
|
nameof(InviterId), OrderByType.Asc,
|
||||||
|
nameof(InvitedUserId), OrderByType.Asc)]
|
||||||
|
[SugarIndex($"index_{nameof(InvitedUserId)}", nameof(InvitedUserId), OrderByType.Asc)]
|
||||||
|
public class InvitationRecordAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||||
|
{
|
||||||
|
public InvitationRecordAggregateRoot()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvitationRecordAggregateRoot(Guid inviterId, Guid invitedUserId, string inviteCode)
|
||||||
|
{
|
||||||
|
InviterId = inviterId;
|
||||||
|
InvitedUserId = invitedUserId;
|
||||||
|
InviteCode = inviteCode;
|
||||||
|
InvitationTime = DateTime.Now;
|
||||||
|
Status = InvitationStatus.Valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请人ID
|
||||||
|
/// </summary>
|
||||||
|
public Guid InviterId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 被邀请人ID
|
||||||
|
/// </summary>
|
||||||
|
public Guid InvitedUserId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用的邀请码
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(Length = 50)]
|
||||||
|
public string InviteCode { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime InvitationTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请状态(0=有效,1=已撤销)
|
||||||
|
/// </summary>
|
||||||
|
public InvitationStatus Status { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 备注信息
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(Length = 500, IsNullable = true)]
|
||||||
|
public string? Remark { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请状态枚举
|
||||||
|
/// </summary>
|
||||||
|
public enum InvitationStatus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 有效
|
||||||
|
/// </summary>
|
||||||
|
Valid = 0,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 已撤销
|
||||||
|
/// </summary>
|
||||||
|
Revoked = 1
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using Volo.Abp.Domain.Entities.Auditing;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用户邀请码
|
||||||
|
/// </summary>
|
||||||
|
[SugarTable("Ai_InviteCode")]
|
||||||
|
[SugarIndex($"index_{nameof(UserId)}", nameof(UserId), OrderByType.Asc, true)]
|
||||||
|
[SugarIndex($"index_{nameof(Code)}", nameof(Code), OrderByType.Asc, true)]
|
||||||
|
public class InviteCodeAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||||
|
{
|
||||||
|
public InviteCodeAggregateRoot()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public InviteCodeAggregateRoot(Guid userId, string code)
|
||||||
|
{
|
||||||
|
UserId = userId;
|
||||||
|
Code = code;
|
||||||
|
UsedCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用户ID(邀请码拥有者)
|
||||||
|
/// </summary>
|
||||||
|
public Guid UserId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请码(唯一)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(Length = 50)]
|
||||||
|
public string Code { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 被使用次数(统计用,一个邀请码可以被多人使用)
|
||||||
|
/// </summary>
|
||||||
|
public int UsedCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 备注信息
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(Length = 500, IsNullable = true)]
|
||||||
|
public string? Remark { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
@@ -52,7 +52,17 @@ public class AiModelEntity : Entity<Guid>, IOrderNum, ISoftDelete
|
|||||||
public string? ExtraInfo { get; set; }
|
public string? ExtraInfo { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 模型类型
|
/// 模型类型(聊天/图片等)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ModelTypeEnum ModelType { get; set; }
|
public ModelTypeEnum ModelType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模型Api类型,现支持同一个模型id,多种接口格式
|
||||||
|
/// </summary>
|
||||||
|
public ModelApiTypeEnum ModelApiType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模型倍率
|
||||||
|
/// </summary>
|
||||||
|
public decimal Multiplier { get; set; } = 1;
|
||||||
}
|
}
|
||||||
@@ -5,27 +5,84 @@ using Volo.Abp.Domain.Entities.Auditing;
|
|||||||
namespace Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
namespace Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
||||||
|
|
||||||
[SugarTable("Ai_Token")]
|
[SugarTable("Ai_Token")]
|
||||||
|
[SugarIndex($"index_{{table}}_{nameof(UserId)}", nameof(UserId), OrderByType.Asc)]
|
||||||
public class TokenAggregateRoot : FullAuditedAggregateRoot<Guid>
|
public class TokenAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||||
{
|
{
|
||||||
public TokenAggregateRoot()
|
public TokenAggregateRoot()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public TokenAggregateRoot(Guid userId)
|
public TokenAggregateRoot(Guid userId, string name)
|
||||||
{
|
{
|
||||||
this.UserId = userId;
|
UserId = userId;
|
||||||
this.Token = GenerateToken();
|
Name = name;
|
||||||
|
Token = GenerateToken();
|
||||||
|
IsDisabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Token密钥
|
||||||
|
/// </summary>
|
||||||
public string Token { get; set; }
|
public string Token { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用户Id
|
||||||
|
/// </summary>
|
||||||
public Guid UserId { get; set; }
|
public Guid UserId { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 重置token
|
/// 名称
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void ResetToken()
|
[SugarColumn(Length = 100)]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 过期时间(空为永不过期)
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? ExpireTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 尊享包额度限制(空为不限制)
|
||||||
|
/// </summary>
|
||||||
|
public long? PremiumQuotaLimit { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否禁用
|
||||||
|
/// </summary>
|
||||||
|
public bool IsDisabled { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查Token是否可用
|
||||||
|
/// </summary>
|
||||||
|
public bool IsAvailable()
|
||||||
{
|
{
|
||||||
this.Token = GenerateToken();
|
if (IsDisabled)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ExpireTime.HasValue && ExpireTime.Value < DateTime.Now)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 禁用Token
|
||||||
|
/// </summary>
|
||||||
|
public void Disable()
|
||||||
|
{
|
||||||
|
IsDisabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启用Token
|
||||||
|
/// </summary>
|
||||||
|
public void Enable()
|
||||||
|
{
|
||||||
|
IsDisabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GenerateToken(int length = 36)
|
private string GenerateToken(int length = 36)
|
||||||
|
|||||||
@@ -76,23 +76,9 @@ public class PremiumPackageAggregateRoot : FullAuditedAggregateRoot<Guid>
|
|||||||
/// <returns>是否消耗成功</returns>
|
/// <returns>是否消耗成功</returns>
|
||||||
public bool ConsumeTokens(long tokenCount)
|
public bool ConsumeTokens(long tokenCount)
|
||||||
{
|
{
|
||||||
if (RemainingTokens < tokenCount)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!IsActive)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ExpireDateTime.HasValue && ExpireDateTime.Value < DateTime.Now)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
RemainingTokens -= tokenCount;
|
RemainingTokens -= tokenCount;
|
||||||
UsedTokens += tokenCount;
|
UsedTokens += tokenCount;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,14 +86,14 @@ public class PremiumPackageAggregateRoot : FullAuditedAggregateRoot<Guid>
|
|||||||
/// 检查是否可用
|
/// 检查是否可用
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>是否可用</returns>
|
/// <returns>是否可用</returns>
|
||||||
public bool IsAvailable()
|
public bool IsAvailable(bool isVerifyRemainingToken=true)
|
||||||
{
|
{
|
||||||
if (!IsActive)
|
if (!IsActive)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (RemainingTokens <= 0)
|
if (isVerifyRemainingToken&&RemainingTokens <= 0)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,16 +7,22 @@ namespace Yi.Framework.AiHub.Domain.Entities;
|
|||||||
/// 用量统计
|
/// 用量统计
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[SugarTable("Ai_UsageStatistics")]
|
[SugarTable("Ai_UsageStatistics")]
|
||||||
|
[SugarIndex($"index_{{table}}_{nameof(UserId)}_{nameof(ModelId)}_{nameof(TokenId)}",
|
||||||
|
nameof(UserId), OrderByType.Asc,
|
||||||
|
nameof(ModelId), OrderByType.Asc,
|
||||||
|
nameof(TokenId), OrderByType.Asc
|
||||||
|
)]
|
||||||
public class UsageStatisticsAggregateRoot : FullAuditedAggregateRoot<Guid>
|
public class UsageStatisticsAggregateRoot : FullAuditedAggregateRoot<Guid>
|
||||||
{
|
{
|
||||||
public UsageStatisticsAggregateRoot()
|
public UsageStatisticsAggregateRoot()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public UsageStatisticsAggregateRoot(Guid? userId, string modelId)
|
public UsageStatisticsAggregateRoot(Guid? userId, string modelId, Guid tokenId)
|
||||||
{
|
{
|
||||||
UserId = userId;
|
UserId = userId;
|
||||||
ModelId = modelId;
|
ModelId = modelId;
|
||||||
|
TokenId = tokenId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -29,6 +35,11 @@ public class UsageStatisticsAggregateRoot : FullAuditedAggregateRoot<Guid>
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string ModelId { get; set; }
|
public string ModelId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Token密钥Id(通过API调用时记录,Web调用为Guid.Empty)
|
||||||
|
/// </summary>
|
||||||
|
public Guid TokenId { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 对话次数
|
/// 对话次数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -12,11 +12,13 @@ using Volo.Abp.Domain.Services;
|
|||||||
using Yi.Framework.AiHub.Domain.AiGateWay;
|
using Yi.Framework.AiHub.Domain.AiGateWay;
|
||||||
using Yi.Framework.AiHub.Domain.AiGateWay.Exceptions;
|
using Yi.Framework.AiHub.Domain.AiGateWay.Exceptions;
|
||||||
using Yi.Framework.AiHub.Domain.Entities.Model;
|
using Yi.Framework.AiHub.Domain.Entities.Model;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Embeddings;
|
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Embeddings;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Images;
|
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi.Images;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||||
using Yi.Framework.Core.Extensions;
|
using Yi.Framework.Core.Extensions;
|
||||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
using JsonSerializer = System.Text.Json.JsonSerializer;
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
||||||
@@ -27,21 +29,24 @@ namespace Yi.Framework.AiHub.Domain.Managers;
|
|||||||
public class AiGateWayManager : DomainService
|
public class AiGateWayManager : DomainService
|
||||||
{
|
{
|
||||||
private readonly ISqlSugarRepository<AiAppAggregateRoot> _aiAppRepository;
|
private readonly ISqlSugarRepository<AiAppAggregateRoot> _aiAppRepository;
|
||||||
|
private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;
|
||||||
private readonly ILogger<AiGateWayManager> _logger;
|
private readonly ILogger<AiGateWayManager> _logger;
|
||||||
private readonly AiMessageManager _aiMessageManager;
|
private readonly AiMessageManager _aiMessageManager;
|
||||||
private readonly UsageStatisticsManager _usageStatisticsManager;
|
private readonly UsageStatisticsManager _usageStatisticsManager;
|
||||||
private readonly ISpecialCompatible _specialCompatible;
|
private readonly ISpecialCompatible _specialCompatible;
|
||||||
private PremiumPackageManager? _premiumPackageManager;
|
private PremiumPackageManager? _premiumPackageManager;
|
||||||
|
|
||||||
|
|
||||||
public AiGateWayManager(ISqlSugarRepository<AiAppAggregateRoot> aiAppRepository, ILogger<AiGateWayManager> logger,
|
public AiGateWayManager(ISqlSugarRepository<AiAppAggregateRoot> aiAppRepository, ILogger<AiGateWayManager> logger,
|
||||||
AiMessageManager aiMessageManager, UsageStatisticsManager usageStatisticsManager,
|
AiMessageManager aiMessageManager, UsageStatisticsManager usageStatisticsManager,
|
||||||
ISpecialCompatible specialCompatible)
|
ISpecialCompatible specialCompatible, ISqlSugarRepository<AiModelEntity> aiModelRepository)
|
||||||
{
|
{
|
||||||
_aiAppRepository = aiAppRepository;
|
_aiAppRepository = aiAppRepository;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_aiMessageManager = aiMessageManager;
|
_aiMessageManager = aiMessageManager;
|
||||||
_usageStatisticsManager = usageStatisticsManager;
|
_usageStatisticsManager = usageStatisticsManager;
|
||||||
_specialCompatible = specialCompatible;
|
_specialCompatible = specialCompatible;
|
||||||
|
_aiModelRepository = aiModelRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
private PremiumPackageManager PremiumPackageManager =>
|
private PremiumPackageManager PremiumPackageManager =>
|
||||||
@@ -50,17 +55,17 @@ public class AiGateWayManager : DomainService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取模型
|
/// 获取模型
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="modelApiType"></param>
|
||||||
/// <param name="modelId"></param>
|
/// <param name="modelId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private async Task<AiModelDescribe> GetModelAsync(string modelId)
|
private async Task<AiModelDescribe> GetModelAsync(ModelApiTypeEnum modelApiType, string modelId)
|
||||||
{
|
{
|
||||||
var allApp = await _aiAppRepository._DbQueryable.Includes(x => x.AiModels).ToListAsync();
|
var aiModelDescribe = await _aiModelRepository._DbQueryable
|
||||||
foreach (var app in allApp)
|
.LeftJoin<AiAppAggregateRoot>((model, app) => model.AiAppId == app.Id)
|
||||||
{
|
.Where((model, app) => model.ModelId == modelId)
|
||||||
var model = app.AiModels.FirstOrDefault(x => x.ModelId == modelId);
|
.Where((model, app) => model.ModelApiType == modelApiType)
|
||||||
if (model is not null)
|
.Select((model, app) =>
|
||||||
{
|
new AiModelDescribe
|
||||||
return new AiModelDescribe
|
|
||||||
{
|
{
|
||||||
AppId = app.Id,
|
AppId = app.Id,
|
||||||
AppName = app.Name,
|
AppName = app.Name,
|
||||||
@@ -72,12 +77,16 @@ public class AiGateWayManager : DomainService
|
|||||||
ModelName = model.Name,
|
ModelName = model.Name,
|
||||||
Description = model.Description,
|
Description = model.Description,
|
||||||
AppExtraUrl = app.ExtraUrl,
|
AppExtraUrl = app.ExtraUrl,
|
||||||
ModelExtraInfo = model.ExtraInfo
|
ModelExtraInfo = model.ExtraInfo,
|
||||||
};
|
Multiplier = model.Multiplier
|
||||||
}
|
})
|
||||||
|
.FirstAsync();
|
||||||
|
if (aiModelDescribe is null)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException($"【{modelId}】模型当前版本【{modelApiType}】格式不支持");
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new UserFriendlyException($"{modelId}模型当前版本不支持");
|
return aiModelDescribe;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -92,12 +101,13 @@ public class AiGateWayManager : DomainService
|
|||||||
[EnumeratorCancellation] CancellationToken cancellationToken)
|
[EnumeratorCancellation] CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_specialCompatible.Compatible(request);
|
_specialCompatible.Compatible(request);
|
||||||
var modelDescribe = await GetModelAsync(request.Model);
|
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, request.Model);
|
||||||
var chatService =
|
var chatService =
|
||||||
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
||||||
|
|
||||||
await foreach (var result in chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken))
|
await foreach (var result in chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken))
|
||||||
{
|
{
|
||||||
|
result.SupplementalMultiplier(modelDescribe.Multiplier);
|
||||||
yield return result;
|
yield return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,41 +120,55 @@ public class AiGateWayManager : DomainService
|
|||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="userId"></param>
|
/// <param name="userId"></param>
|
||||||
/// <param name="sessionId"></param>
|
/// <param name="sessionId"></param>
|
||||||
|
/// <param name="tokenId">Token Id(Web端传null或Guid.Empty)</param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task CompleteChatForStatisticsAsync(HttpContext httpContext,
|
public async Task CompleteChatForStatisticsAsync(HttpContext httpContext,
|
||||||
ThorChatCompletionsRequest request,
|
ThorChatCompletionsRequest request,
|
||||||
Guid? userId = null,
|
Guid? userId = null,
|
||||||
Guid? sessionId = null,
|
Guid? sessionId = null,
|
||||||
|
Guid? tokenId = null,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
_specialCompatible.Compatible(request);
|
_specialCompatible.Compatible(request);
|
||||||
var response = httpContext.Response;
|
var response = httpContext.Response;
|
||||||
// 设置响应头,声明是 json
|
// 设置响应头,声明是 json
|
||||||
//response.ContentType = "application/json; charset=UTF-8";
|
//response.ContentType = "application/json; charset=UTF-8";
|
||||||
var modelDescribe = await GetModelAsync(request.Model);
|
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, request.Model);
|
||||||
var chatService =
|
var chatService =
|
||||||
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
||||||
var data = await chatService.CompleteChatAsync(modelDescribe, request, cancellationToken);
|
var data = await chatService.CompleteChatAsync(modelDescribe, request, cancellationToken);
|
||||||
|
data.SupplementalMultiplier(modelDescribe.Multiplier);
|
||||||
if (userId is not null)
|
if (userId is not null)
|
||||||
{
|
{
|
||||||
await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId,
|
await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId,
|
||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = request.Messages?.LastOrDefault().Content ?? string.Empty,
|
Content = sessionId is null ? "不予存储" : request.Messages?.LastOrDefault().Content ?? string.Empty,
|
||||||
ModelId = request.Model,
|
ModelId = request.Model,
|
||||||
TokenUsage = data.Usage,
|
TokenUsage = data.Usage,
|
||||||
});
|
}, tokenId);
|
||||||
|
|
||||||
await _aiMessageManager.CreateSystemMessageAsync(userId.Value, sessionId,
|
await _aiMessageManager.CreateSystemMessageAsync(userId.Value, sessionId,
|
||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = data.Choices?.FirstOrDefault()?.Delta.Content,
|
Content =
|
||||||
|
sessionId is null ? "不予存储" : data.Choices?.FirstOrDefault()?.Delta.Content ?? string.Empty,
|
||||||
ModelId = request.Model,
|
ModelId = request.Model,
|
||||||
TokenUsage = data.Usage
|
TokenUsage = data.Usage
|
||||||
});
|
}, tokenId);
|
||||||
|
|
||||||
await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, data.Usage);
|
await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, data.Usage, tokenId);
|
||||||
|
|
||||||
|
// 扣减尊享token包用量
|
||||||
|
if (PremiumPackageConst.ModeIds.Contains(request.Model))
|
||||||
|
{
|
||||||
|
var totalTokens = data.Usage?.TotalTokens ?? 0;
|
||||||
|
if (totalTokens > 0)
|
||||||
|
{
|
||||||
|
await PremiumPackageManager.TryConsumeTokensAsync(userId.Value, totalTokens);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await response.WriteAsJsonAsync(data, cancellationToken);
|
await response.WriteAsJsonAsync(data, cancellationToken);
|
||||||
@@ -157,6 +181,7 @@ public class AiGateWayManager : DomainService
|
|||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="userId"></param>
|
/// <param name="userId"></param>
|
||||||
/// <param name="sessionId"></param>
|
/// <param name="sessionId"></param>
|
||||||
|
/// <param name="tokenId">Token Id(Web端传null或Guid.Empty)</param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task CompleteChatStreamForStatisticsAsync(
|
public async Task CompleteChatStreamForStatisticsAsync(
|
||||||
@@ -164,6 +189,7 @@ public class AiGateWayManager : DomainService
|
|||||||
ThorChatCompletionsRequest request,
|
ThorChatCompletionsRequest request,
|
||||||
Guid? userId = null,
|
Guid? userId = null,
|
||||||
Guid? sessionId = null,
|
Guid? sessionId = null,
|
||||||
|
Guid? tokenId = null,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var response = httpContext.Response;
|
var response = httpContext.Response;
|
||||||
@@ -216,7 +242,7 @@ public class AiGateWayManager : DomainService
|
|||||||
{
|
{
|
||||||
await foreach (var data in completeChatResponse)
|
await foreach (var data in completeChatResponse)
|
||||||
{
|
{
|
||||||
if (data.Usage is not null)
|
if (data.Usage is not null && (data.Usage.CompletionTokens > 0 || data.Usage.OutputTokens > 0))
|
||||||
{
|
{
|
||||||
tokenUsage = data.Usage;
|
tokenUsage = data.Usage;
|
||||||
}
|
}
|
||||||
@@ -263,20 +289,30 @@ public class AiGateWayManager : DomainService
|
|||||||
await _aiMessageManager.CreateUserMessageAsync(userId, sessionId,
|
await _aiMessageManager.CreateUserMessageAsync(userId, sessionId,
|
||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = request.Messages?.LastOrDefault()?.Content ?? string.Empty,
|
Content = sessionId is null ? "不予存储" : request.Messages?.LastOrDefault()?.Content ?? string.Empty,
|
||||||
ModelId = request.Model,
|
ModelId = request.Model,
|
||||||
TokenUsage = tokenUsage,
|
TokenUsage = tokenUsage,
|
||||||
});
|
}, tokenId);
|
||||||
|
|
||||||
await _aiMessageManager.CreateSystemMessageAsync(userId, sessionId,
|
await _aiMessageManager.CreateSystemMessageAsync(userId, sessionId,
|
||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = backupSystemContent.ToString(),
|
Content = sessionId is null ? "不予存储" : backupSystemContent.ToString(),
|
||||||
ModelId = request.Model,
|
ModelId = request.Model,
|
||||||
TokenUsage = tokenUsage
|
TokenUsage = tokenUsage
|
||||||
});
|
}, tokenId);
|
||||||
|
|
||||||
await _usageStatisticsManager.SetUsageAsync(userId, request.Model, tokenUsage);
|
await _usageStatisticsManager.SetUsageAsync(userId, request.Model, tokenUsage, tokenId);
|
||||||
|
|
||||||
|
// 扣减尊享token包用量
|
||||||
|
if (userId is not null && PremiumPackageConst.ModeIds.Contains(request.Model))
|
||||||
|
{
|
||||||
|
var totalTokens = tokenUsage.TotalTokens ?? 0;
|
||||||
|
if (totalTokens > 0)
|
||||||
|
{
|
||||||
|
await PremiumPackageManager.TryConsumeTokensAsync(userId.Value, totalTokens);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -287,17 +323,18 @@ public class AiGateWayManager : DomainService
|
|||||||
/// <param name="userId"></param>
|
/// <param name="userId"></param>
|
||||||
/// <param name="sessionId"></param>
|
/// <param name="sessionId"></param>
|
||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
|
/// <param name="tokenId">Token Id(Web端传null或Guid.Empty)</param>
|
||||||
/// <exception cref="BusinessException"></exception>
|
/// <exception cref="BusinessException"></exception>
|
||||||
/// <exception cref="Exception"></exception>
|
/// <exception cref="Exception"></exception>
|
||||||
public async Task CreateImageForStatisticsAsync(HttpContext context, Guid? userId, Guid? sessionId,
|
public async Task CreateImageForStatisticsAsync(HttpContext context, Guid? userId, Guid? sessionId,
|
||||||
ImageCreateRequest request)
|
ImageCreateRequest request, Guid? tokenId = null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var model = request.Model;
|
var model = request.Model;
|
||||||
if (string.IsNullOrEmpty(model)) model = "dall-e-2";
|
if (string.IsNullOrEmpty(model)) model = "dall-e-2";
|
||||||
|
|
||||||
var modelDescribe = await GetModelAsync(model);
|
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, model);
|
||||||
|
|
||||||
// 获取渠道指定的实现类型的服务
|
// 获取渠道指定的实现类型的服务
|
||||||
var imageService =
|
var imageService =
|
||||||
@@ -315,20 +352,30 @@ public class AiGateWayManager : DomainService
|
|||||||
await _aiMessageManager.CreateUserMessageAsync(userId, sessionId,
|
await _aiMessageManager.CreateUserMessageAsync(userId, sessionId,
|
||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = request.Prompt,
|
Content = sessionId is null ? "不予存储" : request.Prompt,
|
||||||
ModelId = model,
|
ModelId = model,
|
||||||
TokenUsage = response.Usage,
|
TokenUsage = response.Usage,
|
||||||
});
|
}, tokenId);
|
||||||
|
|
||||||
await _aiMessageManager.CreateSystemMessageAsync(userId, sessionId,
|
await _aiMessageManager.CreateSystemMessageAsync(userId, sessionId,
|
||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = response.Results?.FirstOrDefault()?.Url,
|
Content = sessionId is null ? "不予存储" : response.Results?.FirstOrDefault()?.Url,
|
||||||
ModelId = model,
|
ModelId = model,
|
||||||
TokenUsage = response.Usage
|
TokenUsage = response.Usage
|
||||||
});
|
}, tokenId);
|
||||||
|
|
||||||
await _usageStatisticsManager.SetUsageAsync(userId, model, response.Usage);
|
await _usageStatisticsManager.SetUsageAsync(userId, model, response.Usage, tokenId);
|
||||||
|
|
||||||
|
// 扣减尊享token包用量
|
||||||
|
if (userId is not null && PremiumPackageConst.ModeIds.Contains(request.Model))
|
||||||
|
{
|
||||||
|
var totalTokens = response.Usage.TotalTokens ?? 0;
|
||||||
|
if (totalTokens > 0)
|
||||||
|
{
|
||||||
|
await PremiumPackageManager.TryConsumeTokensAsync(userId.Value, totalTokens);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@@ -342,13 +389,14 @@ public class AiGateWayManager : DomainService
|
|||||||
/// 向量生成
|
/// 向量生成
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context"></param>
|
/// <param name="context"></param>
|
||||||
|
/// <param name="userId"></param>
|
||||||
/// <param name="sessionId"></param>
|
/// <param name="sessionId"></param>
|
||||||
/// <param name="input"></param>
|
/// <param name="input"></param>
|
||||||
/// <param name="userId"></param>
|
/// <param name="tokenId">Token Id(Web端传null或Guid.Empty)</param>
|
||||||
/// <exception cref="Exception"></exception>
|
/// <exception cref="Exception"></exception>
|
||||||
/// <exception cref="BusinessException"></exception>
|
/// <exception cref="BusinessException"></exception>
|
||||||
public async Task EmbeddingForStatisticsAsync(HttpContext context, Guid? userId, Guid? sessionId,
|
public async Task EmbeddingForStatisticsAsync(HttpContext context, Guid? userId, Guid? sessionId,
|
||||||
ThorEmbeddingInput input)
|
ThorEmbeddingInput input, Guid? tokenId = null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -357,7 +405,7 @@ public class AiGateWayManager : DomainService
|
|||||||
using var embedding =
|
using var embedding =
|
||||||
Activity.Current?.Source.StartActivity("向量模型调用");
|
Activity.Current?.Source.StartActivity("向量模型调用");
|
||||||
|
|
||||||
var modelDescribe = await GetModelAsync(input.Model);
|
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, input.Model);
|
||||||
|
|
||||||
// 获取渠道指定的实现类型的服务
|
// 获取渠道指定的实现类型的服务
|
||||||
var embeddingService =
|
var embeddingService =
|
||||||
@@ -432,7 +480,7 @@ public class AiGateWayManager : DomainService
|
|||||||
// TokenUsage = usage
|
// TokenUsage = usage
|
||||||
// });
|
// });
|
||||||
|
|
||||||
await _usageStatisticsManager.SetUsageAsync(userId, input.Model, usage);
|
await _usageStatisticsManager.SetUsageAsync(userId, input.Model, usage, tokenId);
|
||||||
}
|
}
|
||||||
catch (ThorRateLimitException)
|
catch (ThorRateLimitException)
|
||||||
{
|
{
|
||||||
@@ -461,12 +509,13 @@ public class AiGateWayManager : DomainService
|
|||||||
[EnumeratorCancellation] CancellationToken cancellationToken)
|
[EnumeratorCancellation] CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_specialCompatible.AnthropicCompatible(request);
|
_specialCompatible.AnthropicCompatible(request);
|
||||||
var modelDescribe = await GetModelAsync(request.Model);
|
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Claude, request.Model);
|
||||||
var chatService =
|
var chatService =
|
||||||
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
||||||
|
|
||||||
await foreach (var result in chatService.StreamChatCompletionsAsync(modelDescribe, request, cancellationToken))
|
await foreach (var result in chatService.StreamChatCompletionsAsync(modelDescribe, request, cancellationToken))
|
||||||
{
|
{
|
||||||
|
result.Item2.SupplementalMultiplier(modelDescribe.Multiplier);
|
||||||
yield return result;
|
yield return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -479,51 +528,52 @@ public class AiGateWayManager : DomainService
|
|||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="userId"></param>
|
/// <param name="userId"></param>
|
||||||
/// <param name="sessionId"></param>
|
/// <param name="sessionId"></param>
|
||||||
|
/// <param name="tokenId">Token Id(Web端传null或Guid.Empty)</param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task AnthropicCompleteChatForStatisticsAsync(HttpContext httpContext,
|
public async Task AnthropicCompleteChatForStatisticsAsync(HttpContext httpContext,
|
||||||
AnthropicInput request,
|
AnthropicInput request,
|
||||||
Guid? userId = null,
|
Guid? userId = null,
|
||||||
Guid? sessionId = null,
|
Guid? sessionId = null,
|
||||||
|
Guid? tokenId = null,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
_specialCompatible.AnthropicCompatible(request);
|
_specialCompatible.AnthropicCompatible(request);
|
||||||
var response = httpContext.Response;
|
var response = httpContext.Response;
|
||||||
// 设置响应头,声明是 json
|
// 设置响应头,声明是 json
|
||||||
//response.ContentType = "application/json; charset=UTF-8";
|
//response.ContentType = "application/json; charset=UTF-8";
|
||||||
var modelDescribe = await GetModelAsync(request.Model);
|
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Claude, request.Model);
|
||||||
var chatService =
|
var chatService =
|
||||||
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
||||||
var data = await chatService.ChatCompletionsAsync(modelDescribe, request, cancellationToken);
|
var data = await chatService.ChatCompletionsAsync(modelDescribe, request, cancellationToken);
|
||||||
|
|
||||||
|
data.SupplementalMultiplier(modelDescribe.Multiplier);
|
||||||
|
|
||||||
if (userId is not null)
|
if (userId is not null)
|
||||||
{
|
{
|
||||||
await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId,
|
await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId,
|
||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = request.Messages?.FirstOrDefault()?.Content ?? string.Empty,
|
Content = sessionId is null ? "不予存储" : request.Messages?.FirstOrDefault()?.Content ?? string.Empty,
|
||||||
ModelId = request.Model,
|
ModelId = request.Model,
|
||||||
TokenUsage = data.TokenUsage,
|
TokenUsage = data.TokenUsage,
|
||||||
});
|
}, tokenId);
|
||||||
|
|
||||||
await _aiMessageManager.CreateSystemMessageAsync(userId.Value, sessionId,
|
await _aiMessageManager.CreateSystemMessageAsync(userId.Value, sessionId,
|
||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = data.content?.FirstOrDefault()?.text,
|
Content = sessionId is null ? "不予存储" : data.content?.FirstOrDefault()?.text,
|
||||||
ModelId = request.Model,
|
ModelId = request.Model,
|
||||||
TokenUsage = data.TokenUsage
|
TokenUsage = data.TokenUsage
|
||||||
});
|
}, tokenId);
|
||||||
|
|
||||||
await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, data.TokenUsage);
|
await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, data.TokenUsage, tokenId);
|
||||||
|
|
||||||
// 扣减尊享token包用量
|
// 扣减尊享token包用量
|
||||||
var totalTokens = (data.TokenUsage?.InputTokens ?? 0) + (data.TokenUsage?.OutputTokens ?? 0);
|
var totalTokens = data.TokenUsage.TotalTokens ?? 0;
|
||||||
if (totalTokens > 0)
|
if (totalTokens > 0)
|
||||||
{
|
{
|
||||||
var consumeSuccess = await PremiumPackageManager.ConsumeTokensAsync(userId.Value, totalTokens);
|
await PremiumPackageManager.TryConsumeTokensAsync(userId.Value, totalTokens);
|
||||||
if (!consumeSuccess)
|
|
||||||
{
|
|
||||||
_logger.LogWarning($"用户 {userId.Value} 尊享token包扣减失败,消耗token数: {totalTokens}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -537,6 +587,7 @@ public class AiGateWayManager : DomainService
|
|||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="userId"></param>
|
/// <param name="userId"></param>
|
||||||
/// <param name="sessionId"></param>
|
/// <param name="sessionId"></param>
|
||||||
|
/// <param name="tokenId">Token Id(Web端传null或Guid.Empty)</param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task AnthropicCompleteChatStreamForStatisticsAsync(
|
public async Task AnthropicCompleteChatStreamForStatisticsAsync(
|
||||||
@@ -544,6 +595,7 @@ public class AiGateWayManager : DomainService
|
|||||||
AnthropicInput request,
|
AnthropicInput request,
|
||||||
Guid? userId = null,
|
Guid? userId = null,
|
||||||
Guid? sessionId = null,
|
Guid? sessionId = null,
|
||||||
|
Guid? tokenId = null,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var response = httpContext.Response;
|
var response = httpContext.Response;
|
||||||
@@ -566,6 +618,7 @@ public class AiGateWayManager : DomainService
|
|||||||
{
|
{
|
||||||
tokenUsage = responseResult.Item2?.TokenUsage;
|
tokenUsage = responseResult.Item2?.TokenUsage;
|
||||||
}
|
}
|
||||||
|
|
||||||
backupSystemContent.Append(responseResult.Item2?.Delta?.Text);
|
backupSystemContent.Append(responseResult.Item2?.Delta?.Text);
|
||||||
await WriteAsEventStreamDataAsync(httpContext, responseResult.Item1, responseResult.Item2,
|
await WriteAsEventStreamDataAsync(httpContext, responseResult.Item1, responseResult.Item2,
|
||||||
cancellationToken);
|
cancellationToken);
|
||||||
@@ -576,60 +629,33 @@ public class AiGateWayManager : DomainService
|
|||||||
_logger.LogError(e, $"Ai对话异常");
|
_logger.LogError(e, $"Ai对话异常");
|
||||||
var errorContent = $"对话Ai异常,异常信息:\n当前Ai模型:{request.Model}\n异常信息:{e.Message}\n异常堆栈:{e}";
|
var errorContent = $"对话Ai异常,异常信息:\n当前Ai模型:{request.Model}\n异常信息:{e.Message}\n异常堆栈:{e}";
|
||||||
throw new UserFriendlyException(errorContent);
|
throw new UserFriendlyException(errorContent);
|
||||||
// var model = new AnthropicStreamDto
|
|
||||||
// {
|
|
||||||
// Message = new AnthropicChatCompletionDto
|
|
||||||
// {
|
|
||||||
// content =
|
|
||||||
// [
|
|
||||||
// new AnthropicChatCompletionDtoContent
|
|
||||||
// {
|
|
||||||
// text = errorContent,
|
|
||||||
// }
|
|
||||||
// ],
|
|
||||||
// },
|
|
||||||
// Error = new AnthropicStreamErrorDto
|
|
||||||
// {
|
|
||||||
// Type = null,
|
|
||||||
// Message = errorContent
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
// var message = JsonConvert.SerializeObject(model, new JsonSerializerSettings
|
|
||||||
// {
|
|
||||||
// ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
||||||
// });
|
|
||||||
// await response.WriteAsJsonAsync(message, ThorJsonSerializer.DefaultOptions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await _aiMessageManager.CreateUserMessageAsync(userId, sessionId,
|
await _aiMessageManager.CreateUserMessageAsync(userId, sessionId,
|
||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = request.Messages?.LastOrDefault()?.Content ?? string.Empty,
|
Content = sessionId is null ? "不予存储" : request.Messages?.LastOrDefault()?.Content ?? string.Empty,
|
||||||
ModelId = request.Model,
|
ModelId = request.Model,
|
||||||
TokenUsage = tokenUsage,
|
TokenUsage = tokenUsage,
|
||||||
});
|
}, tokenId);
|
||||||
|
|
||||||
await _aiMessageManager.CreateSystemMessageAsync(userId, sessionId,
|
await _aiMessageManager.CreateSystemMessageAsync(userId, sessionId,
|
||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = backupSystemContent.ToString(),
|
Content = sessionId is null ? "不予存储" : backupSystemContent.ToString(),
|
||||||
ModelId = request.Model,
|
ModelId = request.Model,
|
||||||
TokenUsage = tokenUsage
|
TokenUsage = tokenUsage
|
||||||
});
|
}, tokenId);
|
||||||
|
|
||||||
await _usageStatisticsManager.SetUsageAsync(userId, request.Model, tokenUsage);
|
await _usageStatisticsManager.SetUsageAsync(userId, request.Model, tokenUsage, tokenId);
|
||||||
|
|
||||||
// 扣减尊享token包用量
|
// 扣减尊享token包用量
|
||||||
if (userId.HasValue && tokenUsage is not null)
|
if (userId.HasValue && tokenUsage is not null)
|
||||||
{
|
{
|
||||||
var totalTokens = tokenUsage.TotalTokens ?? 0;
|
var totalTokens = tokenUsage.TotalTokens ?? 0;
|
||||||
if (totalTokens > 0)
|
if (tokenUsage.TotalTokens > 0)
|
||||||
{
|
{
|
||||||
var consumeSuccess = await PremiumPackageManager.ConsumeTokensAsync(userId.Value, totalTokens);
|
await PremiumPackageManager.TryConsumeTokensAsync(userId.Value, totalTokens);
|
||||||
if (!consumeSuccess)
|
|
||||||
{
|
|
||||||
_logger.LogWarning($"用户 {userId.Value} 尊享token包扣减失败,消耗token数: {totalTokens}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,28 +19,30 @@ public class AiMessageManager : DomainService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建系统消息
|
/// 创建系统消息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sessionId"></param>
|
/// <param name="userId">用户Id</param>
|
||||||
/// <param name="userId"></param>
|
/// <param name="sessionId">会话Id</param>
|
||||||
/// <param name="input"></param>
|
/// <param name="input">消息输入</param>
|
||||||
|
/// <param name="tokenId">Token Id(Web端传Guid.Empty)</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task CreateSystemMessageAsync(Guid? userId, Guid? sessionId, MessageInputDto input)
|
public async Task CreateSystemMessageAsync(Guid? userId, Guid? sessionId, MessageInputDto input, Guid? tokenId = null)
|
||||||
{
|
{
|
||||||
input.Role = "system";
|
input.Role = "system";
|
||||||
var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId,input.TokenUsage);
|
var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId, input.TokenUsage, tokenId);
|
||||||
await _repository.InsertAsync(message);
|
await _repository.InsertAsync(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建系统消息
|
/// 创建用户消息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sessionId"></param>
|
/// <param name="userId">用户Id</param>
|
||||||
/// <param name="userId"></param>
|
/// <param name="sessionId">会话Id</param>
|
||||||
/// <param name="input"></param>
|
/// <param name="input">消息输入</param>
|
||||||
|
/// <param name="tokenId">Token Id(Web端传Guid.Empty)</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task CreateUserMessageAsync(Guid? userId, Guid? sessionId, MessageInputDto input)
|
public async Task CreateUserMessageAsync(Guid? userId, Guid? sessionId, MessageInputDto input, Guid? tokenId = null)
|
||||||
{
|
{
|
||||||
input.Role = "user";
|
input.Role = "user";
|
||||||
var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId,input.TokenUsage);
|
var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId, input.TokenUsage, tokenId);
|
||||||
await _repository.InsertAsync(message);
|
await _repository.InsertAsync(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
using Volo.Abp.Domain.Services;
|
using Volo.Abp.Domain.Services;
|
||||||
using Yi.Framework.AiHub.Domain.Entities;
|
using Yi.Framework.AiHub.Domain.Entities;
|
||||||
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
namespace Yi.Framework.AiHub.Domain.Managers;
|
namespace Yi.Framework.AiHub.Domain.Managers;
|
||||||
@@ -28,7 +29,7 @@ public class AiRechargeManager : DomainService
|
|||||||
var currentTime = DateTime.Now;
|
var currentTime = DateTime.Now;
|
||||||
|
|
||||||
// 查找所有充值记录,按用户分组
|
// 查找所有充值记录,按用户分组
|
||||||
var allRecharges = await _rechargeRepository._DbQueryable
|
var allRecharges = await _rechargeRepository._DbQueryable.Where(x => x.RechargeType == RechargeTypeEnum.Vip)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
if (!allRecharges.Any())
|
if (!allRecharges.Any())
|
||||||
|
|||||||
@@ -0,0 +1,356 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Volo.Abp.Domain.Services;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Domain.Managers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌管理器 - 负责翻牌核心业务逻辑
|
||||||
|
/// </summary>
|
||||||
|
public class CardFlipManager : DomainService
|
||||||
|
{
|
||||||
|
private readonly ISqlSugarRepository<CardFlipTaskAggregateRoot> _cardFlipTaskRepository;
|
||||||
|
private readonly ISqlSugarRepository<InvitationRecordAggregateRoot> _invitationRecordRepository;
|
||||||
|
private readonly InviteCodeManager _inviteCodeManager;
|
||||||
|
private readonly ILogger<CardFlipManager> _logger;
|
||||||
|
|
||||||
|
// 翻牌规则配置
|
||||||
|
public const int MAX_FREE_FLIPS = 7; // 免费翻牌次数
|
||||||
|
public const int MAX_INVITE_FLIPS = 3; // 邀请解锁翻牌次数
|
||||||
|
public const int TOTAL_MAX_FLIPS = 10; // 总最大翻牌次数
|
||||||
|
|
||||||
|
private const int EIGHTH_FLIP = 8; // 第8次翻牌
|
||||||
|
private const int NINTH_FLIP = 9; // 第9次翻牌
|
||||||
|
private const int TENTH_FLIP = 10; // 第10次翻牌
|
||||||
|
|
||||||
|
// 前7次免费翻牌奖励配置
|
||||||
|
private const long FREE_MIN_REWARD = 10000; // 前7次最小奖励 1w
|
||||||
|
private const long FREE_MAX_REWARD = 30000; // 前7次最大奖励 3w
|
||||||
|
private const double FREE_WIN_RATE = 0.5; // 前7次中奖概率 50%
|
||||||
|
|
||||||
|
private const long EIGHTH_MIN_REWARD = 1000000; // 第8次最小奖励 100w
|
||||||
|
private const long EIGHTH_MAX_REWARD = 3000000; // 第8次最大奖励 300w
|
||||||
|
private const long NINTH_MIN_REWARD = 1000000; // 第9次最小奖励 100w
|
||||||
|
private const long NINTH_MAX_REWARD = 5000000; // 第9次最大奖励 500w
|
||||||
|
private const long TENTH_MIN_REWARD = 1000000; // 第10次最小奖励 100w
|
||||||
|
private const long TENTH_MAX_REWARD = 10000000; // 第10次最大奖励 1000w
|
||||||
|
|
||||||
|
public CardFlipManager(
|
||||||
|
ISqlSugarRepository<CardFlipTaskAggregateRoot> cardFlipTaskRepository,
|
||||||
|
ISqlSugarRepository<InvitationRecordAggregateRoot> invitationRecordRepository,
|
||||||
|
InviteCodeManager inviteCodeManager,
|
||||||
|
ILogger<CardFlipManager> logger)
|
||||||
|
{
|
||||||
|
_cardFlipTaskRepository = cardFlipTaskRepository;
|
||||||
|
_invitationRecordRepository = invitationRecordRepository;
|
||||||
|
_inviteCodeManager = inviteCodeManager;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或创建本周任务
|
||||||
|
/// </summary>
|
||||||
|
public async Task<CardFlipTaskAggregateRoot?> GetOrCreateWeeklyTaskAsync(
|
||||||
|
Guid userId,
|
||||||
|
DateTime weekStart,
|
||||||
|
bool createIfNotExists)
|
||||||
|
{
|
||||||
|
var task = await _cardFlipTaskRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId && x.WeekStartDate == weekStart)
|
||||||
|
.FirstAsync();
|
||||||
|
|
||||||
|
if (task == null && createIfNotExists)
|
||||||
|
{
|
||||||
|
task = new CardFlipTaskAggregateRoot(userId, weekStart);
|
||||||
|
await _cardFlipTaskRepository.InsertAsync(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取已翻牌的顺序列表
|
||||||
|
/// </summary>
|
||||||
|
public List<int> GetFlippedOrder(CardFlipTaskAggregateRoot task)
|
||||||
|
{
|
||||||
|
return task.FlippedOrder ?? new List<int>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行翻牌逻辑
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId">用户ID</param>
|
||||||
|
/// <param name="flipNumber">翻牌序号</param>
|
||||||
|
/// <param name="weekStart">本周开始日期</param>
|
||||||
|
/// <returns>翻牌结果</returns>
|
||||||
|
public async Task<FlipResult> ExecuteFlipAsync(Guid userId, int flipNumber, DateTime weekStart)
|
||||||
|
{
|
||||||
|
// 验证翻牌序号
|
||||||
|
if (flipNumber < 1 || flipNumber > TOTAL_MAX_FLIPS)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException($"翻牌序号必须在1-{TOTAL_MAX_FLIPS}之间");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取或创建本周任务
|
||||||
|
var task = await GetOrCreateWeeklyTaskAsync(userId, weekStart, createIfNotExists: true);
|
||||||
|
|
||||||
|
// 验证翻牌次数
|
||||||
|
if (task.TotalFlips >= TOTAL_MAX_FLIPS)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("本周翻牌次数已用完,请下周再来!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证该牌是否已经翻过
|
||||||
|
var flippedOrder = GetFlippedOrder(task);
|
||||||
|
if (flippedOrder.Contains(flipNumber))
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException($"第 {flipNumber} 号牌已经翻过了!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断翻牌类型
|
||||||
|
var flipType = DetermineFlipType(task);
|
||||||
|
|
||||||
|
// 验证是否有足够的次数
|
||||||
|
if (!CanUseFlipType(task, flipType))
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException(GetFlipTypeErrorMessage(flipType));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果是邀请类型翻牌,必须验证用户本周的邀请记录数量足够(包括填写别人的邀请码和别人填写我的邀请码)
|
||||||
|
if (flipType == FlipType.Invite)
|
||||||
|
{
|
||||||
|
// 查询本周作为邀请人或被邀请人的记录数量(双方都会增加翻牌次数)
|
||||||
|
var weeklyInviteRecordCount = await _invitationRecordRepository._DbQueryable
|
||||||
|
.Where(x => x.InviterId == userId || x.InvitedUserId == userId)
|
||||||
|
.Where(x => x.InvitationTime >= weekStart)
|
||||||
|
.CountAsync();
|
||||||
|
|
||||||
|
// 本周邀请记录数量必须 >= 即将使用的邀请翻牌次数
|
||||||
|
// 例如: 要翻第8次(InviteFlipsUsed=0->1), 需要至少有1条邀请记录(我邀请别人或别人邀请我)
|
||||||
|
// 要翻第9次(InviteFlipsUsed=1->2), 需要至少有2条邀请记录
|
||||||
|
// 要翻第10次(InviteFlipsUsed=2->3), 需要至少有3条邀请记录
|
||||||
|
var requiredInviteRecordCount = task.InviteFlipsUsed + 1;
|
||||||
|
if (weeklyInviteRecordCount < requiredInviteRecordCount)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException($"需本周累积{requiredInviteRecordCount}次邀请记录(填写别人的邀请码或别人填写你的邀请码)才能解锁第{task.TotalFlips + 1}次翻牌");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算翻牌结果(基于当前是第几次翻牌,而不是卡片序号)
|
||||||
|
var flipCount = task.TotalFlips + 1; // 当前这次翻牌是第几次
|
||||||
|
var result = CalculateFlipResult(flipCount);
|
||||||
|
|
||||||
|
// 将卡片序号信息也返回
|
||||||
|
result.FlipNumber = flipNumber;
|
||||||
|
|
||||||
|
// 更新翻牌次数(必须在记录奖励之前,因为需要先确定是第几次)
|
||||||
|
task.IncrementFlip(flipType);
|
||||||
|
|
||||||
|
// 记录翻牌顺序
|
||||||
|
if (task.FlippedOrder == null)
|
||||||
|
{
|
||||||
|
task.FlippedOrder = new List<int>();
|
||||||
|
}
|
||||||
|
task.FlippedOrder.Add(flipNumber);
|
||||||
|
|
||||||
|
// 如果中奖,记录奖励金额(用于后续查询显示)
|
||||||
|
if (result.IsWin)
|
||||||
|
{
|
||||||
|
task.SetWinReward(flipCount, result.RewardAmount);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _cardFlipTaskRepository.UpdateAsync(task);
|
||||||
|
|
||||||
|
_logger.LogInformation($"用户 {userId} 完成第 {flipNumber} 次翻牌,中奖:{result.IsWin}");
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断是否可以翻牌
|
||||||
|
/// </summary>
|
||||||
|
public bool CanFlipCard(CardFlipTaskAggregateRoot? task)
|
||||||
|
{
|
||||||
|
if (task == null) return true; // 没有任务记录,可以开始翻牌
|
||||||
|
|
||||||
|
return task.TotalFlips < TOTAL_MAX_FLIPS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断翻牌类型
|
||||||
|
/// </summary>
|
||||||
|
public FlipType DetermineFlipType(CardFlipTaskAggregateRoot task)
|
||||||
|
{
|
||||||
|
if (task.FreeFlipsUsed < MAX_FREE_FLIPS)
|
||||||
|
{
|
||||||
|
return FlipType.Free;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return FlipType.Invite;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断是否可以使用该翻牌类型
|
||||||
|
/// </summary>
|
||||||
|
public bool CanUseFlipType(CardFlipTaskAggregateRoot task, FlipType flipType)
|
||||||
|
{
|
||||||
|
return flipType switch
|
||||||
|
{
|
||||||
|
FlipType.Free => task.FreeFlipsUsed < MAX_FREE_FLIPS,
|
||||||
|
FlipType.Invite => task.InviteFlipsUsed < MAX_INVITE_FLIPS,
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 计算翻牌结果
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="flipCount">第几次翻牌(1-10)</param>
|
||||||
|
private FlipResult CalculateFlipResult(int flipCount)
|
||||||
|
{
|
||||||
|
var result = new FlipResult
|
||||||
|
{
|
||||||
|
FlipNumber = 0, // 稍后会被设置为实际的卡片序号
|
||||||
|
IsWin = false
|
||||||
|
};
|
||||||
|
|
||||||
|
var random = new Random();
|
||||||
|
|
||||||
|
// 前7次: 50%概率中奖,奖励1w-3w
|
||||||
|
if (flipCount <= 7)
|
||||||
|
{
|
||||||
|
// 50%概率中奖
|
||||||
|
if (random.NextDouble() < FREE_WIN_RATE)
|
||||||
|
{
|
||||||
|
var rewardAmount = GenerateRandomReward(FREE_MIN_REWARD, FREE_MAX_REWARD);
|
||||||
|
result.IsWin = true;
|
||||||
|
result.RewardAmount = rewardAmount;
|
||||||
|
result.RewardDesc = $"恭喜获得尊享包 {rewardAmount / 10000m:0.##}w tokens!";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result.IsWin = false;
|
||||||
|
result.RewardDesc = "很遗憾,未中奖";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 第8次中奖 (邀请码解锁)
|
||||||
|
else if (flipCount == EIGHTH_FLIP)
|
||||||
|
{
|
||||||
|
var rewardAmount = GenerateRandomReward(EIGHTH_MIN_REWARD, EIGHTH_MAX_REWARD);
|
||||||
|
result.IsWin = true;
|
||||||
|
result.RewardAmount = rewardAmount;
|
||||||
|
result.RewardDesc = $"恭喜获得尊享包 {rewardAmount / 10000}w tokens!";
|
||||||
|
}
|
||||||
|
// 第9次中奖 (邀请码解锁)
|
||||||
|
else if (flipCount == NINTH_FLIP)
|
||||||
|
{
|
||||||
|
var rewardAmount = GenerateRandomReward(NINTH_MIN_REWARD, NINTH_MAX_REWARD);
|
||||||
|
result.IsWin = true;
|
||||||
|
result.RewardAmount = rewardAmount;
|
||||||
|
result.RewardDesc = $"恭喜获得尊享包 {rewardAmount / 10000}w tokens!";
|
||||||
|
}
|
||||||
|
// 第10次中奖 (邀请码解锁)
|
||||||
|
else if (flipCount == TENTH_FLIP)
|
||||||
|
{
|
||||||
|
var rewardAmount = GenerateRandomReward(TENTH_MIN_REWARD, TENTH_MAX_REWARD);
|
||||||
|
result.IsWin = true;
|
||||||
|
result.RewardAmount = rewardAmount;
|
||||||
|
result.RewardDesc = $"恭喜获得尊享包 {rewardAmount / 10000}w tokens!";
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取翻牌类型错误提示
|
||||||
|
/// </summary>
|
||||||
|
private string GetFlipTypeErrorMessage(FlipType flipType)
|
||||||
|
{
|
||||||
|
return flipType switch
|
||||||
|
{
|
||||||
|
FlipType.Free => "免费翻牌次数已用完",
|
||||||
|
FlipType.Invite => "需要使用邀请码解锁更多次数",
|
||||||
|
_ => "无法翻牌"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成随机奖励金额
|
||||||
|
/// </summary>
|
||||||
|
private long GenerateRandomReward(long min, long max)
|
||||||
|
{
|
||||||
|
var random = new Random();
|
||||||
|
|
||||||
|
// 根据最小值判断单位
|
||||||
|
// 如果min小于100000,则使用1w(10000)作为单位;否则使用100w(1000000)作为单位
|
||||||
|
long unit = min < 100000 ? 10000 : 1000000;
|
||||||
|
|
||||||
|
// 将min和max转换为单位的倍数
|
||||||
|
long minUnits = min / unit;
|
||||||
|
long maxUnits = max / unit;
|
||||||
|
|
||||||
|
// 在倍数范围内随机
|
||||||
|
long randomUnits = random.Next((int)minUnits, (int)maxUnits + 1);
|
||||||
|
|
||||||
|
// 返回单位的倍数
|
||||||
|
return randomUnits * unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取本周开始日期(周一)
|
||||||
|
/// </summary>
|
||||||
|
public static DateTime GetWeekStartDate(DateTime date)
|
||||||
|
{
|
||||||
|
var dayOfWeek = (int)date.DayOfWeek;
|
||||||
|
// 将周日(0)转换为7
|
||||||
|
if (dayOfWeek == 0) dayOfWeek = 7;
|
||||||
|
|
||||||
|
// 计算本周一的日期
|
||||||
|
var monday = date.Date.AddDays(-(dayOfWeek - 1));
|
||||||
|
return monday;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取翻牌类型描述
|
||||||
|
/// </summary>
|
||||||
|
public static string GetFlipTypeDesc(int flipNumber)
|
||||||
|
{
|
||||||
|
if (flipNumber <= MAX_FREE_FLIPS)
|
||||||
|
{
|
||||||
|
return "免费";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return "邀请解锁";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌结果
|
||||||
|
/// </summary>
|
||||||
|
public class FlipResult
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 翻牌序号
|
||||||
|
/// </summary>
|
||||||
|
public int FlipNumber { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否中奖
|
||||||
|
/// </summary>
|
||||||
|
public bool IsWin { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 奖励金额
|
||||||
|
/// </summary>
|
||||||
|
public long RewardAmount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 奖励描述
|
||||||
|
/// </summary>
|
||||||
|
public string RewardDesc { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ public class FuwuhaoManager : DomainService
|
|||||||
private IDistributedCache<AccessTokenResponse> _accessTokenCache;
|
private IDistributedCache<AccessTokenResponse> _accessTokenCache;
|
||||||
private ISqlSugarRepository<AiUserExtraInfoEntity> _userRepository;
|
private ISqlSugarRepository<AiUserExtraInfoEntity> _userRepository;
|
||||||
private readonly ILogger<FuwuhaoManager> _logger;
|
private readonly ILogger<FuwuhaoManager> _logger;
|
||||||
|
|
||||||
public FuwuhaoManager(IOptions<FuwuhaoOptions> options, IHttpClientFactory httpClientFactory,
|
public FuwuhaoManager(IOptions<FuwuhaoOptions> options, IHttpClientFactory httpClientFactory,
|
||||||
ISqlSugarRepository<AiUserExtraInfoEntity> userRepository,
|
ISqlSugarRepository<AiUserExtraInfoEntity> userRepository,
|
||||||
IDistributedCache<AccessTokenResponse> accessTokenCache, ILogger<FuwuhaoManager> logger)
|
IDistributedCache<AccessTokenResponse> accessTokenCache, ILogger<FuwuhaoManager> logger)
|
||||||
@@ -49,6 +50,11 @@ public class FuwuhaoManager : DomainService
|
|||||||
{
|
{
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
|
||||||
});
|
});
|
||||||
|
if (result is null || string.IsNullOrEmpty(result.AccessToken))
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("微信服务号AccessToken为空");
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}, () => new DistributedCacheEntryOptions()
|
}, () => new DistributedCacheEntryOptions()
|
||||||
{
|
{
|
||||||
@@ -175,7 +181,8 @@ public class FuwuhaoManager : DomainService
|
|||||||
/// <param name="title">图文消息标题</param>
|
/// <param name="title">图文消息标题</param>
|
||||||
/// <param name="description">图文消息描述</param>
|
/// <param name="description">图文消息描述</param>
|
||||||
/// <returns>XML格式的图文消息体</returns>
|
/// <returns>XML格式的图文消息体</returns>
|
||||||
public string BuildRegisterMessage(string toUser, string title="意社区点击一键注册账号", string description="来自意社区SSO统一注册安全中心")
|
public string BuildRegisterMessage(string toUser, string title = "意社区点击一键注册账号",
|
||||||
|
string description = "来自意社区SSO统一注册安全中心")
|
||||||
{
|
{
|
||||||
var createTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
var createTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||||
var fromUser = _options.FromUser;
|
var fromUser = _options.FromUser;
|
||||||
@@ -207,7 +214,8 @@ public class FuwuhaoManager : DomainService
|
|||||||
/// <param name="openId"></param>
|
/// <param name="openId"></param>
|
||||||
/// <param name="bindUserId"></param>
|
/// <param name="bindUserId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<(SceneResultEnum SceneResult,Guid? UserId)> CallBackHandlerAsync(SceneTypeEnum sceneType, string openId, Guid? bindUserId)
|
public async Task<(SceneResultEnum SceneResult, Guid? UserId)> CallBackHandlerAsync(SceneTypeEnum sceneType,
|
||||||
|
string openId, Guid? bindUserId)
|
||||||
{
|
{
|
||||||
var aiUserInfo = await _userRepository._DbQueryable.Where(x => x.FuwuhaoOpenId == openId).FirstAsync();
|
var aiUserInfo = await _userRepository._DbQueryable.Where(x => x.FuwuhaoOpenId == openId).FirstAsync();
|
||||||
switch (sceneType)
|
switch (sceneType)
|
||||||
|
|||||||
@@ -0,0 +1,194 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using SqlSugar;
|
||||||
|
using Volo.Abp.Domain.Services;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Domain.Managers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请码管理器 - 负责邀请码核心业务逻辑
|
||||||
|
/// </summary>
|
||||||
|
public class InviteCodeManager : DomainService
|
||||||
|
{
|
||||||
|
private readonly ISqlSugarRepository<InviteCodeAggregateRoot> _inviteCodeRepository;
|
||||||
|
private readonly ISqlSugarRepository<InvitationRecordAggregateRoot> _invitationRecordRepository;
|
||||||
|
private readonly ILogger<InviteCodeManager> _logger;
|
||||||
|
|
||||||
|
public InviteCodeManager(
|
||||||
|
ISqlSugarRepository<InviteCodeAggregateRoot> inviteCodeRepository,
|
||||||
|
ISqlSugarRepository<InvitationRecordAggregateRoot> invitationRecordRepository,
|
||||||
|
ILogger<InviteCodeManager> logger)
|
||||||
|
{
|
||||||
|
_inviteCodeRepository = inviteCodeRepository;
|
||||||
|
_invitationRecordRepository = invitationRecordRepository;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成用户的邀请码
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId">用户ID</param>
|
||||||
|
/// <returns>邀请码</returns>
|
||||||
|
public async Task<string> GenerateInviteCodeForUserAsync(Guid userId)
|
||||||
|
{
|
||||||
|
// 检查是否已有邀请码
|
||||||
|
var existingCode = await _inviteCodeRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId)
|
||||||
|
.FirstAsync();
|
||||||
|
|
||||||
|
if (existingCode != null)
|
||||||
|
{
|
||||||
|
return existingCode.Code;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成新邀请码
|
||||||
|
var code = GenerateUniqueInviteCode();
|
||||||
|
var inviteCode = new InviteCodeAggregateRoot(userId, code);
|
||||||
|
await _inviteCodeRepository.InsertAsync(inviteCode);
|
||||||
|
|
||||||
|
_logger.LogInformation($"用户 {userId} 生成邀请码 {code}");
|
||||||
|
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取用户的邀请码信息
|
||||||
|
/// </summary>
|
||||||
|
public async Task<InviteCodeAggregateRoot?> GetUserInviteCodeAsync(Guid userId)
|
||||||
|
{
|
||||||
|
return await _inviteCodeRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId)
|
||||||
|
.FirstAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 统计用户本周邀请人数(别人填写我的邀请码的次数/或者我填写别人邀请码)
|
||||||
|
/// </summary>
|
||||||
|
public async Task<int> GetWeeklyInvitationCountAsync(Guid userId, DateTime weekStart)
|
||||||
|
{
|
||||||
|
var inviterCount= await _invitationRecordRepository._DbQueryable
|
||||||
|
.Where(x => x.InviterId == userId)
|
||||||
|
.Where(x => x.InvitationTime >= weekStart)
|
||||||
|
.CountAsync();
|
||||||
|
var invitedUserIdCount= await _invitationRecordRepository._DbQueryable
|
||||||
|
.Where(x => x.InvitedUserId == userId)
|
||||||
|
.Where(x => x.InvitationTime >= weekStart)
|
||||||
|
.CountAsync();
|
||||||
|
return inviterCount + invitedUserIdCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取邀请历史记录
|
||||||
|
/// </summary>
|
||||||
|
public async Task<List<InvitationHistoryDto>> GetInvitationHistoryAsync(Guid userId, int limit = 10)
|
||||||
|
{
|
||||||
|
return await _invitationRecordRepository._DbQueryable
|
||||||
|
.Where(x => x.InviterId == userId)
|
||||||
|
.OrderBy(x => x.InvitationTime, OrderByType.Desc)
|
||||||
|
.Take(limit)
|
||||||
|
.Select(x => new InvitationHistoryDto
|
||||||
|
{
|
||||||
|
InvitedUserName = "用户***", // 脱敏处理
|
||||||
|
InvitationTime = x.InvitationTime
|
||||||
|
})
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 使用邀请码(双方都增加翻牌次数)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId">使用者ID</param>
|
||||||
|
/// <param name="inviteCode">邀请码</param>
|
||||||
|
/// <returns>邀请人ID</returns>
|
||||||
|
public async Task<Guid> UseInviteCodeAsync(Guid userId, string inviteCode)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(inviteCode))
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("邀请码不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查找邀请码
|
||||||
|
var inviteCodeEntity = await _inviteCodeRepository._DbQueryable
|
||||||
|
.Where(x => x.Code == inviteCode)
|
||||||
|
.FirstAsync();
|
||||||
|
|
||||||
|
if (inviteCodeEntity == null)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("邀请码不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证不能使用自己的邀请码
|
||||||
|
if (inviteCodeEntity.UserId == userId)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("不能使用自己的邀请码");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查当前用户是否已经填写过别人的邀请码(一辈子只能填写一次)
|
||||||
|
var hasUsedOthersCode = await IsFilledInviteCodeAsync(userId);
|
||||||
|
|
||||||
|
if (hasUsedOthersCode)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("您已经填写过别人的邀请码了,每个账号只能填写一次");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 增加邀请码被使用次数
|
||||||
|
inviteCodeEntity.UsedCount++;
|
||||||
|
await _inviteCodeRepository.UpdateAsync(inviteCodeEntity);
|
||||||
|
|
||||||
|
// 创建邀请记录(双方都会因为这条记录增加一次翻牌机会)
|
||||||
|
var invitationRecord = new InvitationRecordAggregateRoot(
|
||||||
|
inviteCodeEntity.UserId,
|
||||||
|
userId,
|
||||||
|
inviteCode);
|
||||||
|
await _invitationRecordRepository.InsertAsync(invitationRecord);
|
||||||
|
|
||||||
|
_logger.LogInformation($"用户 {userId} 使用邀请码 {inviteCode} 成功,邀请人 {inviteCodeEntity.UserId} 和被邀请人 {userId} 都增加一次翻牌机会");
|
||||||
|
|
||||||
|
return inviteCodeEntity.UserId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查用户是否已填写过邀请码
|
||||||
|
/// </summary>
|
||||||
|
public async Task<bool> IsFilledInviteCodeAsync(Guid userId)
|
||||||
|
{
|
||||||
|
// 检查当前用户是否已经填写过别人的邀请码(一辈子只能填写一次)
|
||||||
|
return await _invitationRecordRepository._DbQueryable
|
||||||
|
.Where(x => x.InvitedUserId == userId)
|
||||||
|
.AnyAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成唯一邀请码
|
||||||
|
/// </summary>
|
||||||
|
private string GenerateUniqueInviteCode()
|
||||||
|
{
|
||||||
|
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||||
|
var random = new Random();
|
||||||
|
var code = new char[8];
|
||||||
|
|
||||||
|
for (int i = 0; i < code.Length; i++)
|
||||||
|
{
|
||||||
|
code[i] = chars[random.Next(chars.Length)];
|
||||||
|
}
|
||||||
|
|
||||||
|
return new string(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请历史记录DTO
|
||||||
|
/// </summary>
|
||||||
|
public class InvitationHistoryDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 被邀请人名称(脱敏)
|
||||||
|
/// </summary>
|
||||||
|
public string InvitedUserName { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邀请时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime InvitationTime { get; set; }
|
||||||
|
}
|
||||||
@@ -13,13 +13,14 @@ public class PremiumPackageManager : DomainService
|
|||||||
{
|
{
|
||||||
private readonly ISqlSugarRepository<PremiumPackageAggregateRoot, Guid> _premiumPackageRepository;
|
private readonly ISqlSugarRepository<PremiumPackageAggregateRoot, Guid> _premiumPackageRepository;
|
||||||
private readonly ILogger<PremiumPackageManager> _logger;
|
private readonly ILogger<PremiumPackageManager> _logger;
|
||||||
|
private readonly ISqlSugarRepository<AiRechargeAggregateRoot> _rechargeRepository;
|
||||||
public PremiumPackageManager(
|
public PremiumPackageManager(
|
||||||
ISqlSugarRepository<PremiumPackageAggregateRoot, Guid> premiumPackageRepository,
|
ISqlSugarRepository<PremiumPackageAggregateRoot, Guid> premiumPackageRepository,
|
||||||
ILogger<PremiumPackageManager> logger)
|
ILogger<PremiumPackageManager> logger, ISqlSugarRepository<AiRechargeAggregateRoot> rechargeRepository)
|
||||||
{
|
{
|
||||||
_premiumPackageRepository = premiumPackageRepository;
|
_premiumPackageRepository = premiumPackageRepository;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_rechargeRepository = rechargeRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -57,6 +58,21 @@ public class PremiumPackageManager : DomainService
|
|||||||
|
|
||||||
await _premiumPackageRepository.InsertAsync(premiumPackage);
|
await _premiumPackageRepository.InsertAsync(premiumPackage);
|
||||||
|
|
||||||
|
// 创建充值记录
|
||||||
|
var rechargeRecord = new AiRechargeAggregateRoot
|
||||||
|
{
|
||||||
|
UserId = userId,
|
||||||
|
RechargeAmount = totalAmount,
|
||||||
|
Content = packageName,
|
||||||
|
ExpireDateTime = premiumPackage.ExpireDateTime,
|
||||||
|
Remark = "自助充值",
|
||||||
|
ContactInfo = null,
|
||||||
|
RechargeType = RechargeTypeEnum.PremiumPackage
|
||||||
|
};
|
||||||
|
|
||||||
|
// 保存充值记录到数据库
|
||||||
|
await _rechargeRepository.InsertAsync(rechargeRecord);
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
$"用户 {userId} 购买尊享包成功: {packageName}, Token数量: {tokenAmount}, 金额: {totalAmount}");
|
$"用户 {userId} 购买尊享包成功: {packageName}, Token数量: {tokenAmount}, 金额: {totalAmount}");
|
||||||
|
|
||||||
@@ -69,12 +85,12 @@ public class PremiumPackageManager : DomainService
|
|||||||
/// <param name="userId">用户ID</param>
|
/// <param name="userId">用户ID</param>
|
||||||
/// <param name="tokenCount">需要消耗的Token数量</param>
|
/// <param name="tokenCount">需要消耗的Token数量</param>
|
||||||
/// <returns>是否消耗成功</returns>
|
/// <returns>是否消耗成功</returns>
|
||||||
public async Task<bool> ConsumeTokensAsync(Guid userId, long tokenCount)
|
public async Task<bool> TryConsumeTokensAsync(Guid userId, long tokenCount)
|
||||||
{
|
{
|
||||||
// 获取用户所有可用的尊享包,按剩余token升序排列(优先消耗快用完的)
|
// 获取用户所有可用的尊享包,按剩余token升序排列(优先消耗快用完的)
|
||||||
var availablePackages = await _premiumPackageRepository._DbQueryable
|
var availablePackages = await _premiumPackageRepository._DbQueryable
|
||||||
.Where(x => x.UserId == userId && x.IsActive && x.RemainingTokens > 0)
|
.Where(x => x.UserId == userId && x.IsActive)
|
||||||
.OrderBy(x => x.RemainingTokens)
|
.OrderBy(x => x.CreationTime)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
if (!availablePackages.Any())
|
if (!availablePackages.Any())
|
||||||
@@ -94,34 +110,12 @@ public class PremiumPackageManager : DomainService
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算总可用Token
|
var firstPackage = validPackages.First();
|
||||||
var totalAvailableTokens = validPackages.Sum(p => p.RemainingTokens);
|
// 直接扣除最早的token包需要消耗的token,允许扣减到负数
|
||||||
if (totalAvailableTokens < tokenCount)
|
firstPackage.ConsumeTokens(tokenCount);
|
||||||
{
|
await _premiumPackageRepository.UpdateAsync(firstPackage);
|
||||||
_logger.LogWarning(
|
|
||||||
$"用户 {userId} 尊享包Token不足,需要: {tokenCount}, 可用: {totalAvailableTokens}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 从可用的包中逐个扣除Token
|
return true;
|
||||||
var remainingToConsume = tokenCount;
|
|
||||||
foreach (var package in validPackages)
|
|
||||||
{
|
|
||||||
if (remainingToConsume <= 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
var toConsume = Math.Min(remainingToConsume, package.RemainingTokens);
|
|
||||||
if (package.ConsumeTokens(toConsume))
|
|
||||||
{
|
|
||||||
await _premiumPackageRepository.UpdateAsync(package);
|
|
||||||
remainingToConsume -= toConsume;
|
|
||||||
|
|
||||||
_logger.LogInformation(
|
|
||||||
$"用户 {userId} 从尊享包 {package.Id} 消耗 {toConsume} tokens, 剩余: {package.RemainingTokens}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return remainingToConsume == 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -130,55 +124,11 @@ public class PremiumPackageManager : DomainService
|
|||||||
/// <param name="userId">用户ID</param>
|
/// <param name="userId">用户ID</param>
|
||||||
/// <returns>可用Token总数</returns>
|
/// <returns>可用Token总数</returns>
|
||||||
public async Task<long> GetAvailableTokensAsync(Guid userId)
|
public async Task<long> GetAvailableTokensAsync(Guid userId)
|
||||||
{
|
|
||||||
var packages = await _premiumPackageRepository._DbQueryable
|
|
||||||
.Where(x => x.UserId == userId && x.IsActive && x.RemainingTokens > 0)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
return packages
|
|
||||||
.Where(p => p.IsAvailable())
|
|
||||||
.Sum(p => p.RemainingTokens);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取用户的所有尊享包
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="userId">用户ID</param>
|
|
||||||
/// <returns>尊享包列表</returns>
|
|
||||||
public async Task<List<PremiumPackageAggregateRoot>> GetUserPremiumPackagesAsync(Guid userId)
|
|
||||||
{
|
{
|
||||||
return await _premiumPackageRepository._DbQueryable
|
return await _premiumPackageRepository._DbQueryable
|
||||||
.Where(x => x.UserId == userId)
|
.Where(x => x.UserId == userId && x.IsActive && x.RemainingTokens > 0)
|
||||||
.OrderByDescending(x => x.CreationTime)
|
.Where(p => p.IsActive)
|
||||||
.ToListAsync();
|
.Where(p => !p.ExpireDateTime.HasValue || p.ExpireDateTime.Value >= DateTime.Now)
|
||||||
}
|
.SumAsync(p => p.RemainingTokens);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 停用过期的尊享包
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>停用的包数量</returns>
|
|
||||||
public async Task<int> DeactivateExpiredPackagesAsync()
|
|
||||||
{
|
|
||||||
_logger.LogInformation("开始执行尊享包过期自动停用任务");
|
|
||||||
|
|
||||||
var now = DateTime.Now;
|
|
||||||
var expiredPackages = await _premiumPackageRepository._DbQueryable
|
|
||||||
.Where(x => x.IsActive && x.ExpireDateTime.HasValue && x.ExpireDateTime.Value < now)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
if (!expiredPackages.Any())
|
|
||||||
{
|
|
||||||
_logger.LogInformation("没有找到过期的尊享包");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var package in expiredPackages)
|
|
||||||
{
|
|
||||||
package.Deactivate();
|
|
||||||
await _premiumPackageRepository.UpdateAsync(package);
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.LogInformation($"成功停用 {expiredPackages.Count} 个过期的尊享包");
|
|
||||||
return expiredPackages.Count;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,64 +1,134 @@
|
|||||||
using Volo.Abp.Domain.Services;
|
using SqlSugar;
|
||||||
using Volo.Abp.Users;
|
using Volo.Abp.Domain.Services;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities;
|
||||||
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
||||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
namespace Yi.Framework.AiHub.Domain.Managers;
|
namespace Yi.Framework.AiHub.Domain.Managers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Token验证结果
|
||||||
|
/// </summary>
|
||||||
|
public class TokenValidationResult
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 用户Id
|
||||||
|
/// </summary>
|
||||||
|
public Guid UserId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Token Id
|
||||||
|
/// </summary>
|
||||||
|
public Guid TokenId { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
public class TokenManager : DomainService
|
public class TokenManager : DomainService
|
||||||
{
|
{
|
||||||
private readonly ISqlSugarRepository<TokenAggregateRoot> _tokenRepository;
|
private readonly ISqlSugarRepository<TokenAggregateRoot> _tokenRepository;
|
||||||
|
private readonly ISqlSugarRepository<UsageStatisticsAggregateRoot> _usageStatisticsRepository;
|
||||||
|
|
||||||
public TokenManager(ISqlSugarRepository<TokenAggregateRoot> tokenRepository)
|
public TokenManager(
|
||||||
|
ISqlSugarRepository<TokenAggregateRoot> tokenRepository,
|
||||||
|
ISqlSugarRepository<UsageStatisticsAggregateRoot> usageStatisticsRepository)
|
||||||
{
|
{
|
||||||
_tokenRepository = tokenRepository;
|
_tokenRepository = tokenRepository;
|
||||||
|
_usageStatisticsRepository = usageStatisticsRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string?> GetAsync(Guid userId)
|
/// <summary>
|
||||||
{
|
/// 验证Token并返回用户Id和TokenId
|
||||||
var entity = await _tokenRepository._DbQueryable.FirstAsync(x => x.UserId == userId);
|
/// </summary>
|
||||||
if (entity is not null)
|
/// <param name="token">Token密钥</param>
|
||||||
{
|
/// <param name="modelId">模型Id(用于判断是否是尊享模型需要检查额度)</param>
|
||||||
return entity.Token;
|
/// <returns>Token验证结果</returns>
|
||||||
}
|
public async Task<TokenValidationResult> ValidateTokenAsync(string? token, string? modelId = null)
|
||||||
else
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task CreateAsync(Guid userId)
|
|
||||||
{
|
|
||||||
var entity = await _tokenRepository._DbQueryable.FirstAsync(x => x.UserId == userId);
|
|
||||||
if (entity is not null)
|
|
||||||
{
|
|
||||||
entity.ResetToken();
|
|
||||||
await _tokenRepository.UpdateAsync(entity);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var token = new TokenAggregateRoot(userId);
|
|
||||||
await _tokenRepository.InsertAsync(token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<Guid> GetUserIdAsync(string? token)
|
|
||||||
{
|
{
|
||||||
if (token is null)
|
if (token is null)
|
||||||
{
|
{
|
||||||
throw new UserFriendlyException("当前请求未包含token", "401");
|
throw new UserFriendlyException("当前请求未包含token", "401");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (token.StartsWith("yi-"))
|
if (!token.StartsWith("yi-"))
|
||||||
{
|
{
|
||||||
var entity = await _tokenRepository._DbQueryable.Where(x => x.Token == token).FirstAsync();
|
throw new UserFriendlyException("当前请求token非法", "401");
|
||||||
|
}
|
||||||
|
|
||||||
|
var entity = await _tokenRepository._DbQueryable
|
||||||
|
.Where(x => x.Token == token)
|
||||||
|
.FirstAsync();
|
||||||
|
|
||||||
if (entity is null)
|
if (entity is null)
|
||||||
{
|
{
|
||||||
throw new UserFriendlyException("当前请求token无效", "401");
|
throw new UserFriendlyException("当前请求token无效", "401");
|
||||||
}
|
}
|
||||||
|
|
||||||
return entity.UserId;
|
// 检查Token是否被禁用
|
||||||
|
if (entity.IsDisabled)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("当前Token已被禁用,请启用后再使用", "403");
|
||||||
}
|
}
|
||||||
throw new UserFriendlyException("当前请求token非法", "401");
|
|
||||||
|
// 检查Token是否过期
|
||||||
|
if (entity.ExpireTime.HasValue && entity.ExpireTime.Value < DateTime.Now)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("当前Token已过期,请更新过期时间或创建新的Token", "403");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果是尊享模型且Token设置了额度限制,检查是否超限
|
||||||
|
if (!string.IsNullOrEmpty(modelId) &&
|
||||||
|
PremiumPackageConst.ModeIds.Contains(modelId) &&
|
||||||
|
entity.PremiumQuotaLimit.HasValue)
|
||||||
|
{
|
||||||
|
var usedQuota = await GetTokenPremiumUsedQuotaAsync(entity.UserId, entity.Id);
|
||||||
|
if (usedQuota >= entity.PremiumQuotaLimit.Value)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException($"当前Token的尊享包额度已用完(已使用:{usedQuota},限制:{entity.PremiumQuotaLimit.Value}),请调整额度限制或使用其他Token", "403");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new TokenValidationResult
|
||||||
|
{
|
||||||
|
UserId = entity.UserId,
|
||||||
|
TokenId = entity.Id
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取Token的尊享包已使用额度
|
||||||
|
/// </summary>
|
||||||
|
private async Task<long> GetTokenPremiumUsedQuotaAsync(Guid userId, Guid tokenId)
|
||||||
|
{
|
||||||
|
var premiumModelIds = PremiumPackageConst.ModeIds;
|
||||||
|
|
||||||
|
var usedQuota = await _usageStatisticsRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId && x.TokenId == tokenId && premiumModelIds.Contains(x.ModelId))
|
||||||
|
.SumAsync(x => x.TotalTokenCount);
|
||||||
|
|
||||||
|
return usedQuota;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取用户的Token(兼容旧接口,返回第一个可用的Token)
|
||||||
|
/// </summary>
|
||||||
|
[Obsolete("请使用 ValidateTokenAsync 方法")]
|
||||||
|
public async Task<string?> GetAsync(Guid userId)
|
||||||
|
{
|
||||||
|
var entity = await _tokenRepository._DbQueryable
|
||||||
|
.Where(x => x.UserId == userId && !x.IsDisabled)
|
||||||
|
.OrderBy(x => x.CreationTime)
|
||||||
|
.FirstAsync();
|
||||||
|
|
||||||
|
return entity?.Token;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取用户Id(兼容旧接口)
|
||||||
|
/// </summary>
|
||||||
|
[Obsolete("请使用 ValidateTokenAsync 方法")]
|
||||||
|
public async Task<Guid> GetUserIdAsync(string? token)
|
||||||
|
{
|
||||||
|
var result = await ValidateTokenAsync(token);
|
||||||
|
return result.UserId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,8 +18,10 @@ public class UsageStatisticsManager : DomainService
|
|||||||
private IDistributedLockProvider DistributedLock =>
|
private IDistributedLockProvider DistributedLock =>
|
||||||
LazyServiceProvider.LazyGetRequiredService<IDistributedLockProvider>();
|
LazyServiceProvider.LazyGetRequiredService<IDistributedLockProvider>();
|
||||||
|
|
||||||
public async Task SetUsageAsync(Guid? userId, string modelId, ThorUsageResponse? tokenUsage)
|
public async Task SetUsageAsync(Guid? userId, string modelId, ThorUsageResponse? tokenUsage, Guid? tokenId = null)
|
||||||
{
|
{
|
||||||
|
var actualTokenId = tokenId ?? Guid.Empty;
|
||||||
|
|
||||||
long inputTokenCount = tokenUsage?.PromptTokens
|
long inputTokenCount = tokenUsage?.PromptTokens
|
||||||
?? tokenUsage?.InputTokens
|
?? tokenUsage?.InputTokens
|
||||||
?? 0;
|
?? 0;
|
||||||
@@ -28,10 +30,10 @@ public class UsageStatisticsManager : DomainService
|
|||||||
?? tokenUsage?.OutputTokens
|
?? tokenUsage?.OutputTokens
|
||||||
?? 0;
|
?? 0;
|
||||||
|
|
||||||
await using (await DistributedLock.AcquireLockAsync($"UsageStatistics:{userId?.ToString()}"))
|
await using (await DistributedLock.AcquireLockAsync($"UsageStatistics:{userId?.ToString()}:{actualTokenId}:{modelId}"))
|
||||||
{
|
{
|
||||||
var entity = await _repository._DbQueryable.FirstAsync(x => x.UserId == userId && x.ModelId == modelId);
|
var entity = await _repository._DbQueryable.FirstAsync(x => x.UserId == userId && x.ModelId == modelId && x.TokenId == actualTokenId);
|
||||||
//存在数据,更细
|
//存在数据,更新
|
||||||
if (entity is not null)
|
if (entity is not null)
|
||||||
{
|
{
|
||||||
entity.AddOnceChat(inputTokenCount, outputTokenCount);
|
entity.AddOnceChat(inputTokenCount, outputTokenCount);
|
||||||
@@ -40,7 +42,7 @@ public class UsageStatisticsManager : DomainService
|
|||||||
//不存在插入
|
//不存在插入
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var usage = new UsageStatisticsAggregateRoot(userId, modelId);
|
var usage = new UsageStatisticsAggregateRoot(userId, modelId, actualTokenId);
|
||||||
usage.AddOnceChat(inputTokenCount, outputTokenCount);
|
usage.AddOnceChat(inputTokenCount, outputTokenCount);
|
||||||
await _repository.InsertAsync(usage);
|
await _repository.InsertAsync(usage);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ namespace Yi.Framework.AiHub.Domain
|
|||||||
nameof(DeepSeekChatCompletionsService));
|
nameof(DeepSeekChatCompletionsService));
|
||||||
services.AddKeyedTransient<IChatCompletionService, OpenAiChatCompletionsService>(
|
services.AddKeyedTransient<IChatCompletionService, OpenAiChatCompletionsService>(
|
||||||
nameof(OpenAiChatCompletionsService));
|
nameof(OpenAiChatCompletionsService));
|
||||||
|
services.AddKeyedTransient<IChatCompletionService, ClaudiaChatCompletionsService>(
|
||||||
|
nameof(ClaudiaChatCompletionsService));
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -72,6 +74,34 @@ namespace Yi.Framework.AiHub.Domain
|
|||||||
//ai模型特殊性兼容处理
|
//ai模型特殊性兼容处理
|
||||||
Configure<SpecialCompatibleOptions>(options =>
|
Configure<SpecialCompatibleOptions>(options =>
|
||||||
{
|
{
|
||||||
|
options.Handles.Add(request =>
|
||||||
|
{
|
||||||
|
if (request.Model == "gpt-5.1-chat")
|
||||||
|
{
|
||||||
|
request.Temperature = null;
|
||||||
|
request.TopP = null;
|
||||||
|
request.MaxCompletionTokens = request.MaxTokens;
|
||||||
|
request.MaxTokens = null;
|
||||||
|
request.PresencePenalty = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
options.Handles.Add(request =>
|
||||||
|
{
|
||||||
|
if (request.Model =="gpt-5-mini")
|
||||||
|
{
|
||||||
|
request.Temperature = null;
|
||||||
|
request.TopP = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
options.Handles.Add(request =>
|
||||||
|
{
|
||||||
|
if (request.Model == "databricks-claude-sonnet-4")
|
||||||
|
{
|
||||||
|
request.PresencePenalty = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
options.Handles.Add(request =>
|
options.Handles.Add(request =>
|
||||||
{
|
{
|
||||||
if (request.Model == "o1")
|
if (request.Model == "o1")
|
||||||
@@ -100,9 +130,9 @@ namespace Yi.Framework.AiHub.Domain
|
|||||||
});
|
});
|
||||||
options.Handles.Add(request =>
|
options.Handles.Add(request =>
|
||||||
{
|
{
|
||||||
if (request.MaxTokens >= 16384)
|
if (request.MaxTokens > 128000)
|
||||||
{
|
{
|
||||||
request.MaxTokens = 16384;
|
request.MaxTokens = 128000;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
options.AnthropicHandles.add(request =>
|
options.AnthropicHandles.add(request =>
|
||||||
@@ -127,6 +157,7 @@ namespace Yi.Framework.AiHub.Domain
|
|||||||
{
|
{
|
||||||
builder.ConfigureHttpClient(client =>
|
builder.ConfigureHttpClient(client =>
|
||||||
{
|
{
|
||||||
|
client.DefaultRequestHeaders.Add("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
|
||||||
client.Timeout = TimeSpan.FromMinutes(10);
|
client.Timeout = TimeSpan.FromMinutes(10);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ namespace Yi.Framework.Bbs.Domain.Entities.Integral
|
|||||||
[SugarTable("SignIn")]
|
[SugarTable("SignIn")]
|
||||||
|
|
||||||
[SugarIndex($"index_{nameof(CreatorId)}", nameof(CreatorId), OrderByType.Asc)]
|
[SugarIndex($"index_{nameof(CreatorId)}", nameof(CreatorId), OrderByType.Asc)]
|
||||||
public class SignInAggregateRoot : AggregateRoot<Guid>, ICreationAuditedObject
|
public class
|
||||||
|
SignInAggregateRoot : AggregateRoot<Guid>, ICreationAuditedObject
|
||||||
{
|
{
|
||||||
|
|
||||||
[SugarColumn(IsPrimaryKey = true)]
|
[SugarColumn(IsPrimaryKey = true)]
|
||||||
|
|||||||
@@ -56,8 +56,8 @@ namespace Yi.Framework.Rbac.Application.Services
|
|||||||
return new NotFoundResult();
|
return new NotFoundResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
var steam = await File.ReadAllBytesAsync(path);
|
var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||||
return new FileContentResult(steam, file.GetMimeMapping());
|
return new FileStreamResult(stream, file!.GetMimeMapping());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ namespace Yi.Framework.Rbac.Application.Services.System
|
|||||||
.WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!))
|
.WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!))
|
||||||
.WhereIF(input.State is not null, u => u.State == input.State)
|
.WhereIF(input.State is not null, u => u.State == input.State)
|
||||||
.OrderBy(u => u.OrderNum, OrderByType.Asc)
|
.OrderBy(u => u.OrderNum, OrderByType.Asc)
|
||||||
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
|
.ToListAsync();
|
||||||
return new PagedResultDto<DeptGetListOutputDto>
|
return new PagedResultDto<DeptGetListOutputDto>
|
||||||
{
|
{
|
||||||
Items = await MapToGetListOutputDtosAsync(entities),
|
Items = await MapToGetListOutputDtosAsync(entities),
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
using Volo.Abp.Application.Dtos;
|
using Volo.Abp.Application.Dtos;
|
||||||
using Yi.Framework.Ddd.Application;
|
using Yi.Framework.Ddd.Application;
|
||||||
@@ -54,5 +55,25 @@ namespace Yi.Framework.Rbac.Application.Services.System
|
|||||||
throw new UserFriendlyException(RoleConst.Exist);
|
throw new UserFriendlyException(RoleConst.Exist);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 更新状态
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="state"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[Route("post/{id}/{state}")]
|
||||||
|
public async Task<PostGetOutputDto> UpdateStateAsync([FromRoute] Guid id, [FromRoute] bool state)
|
||||||
|
{
|
||||||
|
var entity = await _repository.GetByIdAsync(id);
|
||||||
|
if (entity is null)
|
||||||
|
{
|
||||||
|
throw new ApplicationException("岗位未存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
entity.State = state;
|
||||||
|
await _repository.UpdateAsync(entity);
|
||||||
|
return await MapToGetOutputDtoAsync(entity);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace Yi.Framework.Rbac.Domain.Shared.Caches;
|
namespace Yi.Framework.Rbac.Domain.Shared.Caches;
|
||||||
|
|
||||||
public class FileCacheItem
|
public class FileCacheItem
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -34,8 +34,7 @@ namespace Yi.Framework.Rbac.Domain.Entities
|
|||||||
var type = GetFileType();
|
var type = GetFileType();
|
||||||
|
|
||||||
var savePath = GetSaveFilePath();
|
var savePath = GetSaveFilePath();
|
||||||
var filePath = Path.Combine(savePath, this.FileName);
|
this.FilePath = savePath;
|
||||||
this.FilePath = filePath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Text.Json;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
using Yi.Abp.Web;
|
using Yi.Abp.Web;
|
||||||
|
|||||||
@@ -29,12 +29,17 @@ using Volo.Abp.Swashbuckle;
|
|||||||
using Yi.Abp.Application;
|
using Yi.Abp.Application;
|
||||||
using Yi.Abp.SqlsugarCore;
|
using Yi.Abp.SqlsugarCore;
|
||||||
using Yi.Framework.AiHub.Application;
|
using Yi.Framework.AiHub.Application;
|
||||||
|
using Yi.Framework.AiHub.Application.Services;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
||||||
|
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
||||||
using Yi.Framework.AspNetCore;
|
using Yi.Framework.AspNetCore;
|
||||||
using Yi.Framework.AspNetCore.Authentication.OAuth;
|
using Yi.Framework.AspNetCore.Authentication.OAuth;
|
||||||
using Yi.Framework.AspNetCore.Authentication.OAuth.Gitee;
|
using Yi.Framework.AspNetCore.Authentication.OAuth.Gitee;
|
||||||
using Yi.Framework.AspNetCore.Authentication.OAuth.QQ;
|
using Yi.Framework.AspNetCore.Authentication.OAuth.QQ;
|
||||||
using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder;
|
using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder;
|
||||||
using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection;
|
using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Yi.Framework.AspNetCore.UnifyResult;
|
||||||
using Yi.Framework.BackgroundWorkers.Hangfire;
|
using Yi.Framework.BackgroundWorkers.Hangfire;
|
||||||
using Yi.Framework.Bbs.Application;
|
using Yi.Framework.Bbs.Application;
|
||||||
using Yi.Framework.Bbs.Application.Extensions;
|
using Yi.Framework.Bbs.Application.Extensions;
|
||||||
@@ -46,6 +51,7 @@ using Yi.Framework.Rbac.Application;
|
|||||||
using Yi.Framework.Rbac.Domain.Authorization;
|
using Yi.Framework.Rbac.Domain.Authorization;
|
||||||
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
||||||
using Yi.Framework.Rbac.Domain.Shared.Options;
|
using Yi.Framework.Rbac.Domain.Shared.Options;
|
||||||
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
using Yi.Framework.Stock.Application;
|
using Yi.Framework.Stock.Application;
|
||||||
using Yi.Framework.TenantManagement.Application;
|
using Yi.Framework.TenantManagement.Application;
|
||||||
|
|
||||||
@@ -123,6 +129,7 @@ namespace Yi.Abp.Web
|
|||||||
});
|
});
|
||||||
|
|
||||||
//采用furion格式的规范化api,默认不开启,使用abp优雅的方式
|
//采用furion格式的规范化api,默认不开启,使用abp优雅的方式
|
||||||
|
//前置:需要将管道工作单元前加上app.Properties.Add("_AbpExceptionHandlingMiddleware_Added",false);
|
||||||
//你没看错。。。
|
//你没看错。。。
|
||||||
//service.AddFurionUnifyResultApi();
|
//service.AddFurionUnifyResultApi();
|
||||||
|
|
||||||
@@ -350,6 +357,10 @@ namespace Yi.Abp.Web
|
|||||||
var app = context.GetApplicationBuilder();
|
var app = context.GetApplicationBuilder();
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
||||||
|
// app.ApplicationServices.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient.CodeFirst.InitTables<MessageAggregateRoot>();
|
||||||
|
// app.ApplicationServices.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient.CodeFirst.InitTables<TokenAggregateRoot>();
|
||||||
|
// app.ApplicationServices.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient.CodeFirst.InitTables<UsageStatisticsAggregateRoot>();
|
||||||
|
|
||||||
//跨域
|
//跨域
|
||||||
app.UseCors(DefaultCorsPolicyName);
|
app.UseCors(DefaultCorsPolicyName);
|
||||||
|
|
||||||
@@ -394,7 +405,6 @@ namespace Yi.Abp.Web
|
|||||||
app.UseDefaultFiles();
|
app.UseDefaultFiles();
|
||||||
app.UseDirectoryBrowser("/api/app/wwwroot");
|
app.UseDirectoryBrowser("/api/app/wwwroot");
|
||||||
|
|
||||||
|
|
||||||
//app.Properties.Add("_AbpExceptionHandlingMiddleware_Added",false);
|
//app.Properties.Add("_AbpExceptionHandlingMiddleware_Added",false);
|
||||||
//工作单元
|
//工作单元
|
||||||
app.UseUnitOfWork();
|
app.UseUnitOfWork();
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
{
|
|
||||||
"env": {
|
|
||||||
"ANTHROPIC_AUTH_TOKEN": "sk-lVcGFMOQfXYtZWp1rD4SaBNDNpM270UX2wDqWh",
|
|
||||||
"ANTHROPIC_BASE_URL": "https://api.token-ai.cn",
|
|
||||||
"ANTHROPIC_MODEL": "gpt-4o-mini"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
9
Yi.Ai.Vue3/.claude/settings.local.json
Normal file
9
Yi.Ai.Vue3/.claude/settings.local.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"permissions": {
|
||||||
|
"allow": [
|
||||||
|
"Bash(npx vue-tsc --noEmit)"
|
||||||
|
],
|
||||||
|
"deny": [],
|
||||||
|
"ask": []
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,8 +5,6 @@
|
|||||||
"ComputedRef": true,
|
"ComputedRef": true,
|
||||||
"DirectiveBinding": true,
|
"DirectiveBinding": true,
|
||||||
"EffectScope": true,
|
"EffectScope": true,
|
||||||
"ElMessage": true,
|
|
||||||
"ElMessageBox": true,
|
|
||||||
"ExtractDefaultPropTypes": true,
|
"ExtractDefaultPropTypes": true,
|
||||||
"ExtractPropTypes": true,
|
"ExtractPropTypes": true,
|
||||||
"ExtractPublicPropTypes": true,
|
"ExtractPublicPropTypes": true,
|
||||||
|
|||||||
@@ -54,6 +54,13 @@ pnpm lint:stylelint # 样式格式化
|
|||||||
pnpm cz # 规范提交(自动执行lint)
|
pnpm cz # 规范提交(自动执行lint)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 服务端启动
|
||||||
|
目录:E:\devDemo\Yi\Yi.Abp.Net8\src\Yi.Abp.Web
|
||||||
|
```bash
|
||||||
|
dotnet run
|
||||||
|
```
|
||||||
|
|
||||||
## 🧸 即将推出 (含 接口联调)
|
## 🧸 即将推出 (含 接口联调)
|
||||||
- [x] 会话管理
|
- [x] 会话管理
|
||||||
- [x] 发送消息
|
- [x] 发送消息
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
<link rel="icon" href="/favicon.ico"/>
|
<link rel="icon" href="/favicon.ico"/>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||||
<meta name="baidu-site-verification" content="codeva-mkVpSFmYJm"/>
|
<meta name="baidu-site-verification" content="codeva-mkVpSFmYJm"/>
|
||||||
<meta name="description" content="意心AI:一站式多模型 AI 平台,提供 GPT-4o、DeepSeek 等服务"/>
|
<meta name="description" content="意心AI:一站式多模型 AI 平台,提供 AI 服务"/>
|
||||||
<meta name="description" content="各大主流AI无限制使用,直连,AI,claude ,DeepSeek,open-ai"/>
|
<meta name="description" content="各大主流AI无限制使用,直连,AI,claude ,DeepSeek,open-ai"/>
|
||||||
<meta name="keywords" content="意心AI, GPT-4.5, 多模型AI, AI工具"/>
|
<meta name="keywords" content="意心AI, 多模型AI, AI工具"/>
|
||||||
<meta name="keywords" content="橙子,chengzi,橙子老哥,ccnetcore,意社区"/>
|
<meta name="keywords" content="橙子,chengzi,橙子老哥,ccnetcore,意社区"/>
|
||||||
<meta name="author" content="橙子,chengzi,橙子老哥,ccnetcore"/>
|
<meta name="author" content="橙子,chengzi,橙子老哥,ccnetcore"/>
|
||||||
<meta name="version" content="%VITE_APP_VERSION%"/>
|
<meta name="version" content="%VITE_APP_VERSION%"/>
|
||||||
@@ -112,7 +112,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<!-- 加载动画容器 -->
|
<!-- 加载动画容器 -->
|
||||||
<div id="yixinai-loader" class="loader-container">
|
<div id="yixinai-loader" class="loader-container">
|
||||||
<div class="loader-title">意心Ai</div>
|
<div class="loader-title">意心Ai 2.5</div>
|
||||||
<div class="loader-subtitle">海外地址,仅首次访问预计加载约10秒</div>
|
<div class="loader-subtitle">海外地址,仅首次访问预计加载约10秒</div>
|
||||||
<div class="loader-logo">
|
<div class="loader-logo">
|
||||||
<div class="pulse-box"></div>
|
<div class="pulse-box"></div>
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
12
Yi.Ai.Vue3/src/api/announcement/index.ts
Normal file
12
Yi.Ai.Vue3/src/api/announcement/index.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import type { AnnouncementLogDto } from './types';
|
||||||
|
import { get } from '@/utils/request';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取系统公告和活动数据
|
||||||
|
* 后端接口: GET /api/app/announcement
|
||||||
|
* 返回格式: AnnouncementLogDto[]
|
||||||
|
*/
|
||||||
|
export function getSystemAnnouncements() {
|
||||||
|
return get<AnnouncementLogDto[]>('/announcement').json();
|
||||||
|
}
|
||||||
|
export * from './types';
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user