Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<ProjectReference Include="..\LINGYUN.Abp.WeChat.MiniProgram\LINGYUN.Abp.WeChat.MiniProgram.csproj" />
<ProjectReference Include="..\LINGYUN.Abp.WeChat.Official\LINGYUN.Abp.WeChat.Official.csproj" />
<ProjectReference Include="..\LINGYUN.Abp.WeChat.Work.Contacts\LINGYUN.Abp.WeChat.Work.Contacts.csproj" />
<ProjectReference Include="..\LINGYUN.Abp.WeChat.Work.ExternalContact\LINGYUN.Abp.WeChat.Work.ExternalContact.csproj" />
<ProjectReference Include="..\LINGYUN.Abp.WeChat.Work\LINGYUN.Abp.WeChat.Work.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LINGYUN.Abp.WeChat.Official;
using LINGYUN.Abp.WeChat.Work;
using LINGYUN.Abp.WeChat.Work.Contacts;
using LINGYUN.Abp.WeChat.Work.ExternalContact;
using LINGYUN.Abp.WeChat.Work.Localization;
using Localization.Resources.AbpUi;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -18,6 +19,7 @@ namespace LINGYUN.Abp.WeChat.SettingManagement;
typeof(AbpWeChatMiniProgramModule),
typeof(AbpWeChatWorkModule),
typeof(AbpWeChatWorkContactModule),
typeof(AbpWeChatWorkExternalContactModule),
typeof(AbpAspNetCoreMvcModule))]
public class AbpWeChatSettingManagementModule : AbpModule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using LINGYUN.Abp.WeChat.Settings;
using LINGYUN.Abp.WeChat.Work.Contacts.Features;
using LINGYUN.Abp.WeChat.Work.Contacts.Settings;
using LINGYUN.Abp.WeChat.Work.ExternalContact.Features;
using LINGYUN.Abp.WeChat.Work.ExternalContact.Settings;
using LINGYUN.Abp.WeChat.Work.Features;
using LINGYUN.Abp.WeChat.Work.Settings;
using System.Threading.Tasks;
Expand Down Expand Up @@ -186,6 +188,16 @@ await SettingManager.GetOrNullAsync(WeChatWorkContactSettingNames.Secret, provid
providerName);
}

if (await FeatureChecker.IsEnabledAsync(WeChatWorkExternalContactFeatureNames.Enable))
{
workConnectionSetting.AddDetail(
await SettingDefinitionManager.GetAsync(WeChatWorkExternalContactSettingNames.Secret),
StringLocalizerFactory,
await SettingManager.GetOrNullAsync(WeChatWorkExternalContactSettingNames.Secret, providerName, providerKey),
ValueType.String,
providerName);
}

workConnectionSetting.AddDetail(
await SettingDefinitionManager.GetAsync(WeChatWorkSettingNames.Connection.Token),
StringLocalizerFactory,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using LINGYUN.Abp.WeChat.Work.Contacts.Features;
using LINGYUN.Abp.WeChat.Work.Contacts.Settings;
using LINGYUN.Abp.WeChat.Work.Settings;
using LINGYUN.Abp.WeChat.Work.Token;
using LINGYUN.Abp.WeChat.Work.Token.Models;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -19,22 +15,15 @@
namespace LINGYUN.Abp.WeChat.Work.Contacts.Token;

[RequiresFeature(WeChatWorkContactsFeatureNames.Enable)]
public class WeChatWorkContactTokenProvider : IWeChatWorkContactTokenProvider, ISingletonDependency
public class WeChatWorkContactTokenProvider : WeChatWorkTokenProviderBase, IWeChatWorkContactTokenProvider, ISingletonDependency
{
public ILogger<WeChatWorkContactTokenProvider> Logger { get; set; }
protected ISettingProvider SettingProvider { get; }
protected IHttpClientFactory HttpClientFactory { get; }
protected IDistributedCache<WeChatWorkTokenCacheItem> WeChatWorkTokenCache { get; }
protected override string ProviderName => "WeChatWorkContactToken";
public WeChatWorkContactTokenProvider(
ISettingProvider settingProvider,
IHttpClientFactory httpClientFactory,
IDistributedCache<WeChatWorkTokenCacheItem> cache)
: base(settingProvider, httpClientFactory, cache)
{
HttpClientFactory = httpClientFactory;
SettingProvider = settingProvider;
WeChatWorkTokenCache = cache;

Logger = NullLogger<WeChatWorkContactTokenProvider>.Instance;
}

public async virtual Task<WeChatWorkToken> GetTokenAsync(CancellationToken cancellationToken = default)
Expand All @@ -56,85 +45,6 @@ public async virtual Task<WeChatWorkToken> GetTokenAsync(
string secret,
CancellationToken cancellationToken = default)
{
return (await GetCacheItemAsync("WeChatWorkContactToken", corpId, agentId, secret, cancellationToken)).Token;
}
/// <summary>
/// 获取缓存中的Token配置
/// </summary>
/// <param name="provider"></param>
/// <param name="corpId"></param>
/// <param name="agentId"></param>
/// <param name="secret"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected async virtual Task<WeChatWorkTokenCacheItem> GetCacheItemAsync(
string provider,
string corpId,
string agentId,
string secret,
CancellationToken cancellationToken = default)
{
Check.NotNullOrEmpty(corpId, nameof(corpId));
Check.NotNullOrEmpty(agentId, nameof(agentId));
Check.NotNullOrEmpty(secret, nameof(secret));

var cacheKey = WeChatWorkTokenCacheItem.CalculateCacheKey(provider, corpId, agentId);

return await GetCacheItemAsync(cacheKey, provider, corpId, agentId, secret);
}
/// <summary>
/// 获取或刷新分布式缓存中的Token配置
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="provider"></param>
/// <param name="corpId"></param>
/// <param name="agentId"></param>
/// <param name="secret"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected async virtual Task<WeChatWorkTokenCacheItem> GetCacheItemAsync(
string cacheKey,
string provider,
string corpId,
string agentId,
string secret,
CancellationToken cancellationToken = default)
{
var cacheItem = await WeChatWorkTokenCache.GetAsync(cacheKey, token: cancellationToken);

if (cacheItem != null)
{
Logger.LogDebug($"Found WeChatWorkContactToken in the cache: {cacheKey}");
return cacheItem;
}

Logger.LogDebug($"Not found WeChatWorkContactToken in the cache, getting from the httpClient: {cacheKey}");

var client = HttpClientFactory.CreateWeChatWorkApiClient();

var request = new WeChatWorkTokenRequest
{
CorpId = corpId,
CorpSecret = secret,
};

using var response = await client.GetTokenAsync(request, cancellationToken);
var tokenResponse = await response.DeserializeObjectAsync<WeChatWorkTokenResponse>();
var token = tokenResponse.ToWeChatWorkToken();
cacheItem = new WeChatWorkTokenCacheItem(corpId, agentId, token);

Logger.LogDebug($"Setting the cache item: {cacheKey}");

var cacheOptions = new DistributedCacheEntryOptions
{
// 设置绝对过期时间为Token有效期剩余的二分钟
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(token.ExpiresIn - 100),
};

await WeChatWorkTokenCache.SetAsync(cacheKey, cacheItem, cacheOptions, token: cancellationToken);

Logger.LogDebug($"Finished setting the cache item: {cacheKey}");

return cacheItem;
return (await InternalGetTokenAsync(corpId, agentId, secret, cancellationToken)).Token;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
using LINGYUN.Abp.WeChat.Work.Token.Models;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace System.Net.Http;
namespace System.Net.Http;

internal static partial class HttpClientWeChatWorkRequestExtensions
{
public async static Task<HttpResponseMessage> GetTokenAsync(this HttpMessageInvoker client, WeChatWorkTokenRequest request, CancellationToken cancellationToken = default)
{
var urlBuilder = new StringBuilder();
urlBuilder.Append("/cgi-bin/gettoken");
urlBuilder.AppendFormat("?corpid={0}", request.CorpId);
urlBuilder.AppendFormat("&corpsecret={0}", request.CorpSecret);

var httpRequest = new HttpRequestMessage(HttpMethod.Get, urlBuilder.ToString());

return await client.SendAsync(httpRequest, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Nullable>enable</Nullable>
<RootNamespace />
<!--<Version>9.3.6.3</Version>
<PackageVersion>9.3.6.3</PackageVersion>-->
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
"del_external_contact" => context.GetWeChatMessage<ExternalContactDeleteEvent>(),
"del_follow_user" => context.GetWeChatMessage<ExternalContactDeleteFollowUserEvent>(),
"transfer_fail" => context.GetWeChatMessage<ExternalContactTransferFailEvent>(),
"msg_audit_approved" => context.GetWeChatMessage<ExternalContactMsgAuditApprovedEvent>(),
_ => throw new AbpWeChatException($"Contact change event change_external_contact:{changeType} is not mounted!"),
};
});
Expand Down Expand Up @@ -69,6 +70,8 @@ public override void ConfigureServices(ServiceConfigurationContext context)
_ => throw new AbpWeChatException($"Contact change event change_external_tag:{changeType} is not mounted!"),
};
});
// 产生会话回调事件
options.MapEvent("msgaudit_notify", context => context.GetWeChatMessage<ExternalContactMsgAuditNotifyEvent>());
});

Configure<AbpVirtualFileSystemOptions>(options =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ public async virtual Task<WeChatWorkUploadAttachmentResponse> UploadAsync(
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.UploadAsync(token.AccessToken, request, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkUploadAttachmentResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.UploadAsync(token.AccessToken, request, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ public async virtual Task<WeChatWorkGetExternalContactListResponse> GetExternalC
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.GetExternalContactListAsync(token.AccessToken, request, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkGetExternalContactListResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.GetExternalContactListAsync(token.AccessToken, request, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public async virtual Task<WeChatWorkGetCustomerListResponse> GetCustomerListAsyn
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.GetCustomerListAsync(token.AccessToken, userId, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkGetCustomerListResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.GetCustomerListAsync(token.AccessToken, userId, cancellationToken);
}

public async virtual Task<WeChatWorkBulkGetCustomerResponse> BulkGetCustomerAsync(
Expand All @@ -51,11 +47,7 @@ public async virtual Task<WeChatWorkBulkGetCustomerResponse> BulkGetCustomerAsyn
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.BulkGetCustomerAsync(token.AccessToken, request, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkBulkGetCustomerResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.BulkGetCustomerAsync(token.AccessToken, request, cancellationToken);
}

public async virtual Task<WeChatWorkGetCustomerResponse> GetCustomerAsync(
Expand All @@ -68,11 +60,7 @@ public async virtual Task<WeChatWorkGetCustomerResponse> GetCustomerAsync(
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.GetCustomerAsync(token.AccessToken, externalUserid, cursor, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkGetCustomerResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.GetCustomerAsync(token.AccessToken, externalUserid, cursor, cancellationToken);
}

public async virtual Task<WeChatWorkResponse> UpdateCustomerRemarkAsync(
Expand All @@ -84,10 +72,6 @@ public async virtual Task<WeChatWorkResponse> UpdateCustomerRemarkAsync(
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.UpdateCustomerRemarkAsync(token.AccessToken, request, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.UpdateCustomerRemarkAsync(token.AccessToken, request, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ public async virtual Task<WeChatWorkGetCustomerStrategyListResponse> GetCustomer
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.GetCustomerStrategyListAsync(token.AccessToken, request, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkGetCustomerStrategyListResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.GetCustomerStrategyListAsync(token.AccessToken, request, cancellationToken);
}

public async virtual Task<WeChatWorkGetCustomerStrategyResponse> GetCustomerStrategyAsync(
Expand All @@ -46,11 +42,7 @@ public async virtual Task<WeChatWorkGetCustomerStrategyResponse> GetCustomerStra
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.GetCustomerStrategyAsync(token.AccessToken, request, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkGetCustomerStrategyResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.GetCustomerStrategyAsync(token.AccessToken, request, cancellationToken);
}

public async virtual Task<WeChatWorkGetCustomerStrategyRangeResponse> GetCustomerStrategyRangeAsync(
Expand All @@ -60,11 +52,7 @@ public async virtual Task<WeChatWorkGetCustomerStrategyRangeResponse> GetCustome
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.GetCustomerStrategyRangeAsync(token.AccessToken, request, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkGetCustomerStrategyRangeResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.GetCustomerStrategyRangeAsync(token.AccessToken, request, cancellationToken);
}

public async virtual Task<WeChatWorkCreateCustomerStrategyResponse> CreateCustomerStrategyAsync(
Expand All @@ -74,11 +62,7 @@ public async virtual Task<WeChatWorkCreateCustomerStrategyResponse> CreateCustom
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.CreateCustomerStrategyAsync(token.AccessToken, request, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkCreateCustomerStrategyResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.CreateCustomerStrategyAsync(token.AccessToken, request, cancellationToken);
}

public async virtual Task<WeChatWorkResponse> UpdateCustomerStrategyAsync(
Expand All @@ -88,11 +72,7 @@ public async virtual Task<WeChatWorkResponse> UpdateCustomerStrategyAsync(
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.UpdateCustomerStrategyAsync(token.AccessToken, request, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.UpdateCustomerStrategyAsync(token.AccessToken, request, cancellationToken);
}

public async virtual Task<WeChatWorkResponse> DeleteCustomerStrategyAsync(
Expand All @@ -102,10 +82,6 @@ public async virtual Task<WeChatWorkResponse> DeleteCustomerStrategyAsync(
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.DeleteCustomerStrategyAsync(token.AccessToken, request, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.DeleteCustomerStrategyAsync(token.AccessToken, request, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ public async virtual Task<WeChatWorkGetFollowUserListResponse> GetFollowUserList
var token = await WeChatWorkTokenProvider.GetTokenAsync(cancellationToken);
var client = HttpClientFactory.CreateWeChatWorkApiClient();

using var response = await client.GetFollowUserListAsync(token.AccessToken, cancellationToken);

var wechatResponse = await response.DeserializeObjectAsync<WeChatWorkGetFollowUserListResponse>();
wechatResponse.ThrowIfNotSuccess();
return wechatResponse;
return await client.GetFollowUserListAsync(token.AccessToken, cancellationToken);
}
}
Loading
Loading