Files
2024-11-08 16:36:10 +08:00

43 lines
1.1 KiB
C#

using Dlhis.Middle.Apis;
using Microsoft.Extensions.Caching.Memory;
namespace Dlhis.Middle.Services.Implements;
public class TokenService : ITokenService
{
private readonly IMemoryCache _memoryCache;
private readonly ICommonApi _commonApi;
public TokenService(IMemoryCache memoryCache, ICommonApi commonApi)
{
_memoryCache = memoryCache;
_commonApi = commonApi;
}
/// <inheritdoc />
public async Task<string> AuthAsync()
{
var exists = _memoryCache.TryGetValue("token", out string token);
try
{
if (!exists)
{
var tokenInfo = await _commonApi.AuthAsync(new
{
ClientId = "e509c2aaa98e300c",
ClientSecret = "6184d8979fc48105"
});
token = tokenInfo.Data.AccessToken;
_memoryCache.Set("token", token, TimeSpan.FromSeconds(tokenInfo.Data.Expires));
}
}
finally
{
token = "Bearer " + token;
}
return token;
}
}