Skip to content

Commit 2da994b

Browse files
committed
feat: 增加投票弹窗
1 parent dd08b2f commit 2da994b

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@
1515
<a href="" class="reload">Reload</a>
1616
<a class="dismiss">🗙</a>
1717
</div>
18+
19+
@code {
20+
RenderFragment RenderVote =>
21+
@<div>
22+
<p style="font-weight: bold;">我正在参加 Gitee 2025 最受欢迎的开源软件投票活动,快来给我投票吧!</p>
23+
<div style="display: flex; justify-content: space-around;"><a href="https://gitee.com/activity/2025opensource?ident=I6MYBB" target="_blank" style="font-weight: bold;">必须投一票</a> <a href="https://gitee.com/activity/2025opensource?ident=I6MYBB" target="_blank" class="text-muted" title="老六你居然不投票">我知道了</a></div>
24+
</div>;
25+
}

src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.cs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
using Microsoft.Extensions.Options;
77
using Microsoft.JSInterop;
8+
using System.Globalization;
89

910
namespace BootstrapBlazor.Server.Components.Layout;
1011

1112
/// <summary>
1213
/// 母版页基类
1314
/// </summary>
14-
public partial class BaseLayout : IDisposable
15+
public partial class BaseLayout : IAsyncDisposable
1516
{
1617
[Inject]
1718
[NotNull]
@@ -50,6 +51,8 @@ public partial class BaseLayout : IDisposable
5051
private string? CancelText { get; set; }
5152

5253
private bool _init = false;
54+
private JSModule? _module;
55+
private DotNetObjectReference<BaseLayout>? _interop;
5356

5457
/// <summary>
5558
/// <inheritdoc/>
@@ -71,13 +74,18 @@ protected override void OnInitialized()
7174
/// <inheritdoc/>
7275
/// </summary>
7376
/// <returns></returns>
74-
protected override async Task OnInitializedAsync()
77+
protected override async Task OnAfterRenderAsync(bool firstRender)
7578
{
76-
await base.OnInitializedAsync();
79+
await base.OnAfterRenderAsync(firstRender);
7780

78-
var module = await JSRuntime.LoadModule($"{WebsiteOption.Value.JSModuleRootPath}Layout/BaseLayout.razor.js");
79-
await module.InvokeVoidAsync("doTask");
80-
_init = true;
81+
if (firstRender)
82+
{
83+
_module = await JSRuntime.LoadModule($"{WebsiteOption.Value.JSModuleRootPath}Layout/BaseLayout.razor.js");
84+
_interop = DotNetObjectReference.Create(this);
85+
await _module.InvokeVoidAsync("doTask", _interop);
86+
_init = true;
87+
StateHasChanged();
88+
}
8189
}
8290

8391
private async Task NotifyCommit(DispatchEntry<GiteePostBody> payload)
@@ -121,25 +129,55 @@ private async Task NotifyReboot(DispatchEntry<bool> payload)
121129
}
122130
}
123131

132+
/// <summary>
133+
/// 显示投票弹窗
134+
/// </summary>
135+
/// <returns></returns>
136+
[JSInvokable]
137+
public async Task ShowVoteToast()
138+
{
139+
// 英文环境不投票
140+
if(CultureInfo.CurrentUICulture.Name == "en-US")
141+
{
142+
return;
143+
}
144+
145+
_option = new ToastOption()
146+
{
147+
Category = ToastCategory.Information,
148+
Title = "Gitee 评选活动",
149+
IsAutoHide = false,
150+
ChildContent = RenderVote,
151+
PreventDuplicates = true
152+
};
153+
await Toast.Show(_option);
154+
}
155+
124156
/// <summary>
125157
/// 释放资源
126158
/// </summary>
127159
/// <param name="disposing"></param>
128-
private void Dispose(bool disposing)
160+
private async ValueTask DisposeAsync(bool disposing)
129161
{
130162
if (disposing)
131163
{
132164
CommitDispatchService.UnSubscribe(NotifyCommit);
133165
RebootDispatchService.UnSubscribe(NotifyReboot);
166+
167+
if (_module != null)
168+
{
169+
await _module.InvokeVoidAsync("dispose");
170+
await _module.DisposeAsync();
171+
}
134172
}
135173
}
136174

137175
/// <summary>
138176
/// 释放资源
139177
/// </summary>
140-
public void Dispose()
178+
public async ValueTask DisposeAsync()
141179
{
142-
Dispose(true);
180+
await DisposeAsync(true);
143181
GC.SuppressFinalize(this);
144182
}
145183
}

src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ function initTheme() {
55
setTheme(currentTheme, false);
66
}
77

8-
export function doTask() {
8+
export function doTask(invoke) {
99
initTheme();
10+
const handler = setTimeout(() => {
11+
clearTimeout(handler);
12+
invoke.invokeMethodAsync("ShowVoteToast");
13+
}, 1000);
14+
}
15+
16+
export function dispose() {
17+
1018
}

0 commit comments

Comments
 (0)