Files
Yi.Admin/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/Chat/TokenService.cs

55 lines
1.6 KiB
C#
Raw Normal View History

2025-07-03 22:31:39 +08:00
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Application.Services;
using Volo.Abp.Users;
2025-07-03 22:44:52 +08:00
using Yi.Framework.AiHub.Application.Contracts.Dtos.Token;
2025-07-03 22:31:39 +08:00
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
2025-07-05 15:11:56 +08:00
using Yi.Framework.AiHub.Domain.Extensions;
2025-07-03 22:31:39 +08:00
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]
2025-07-03 22:44:52 +08:00
public async Task<TokenOutput> GetAsync()
2025-07-03 22:31:39 +08:00
{
2025-07-03 22:44:52 +08:00
return new TokenOutput
{
ApiKey = await _tokenManager.GetAsync(CurrentUser.GetId())
};
2025-07-03 22:31:39 +08:00
}
/// <summary>
/// 创建token
/// </summary>
/// <exception cref="UserFriendlyException"></exception>
[Authorize]
public async Task CreateAsync()
{
2025-07-05 15:11:56 +08:00
if (!CurrentUser.IsAiVip())
2025-07-03 22:31:39 +08:00
{
throw new UserFriendlyException("充值成为Vip畅享第三方token服务");
2025-07-03 22:31:39 +08:00
}
await _tokenManager.CreateAsync(CurrentUser.GetId());
}
}