|
|
|
@ -1,4 +1,5 @@ |
|
|
|
using Dapper; |
|
|
|
using Microsoft.Extensions.Caching.Memory; |
|
|
|
using Microsoft.Extensions.Configuration; |
|
|
|
using Microsoft.Identity.Client; |
|
|
|
using Newtonsoft.Json; |
|
|
|
@ -23,7 +24,8 @@ namespace Shentun.Peis.PlugIns.ThirdPushs.Hty |
|
|
|
//当前系统数据库地址
|
|
|
|
protected string? AppConnctionStr; |
|
|
|
protected ThirdInterfaceDto? _thirdInterfaceDto; |
|
|
|
protected string? _thirdToken; |
|
|
|
private string? _thirdToken; |
|
|
|
private readonly IMemoryCache _cache; |
|
|
|
|
|
|
|
static PushPeisResultPlugInsBase() |
|
|
|
{ |
|
|
|
@ -32,6 +34,7 @@ namespace Shentun.Peis.PlugIns.ThirdPushs.Hty |
|
|
|
|
|
|
|
public PushPeisResultPlugInsBase(Guid thirdInterfaceId) |
|
|
|
{ |
|
|
|
_cache = new MemoryCache(new MemoryCacheOptions()); |
|
|
|
|
|
|
|
AppConfig = new ConfigurationBuilder() |
|
|
|
.SetBasePath(DirectoryHelper.GetAppDirectory()) // 设置基础路径为当前目录
|
|
|
|
@ -58,34 +61,70 @@ namespace Shentun.Peis.PlugIns.ThirdPushs.Hty |
|
|
|
InterfaceConfig = configurationBuilder.Build(); |
|
|
|
} |
|
|
|
|
|
|
|
GetThirdToken(); |
|
|
|
// GetThirdToken();
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void GetThirdToken() |
|
|
|
protected string GetThirdToken() |
|
|
|
{ |
|
|
|
var pushBaseApi = InterfaceConfig.GetValue("Interface:PushBaseApi", ""); |
|
|
|
var thirdLoginUser = InterfaceConfig.GetValue("Interface:ThirdLoginUser", ""); |
|
|
|
var thirdLoginPassWord = InterfaceConfig.GetValue("Interface:ThirdLoginPassWord", ""); |
|
|
|
|
|
|
|
var formContent = new MultipartFormDataContent(); |
|
|
|
formContent.Add(new StringContent(thirdLoginUser), "username"); |
|
|
|
formContent.Add(new StringContent(thirdLoginPassWord), "password"); |
|
|
|
formContent.Add(new StringContent("Ea"), "type"); |
|
|
|
|
|
|
|
var result = (HtyHttpPostHelper.PostFormDataAsync($"{pushBaseApi}/api/loginEaV.action", formContent, "PostmanRuntime-ApipostRuntime/1.1.0")).GetAwaiter().GetResult(); |
|
|
|
|
|
|
|
var resModel = JsonConvert.DeserializeObject<GetHtyTokenDto>(result); |
|
|
|
if (resModel != null && resModel.state == "suc") |
|
|
|
if (!IsTokenValid()) |
|
|
|
{ |
|
|
|
_thirdToken = resModel.token2; |
|
|
|
var pushBaseApi = InterfaceConfig.GetValue("Interface:PushBaseApi", ""); |
|
|
|
var thirdLoginUser = InterfaceConfig.GetValue("Interface:ThirdLoginUser", ""); |
|
|
|
var thirdLoginPassWord = InterfaceConfig.GetValue("Interface:ThirdLoginPassWord", ""); |
|
|
|
|
|
|
|
var formContent = new MultipartFormDataContent(); |
|
|
|
formContent.Add(new StringContent(thirdLoginUser), "username"); |
|
|
|
formContent.Add(new StringContent(thirdLoginPassWord), "password"); |
|
|
|
formContent.Add(new StringContent("Ea"), "type"); |
|
|
|
|
|
|
|
var result = (HtyHttpPostHelper.PostFormDataAsync($"{pushBaseApi}/api/loginEaV.action", formContent, "PostmanRuntime-ApipostRuntime/1.1.0")).GetAwaiter().GetResult(); |
|
|
|
|
|
|
|
var resModel = JsonConvert.DeserializeObject<GetHtyTokenDto>(result); |
|
|
|
if (resModel != null && resModel.state == "suc") |
|
|
|
{ |
|
|
|
_thirdToken = resModel.token2; |
|
|
|
SaveToken(_thirdToken); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
throw new UserFriendlyException("获取token失败,检查配置的账户"); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
throw new UserFriendlyException("获取token失败,检查配置的账户"); |
|
|
|
_thirdToken = GetToken(); |
|
|
|
} |
|
|
|
|
|
|
|
return _thirdToken; |
|
|
|
} |
|
|
|
|
|
|
|
// 保存从其他系统获取的 Token
|
|
|
|
public void SaveToken(string token) |
|
|
|
{ |
|
|
|
var cacheEntryOptions = new MemoryCacheEntryOptions() |
|
|
|
.SetAbsoluteExpiration(TimeSpan.FromHours(1)) // 设置1小时过期
|
|
|
|
.SetPriority(CacheItemPriority.NeverRemove); // 避免被优先清除
|
|
|
|
|
|
|
|
_cache.Set("ExternalSystemToken", token, cacheEntryOptions); |
|
|
|
} |
|
|
|
|
|
|
|
// 获取 Token
|
|
|
|
public string GetToken() |
|
|
|
{ |
|
|
|
_cache.TryGetValue("ExternalSystemToken", out string token); |
|
|
|
return token; |
|
|
|
} |
|
|
|
|
|
|
|
// 检查 Token 是否有效
|
|
|
|
public bool IsTokenValid() |
|
|
|
{ |
|
|
|
return _cache.TryGetValue("ExternalSystemToken", out _); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |