diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Server/Volo/Abp/AspNetCore/Components/Server/Extensibility/BlazorServerLookupApiRequestService.cs b/framework/src/Volo.Abp.AspNetCore.Components.Server/Volo/Abp/AspNetCore/Components/Server/Extensibility/BlazorServerLookupApiRequestService.cs new file mode 100644 index 0000000000..61ff6acb3e --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.Server/Volo/Abp/AspNetCore/Components/Server/Extensibility/BlazorServerLookupApiRequestService.cs @@ -0,0 +1,82 @@ +using System; +using System.Globalization; +using System.Net.Http; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Components.Web.Extensibility; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Http.Client; +using Volo.Abp.Http.Client.Authentication; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.AspNetCore.Components.Server.Extensibility +{ + public class BlazorServerLookupApiRequestService : ILookupApiRequestService, ITransientDependency + { + public IHttpClientFactory HttpClientFactory { get; } + public IRemoteServiceHttpClientAuthenticator HttpClientAuthenticator { get; } + + public AbpRemoteServiceOptions RemoteServiceOptions { get; } + + public ICurrentTenant CurrentTenant { get; } + public NavigationManager NavigationManager { get; } + + public BlazorServerLookupApiRequestService(IHttpClientFactory httpClientFactory, + IRemoteServiceHttpClientAuthenticator httpClientAuthenticator, + ICurrentTenant currentTenant, + IOptions remoteServiceOptions, + NavigationManager navigationManager) + { + HttpClientFactory = httpClientFactory; + HttpClientAuthenticator = httpClientAuthenticator; + RemoteServiceOptions = remoteServiceOptions.Value; + CurrentTenant = currentTenant; + NavigationManager = navigationManager; + } + + public async Task SendAsync(string url) + { + var client = HttpClientFactory.CreateClient(); + var requestMessage = new HttpRequestMessage(HttpMethod.Get, url); + AddHeaders(requestMessage); + + var uri = new Uri(url, UriKind.RelativeOrAbsolute); + if (!uri.IsAbsoluteUri) + { + var baseUrl = string.Empty; + try + { + var remoteServiceConfig = RemoteServiceOptions.RemoteServices.GetConfigurationOrDefault("Default"); + baseUrl = remoteServiceConfig.BaseUrl; + } + catch (AbpException) + { + baseUrl = NavigationManager.BaseUri; + } + + client.BaseAddress = new Uri(baseUrl); + await HttpClientAuthenticator.Authenticate(new RemoteServiceHttpClientAuthenticateContext(client, + requestMessage, new RemoteServiceConfiguration(baseUrl), string.Empty)); + } + + var response = await client.SendAsync(requestMessage); + + return await response.Content.ReadAsStringAsync(); + } + + protected virtual void AddHeaders(HttpRequestMessage requestMessage) + { + if (CurrentTenant.Id.HasValue) + { + requestMessage.Headers.Add(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString()); + } + + var currentCulture = CultureInfo.CurrentUICulture.Name ?? CultureInfo.CurrentCulture.Name; + if (!currentCulture.IsNullOrEmpty()) + { + requestMessage.Headers.AcceptLanguage.Add(new(currentCulture)); + } + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/Extensibility/ILookupApiRequestService.cs b/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/Extensibility/ILookupApiRequestService.cs new file mode 100644 index 0000000000..2351606306 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/Extensibility/ILookupApiRequestService.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; +using JetBrains.Annotations; + +namespace Volo.Abp.AspNetCore.Components.Web.Extensibility +{ + public interface ILookupApiRequestService + { + Task SendAsync([NotNull]string url); + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/Extensibility/WebAssemblyLookupApiRequestService.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/Extensibility/WebAssemblyLookupApiRequestService.cs new file mode 100644 index 0000000000..be4a6f8bfb --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/Extensibility/WebAssemblyLookupApiRequestService.cs @@ -0,0 +1,69 @@ +using System; +using System.Globalization; +using System.Net.Http; +using System.Threading.Tasks; +using Castle.Components.DictionaryAdapter; +using Fody; +using Volo.Abp.AspNetCore.Components.Web.Extensibility; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Http.Client.Authentication; +using Volo.Abp.Http.Client; +using Microsoft.Extensions.Options; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly.Extensibility +{ + public class WebAssemblyLookupApiRequestService : ILookupApiRequestService, ITransientDependency + { + public IHttpClientFactory HttpClientFactory { get; } + public IRemoteServiceHttpClientAuthenticator HttpClientAuthenticator { get; } + + public AbpRemoteServiceOptions RemoteServiceOptions { get; } + + public ICurrentTenant CurrentTenant { get; } + + public WebAssemblyLookupApiRequestService(IHttpClientFactory httpClientFactory, + IRemoteServiceHttpClientAuthenticator httpClientAuthenticator, + ICurrentTenant currentTenant, + IOptions remoteServiceOptions) + { + HttpClientFactory = httpClientFactory; + HttpClientAuthenticator = httpClientAuthenticator; + RemoteServiceOptions = remoteServiceOptions.Value; + CurrentTenant = currentTenant; + } + + public async Task SendAsync(string url) + { + var client = HttpClientFactory.CreateClient(); + var requestMessage = new HttpRequestMessage(HttpMethod.Get, url); + AddHeaders(requestMessage); + + var uri = new Uri(url, UriKind.RelativeOrAbsolute); + if (!uri.IsAbsoluteUri) + { + var remoteServiceConfig = RemoteServiceOptions.RemoteServices.GetConfigurationOrDefault("Default"); + client.BaseAddress = new Uri(remoteServiceConfig.BaseUrl); + await HttpClientAuthenticator.Authenticate(new RemoteServiceHttpClientAuthenticateContext(client, requestMessage, new RemoteServiceConfiguration(remoteServiceConfig.BaseUrl), string.Empty)); + } + + var response = await client.SendAsync(requestMessage); + + return await response.Content.ReadAsStringAsync(); + } + + protected virtual void AddHeaders(HttpRequestMessage requestMessage) + { + if (CurrentTenant.Id.HasValue) + { + requestMessage.Headers.Add(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString()); + } + + var currentCulture = CultureInfo.CurrentUICulture.Name ?? CultureInfo.CurrentCulture.Name; + if (!currentCulture.IsNullOrEmpty()) + { + requestMessage.Headers.AcceptLanguage.Add(new (currentCulture)); + } + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.BlazoriseUI/Components/ObjectExtending/LookupExtensionProperty.razor.cs b/framework/src/Volo.Abp.BlazoriseUI/Components/ObjectExtending/LookupExtensionProperty.razor.cs index 2ace2ebfed..b4e74b16d9 100644 --- a/framework/src/Volo.Abp.BlazoriseUI/Components/ObjectExtending/LookupExtensionProperty.razor.cs +++ b/framework/src/Volo.Abp.BlazoriseUI/Components/ObjectExtending/LookupExtensionProperty.razor.cs @@ -9,10 +9,10 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Text.Json; using System.Threading.Tasks; +using Volo.Abp.AspNetCore.Components.Web.Extensibility; using Volo.Abp.Data; using Volo.Abp.Http; using Volo.Abp.Http.Client; -//using Volo.Abp.Http.Client.Authentication; using Volo.Abp.MultiTenancy; using Volo.Abp.ObjectExtending; @@ -23,35 +23,20 @@ namespace Volo.Abp.BlazoriseUI.Components.ObjectExtending { protected List> lookupItems; - [Inject] - public IStringLocalizerFactory StringLocalizerFactory { get; set; } + [Inject] public IStringLocalizerFactory StringLocalizerFactory { get; set; } - [Parameter] - public TEntity Entity { get; set; } + [Parameter] public TEntity Entity { get; set; } - [Parameter] - public ObjectExtensionPropertyInfo PropertyInfo { get; set; } + [Parameter] public ObjectExtensionPropertyInfo PropertyInfo { get; set; } - //[Inject] - //public IRemoteServiceHttpClientAuthenticator ClientAuthenticator { get; set; } - //[Inject] - //public IHttpClientFactory HttpClientFactory { get; set; } - - [Inject] - public ICurrentTenant CurrentTenant { get; set; } - - //[Inject] - //public IOptions RemoteServiceOptions { get; set; } + [Inject] public ILookupApiRequestService LookupApiService { get; set; } public string TextPropertyName => PropertyInfo.Name + "_Text"; public object SelectedValue { - get - { - return Entity.GetProperty(PropertyInfo.Name); - } + get { return Entity.GetProperty(PropertyInfo.Name); } set { Entity.SetProperty(PropertyInfo.Name, value, false); @@ -67,7 +52,8 @@ namespace Volo.Abp.BlazoriseUI.Components.ObjectExtending protected override void OnParametersSet() { var value = Entity.GetProperty(PropertyInfo.Name); - if (value != null) + var text = Entity.GetProperty(TextPropertyName); + if (value != null && text != null) { lookupItems.Add(new SelectItem { @@ -88,34 +74,24 @@ namespace Volo.Abp.BlazoriseUI.Components.ObjectExtending var selectItems = new List>(); var url = PropertyInfo.Lookup.Url; - var uri = new Uri(url, UriKind.RelativeOrAbsolute); if (!filter.IsNullOrEmpty()) { url += $"?{PropertyInfo.Lookup.FilterParamName}={filter.Trim()}"; } - //var client = HttpClientFactory.CreateClient(); - //var requestMessage = new HttpRequestMessage(HttpMethod.Get, url); - //AddHeaders(requestMessage); - - //if (!uri.IsAbsoluteUri) - //{ - // var remoteServiceConfig = RemoteServiceOptions.Value.RemoteServices.GetConfigurationOrDefault("Default"); - // client.BaseAddress = new Uri(remoteServiceConfig.BaseUrl); - // await ClientAuthenticator.Authenticate(new RemoteServiceHttpClientAuthenticateContext(client, requestMessage, new RemoteServiceConfiguration(remoteServiceConfig.BaseUrl), string.Empty)); - //} - - //var response = await client.SendAsync(requestMessage); - //var document = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync()); - //var itemsArrayProp = document.RootElement.GetProperty(PropertyInfo.Lookup.ResultListPropertyName); - //foreach (var item in itemsArrayProp.EnumerateArray()) - //{ - // selectItems.Add(new SelectItem - // { - // Text = item.GetProperty(PropertyInfo.Lookup.DisplayPropertyName).GetString(), - // Value = JsonSerializer.Deserialize(item.GetProperty(PropertyInfo.Lookup.ValuePropertyName).GetRawText(), PropertyInfo.Type) - // }); - //} + var response = await LookupApiService.SendAsync(url); + + var document = JsonDocument.Parse(response); + var itemsArrayProp = document.RootElement.GetProperty(PropertyInfo.Lookup.ResultListPropertyName); + foreach (var item in itemsArrayProp.EnumerateArray()) + { + selectItems.Add(new SelectItem + { + Text = item.GetProperty(PropertyInfo.Lookup.DisplayPropertyName).GetString(), + Value = JsonSerializer.Deserialize( + item.GetProperty(PropertyInfo.Lookup.ValuePropertyName).GetRawText(), PropertyInfo.Type) + }); + } return selectItems; } @@ -129,21 +105,5 @@ namespace Volo.Abp.BlazoriseUI.Components.ObjectExtending { lookupItems = await GetLookupItemsAsync(filter); } - - protected virtual void AddHeaders(HttpRequestMessage requestMessage) - { - if (CurrentTenant.Id.HasValue) - { - requestMessage.Headers.Add(TenantResolverConsts.DefaultTenantKey, CurrentTenant.Id.Value.ToString()); - } - - var currentCulture = CultureInfo.CurrentUICulture.Name ?? CultureInfo.CurrentCulture.Name; - if (!currentCulture.IsNullOrEmpty()) - { - requestMessage.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue(currentCulture)); - } - - //requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MimeTypes.Application.Json)); - } } -} +} \ No newline at end of file