dignite-projects / dignite-abp

Adding to the ecology of the ABP framework: Adds notification system, dynamic forms module, file manager, Pure Theme, and other enhancements.
http://dignite.com/dignite-abp
GNU Lesser General Public License v3.0
52 stars 12 forks source link

Notification system balzor webassembly ui support ? #77

Closed happtim closed 1 day ago

happtim commented 1 month ago

I use Blazor WebAssembly for my UI interface. I attempted to reference Dignite.Abp.NotificationCenter.Blazor.WebAssembly and encountered some issues, which I then tried to fix.

duguankui commented 1 month ago

I am traveling with my family and will test it when I return to Japan. Thanks for following Dignite Abp.

happtim commented 1 month ago

Have a pleasant journey.

happtim commented 1 month ago

There are two main modifications.

In the initialization of the file WebAssembly.Toolbar

        protected override async Task OnInitializedAsync()
        {

            var baseUrl = Configuration.GetValue<string>("RemoteServices:NotificationCenter:BaseUrl");

            if (baseUrl.IsNullOrEmpty())
            {
                baseUrl = Configuration.GetValue<string>("RemoteServices:Default:BaseUrl");
            }

            LocalizationResource = typeof(NotificationCenterResource);
            if (CurrentUser.IsAuthenticated)
            {
                (await AccessTokenProvider.RequestAccessToken()).TryGetToken(out var accessToken);

                hubConnection = new HubConnectionBuilder()
                    .WithUrl(baseUrl.EnsureEndsWith('/') + "signalr-hubs/notifications", options =>
                    {
                        options.AccessTokenProvider = () => Task.FromResult((string?)accessToken!.Value);
                    })
                    .Build();

                notificationCount = await NotificationAppService.GetCountAsync(UserNotificationState.Unread);

                hubConnection.On<RealTimeNotifyEto>("ReceiveNotifications", async (eto) =>
                {
                    notificationCount++;
                    hasNewNotifications = true;
                    await InvokeAsync(StateHasChanged);
                });

                await hubConnection.StartAsync();
            }

            await base.OnInitializedAsync();
        }

and in the ConfigureService of the file DigniteAbpNotificationCenterHttpApiClientModule in http.api.client project

        Configure<AbpSystemTextJsonSerializerOptions>(options => 
        {
            options.JsonSerializerOptions.Converters.Add(new NotificationDataConverter());
        });

the NotificationDataConverter class

    public class NotificationDataConverter : JsonConverter<NotificationData>
    {
        public override NotificationData Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            using (JsonDocument doc = JsonDocument.ParseValue(ref reader))
            {
                JsonElement root = doc.RootElement;

                if (root.TryGetProperty("type", out JsonElement typeElement))
                {
                    string type = typeElement.GetString();

                    switch (type)
                    {
                        case "Dignite.Abp.Notifications.MessageNotificationData":
                            return JsonSerializer.Deserialize<MessageNotificationData>(root.GetRawText(), options);
                        // 如果有其他类型,可以在这里添加更多 case
                        default:
                            throw new JsonException($"Unknown notification data type: {type}");
                    }
                }

                throw new JsonException("Type property not found in notification data");
            }
        }

        public override void Write(Utf8JsonWriter writer, NotificationData value, JsonSerializerOptions options)
        {
            JsonSerializer.Serialize(writer, value, value.GetType(), options);
        }
    }
duguankui commented 1 month ago

If you have fixed the relevant BUG, ​​please submit it to the project and I will merge your code.

duguankui commented 1 day ago

The code has just been merged, thank you very much for your contribution!