From cd29abe12c213f9d494003f4f4ced5e7290bdaf1 Mon Sep 17 00:00:00 2001
From: DismissedLight <1686188646@qq.com>
Date: Fri, 3 Jan 2025 15:47:48 +0800
Subject: [PATCH] https://hut.ao/tasks/2
---
.../Snap.Hutao/Resource/Localization/SH.resx | 3 +
.../Service/Hutao/HutaoUserOptions.cs | 67 +++++++++++++------
.../UI/Xaml/View/Page/HutaoPassportPage.xaml | 5 ++
.../HutaoPassport/HutaoPassportViewModel.cs | 6 ++
4 files changed, 59 insertions(+), 22 deletions(-)
diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx
index 604f026bc8..f7420dd324 100644
--- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx
+++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx
@@ -3296,6 +3296,9 @@
使用兑换码
+
+ 刷新
+
注册
diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Hutao/HutaoUserOptions.cs b/src/Snap.Hutao/Snap.Hutao/Service/Hutao/HutaoUserOptions.cs
index e3ffb2dc08..c9663d7650 100644
--- a/src/Snap.Hutao/Snap.Hutao/Service/Hutao/HutaoUserOptions.cs
+++ b/src/Snap.Hutao/Snap.Hutao/Service/Hutao/HutaoUserOptions.cs
@@ -60,7 +60,7 @@ public async ValueTask GetIsHutaoCdnAllowedAsync()
return IsHutaoCdnAllowed;
}
- public async ValueTask GetAuthTokenAsync()
+ public async ValueTask GetAuthTokenAsync(CancellationToken token = default)
{
using (await operationLock.LockAsync(nameof(GetAuthTokenAsync)).ConfigureAwait(false))
{
@@ -74,7 +74,7 @@ public async ValueTask GetIsHutaoCdnAllowedAsync()
if (authTokenExpiration.ExpireAt < DateTimeOffset.UtcNow)
{
// Re-initialize to refresh the token
- await InitializeAsync().ConfigureAwait(false);
+ await InitializeAsync(token).ConfigureAwait(false);
}
if (!IsLoggedIn)
@@ -112,6 +112,21 @@ public Task WaitUserInfoInitializationAsync()
return infoEvent.WaitAsync();
}
+ public async ValueTask RefreshUserInfoAsync(CancellationToken token = default)
+ {
+ using (await operationLock.LockAsync(nameof(RefreshUserInfoAsync)).ConfigureAwait(false))
+ {
+ // Wait previous Info Event
+ await infoEvent.WaitAsync().ConfigureAwait(false);
+ infoEvent.Reset();
+
+ if (await GetAuthTokenAsync(token).ConfigureAwait(false) is { } authToken)
+ {
+ await RefreshUserInfoCoreAsync(token).ConfigureAwait(false);
+ }
+ }
+ }
+
public async ValueTask LoginAsync(string username, string password, bool resuming = false, CancellationToken token = default)
{
using (await operationLock.LockAsync(nameof(LoginAsync)).ConfigureAwait(false))
@@ -245,6 +260,34 @@ private async ValueTask AcceptAuthTokenAsync(string? username, string? password,
IsLoggedIn = true;
loginEvent.Set();
+ await RefreshUserInfoCoreAsync(token).ConfigureAwait(false);
+ }
+ }
+
+ private async ValueTask LogoutOrUnregisterAsync()
+ {
+ using (await operationLock.LockAsync(nameof(LogoutOrUnregisterAsync)).ConfigureAwait(false))
+ {
+ LocalSetting.Set(SettingKeys.PassportUserName, string.Empty);
+ LocalSetting.Set(SettingKeys.PassportPassword, string.Empty);
+
+ await taskContext.SwitchToMainThreadAsync();
+ authTokenExpiration = default;
+ UserName = default;
+ IsLoggedIn = false;
+ IsDeveloper = false;
+ IsMaintainer = false;
+ IsHutaoCloudAllowed = false;
+ CloudExpireAt = default;
+ IsHutaoCdnAllowed = false;
+ CdnExpireAt = default;
+ }
+ }
+
+ private async ValueTask RefreshUserInfoCoreAsync(CancellationToken token = default)
+ {
+ using (await operationLock.LockAsync(nameof(RefreshUserInfoCoreAsync)).ConfigureAwait(false))
+ {
await taskContext.SwitchToBackgroundAsync();
HutaoPassportClient passportClient = serviceProvider.GetRequiredService();
Response userInfoResponse = await passportClient.GetUserInfoAsync(token).ConfigureAwait(false);
@@ -273,26 +316,6 @@ private async ValueTask AcceptAuthTokenAsync(string? username, string? password,
}
}
- private async ValueTask LogoutOrUnregisterAsync()
- {
- using (await operationLock.LockAsync(nameof(LogoutOrUnregisterAsync)).ConfigureAwait(false))
- {
- LocalSetting.Set(SettingKeys.PassportUserName, string.Empty);
- LocalSetting.Set(SettingKeys.PassportPassword, string.Empty);
-
- await taskContext.SwitchToMainThreadAsync();
- authTokenExpiration = default;
- UserName = default;
- IsLoggedIn = false;
- IsDeveloper = false;
- IsMaintainer = false;
- IsHutaoCloudAllowed = false;
- CloudExpireAt = default;
- IsHutaoCdnAllowed = false;
- CdnExpireAt = default;
- }
- }
-
private readonly struct AuthTokenExpiration
{
public readonly string Token;
diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/HutaoPassportPage.xaml b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/HutaoPassportPage.xaml
index ee6fde01c0..9c1f87bf5d 100644
--- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/HutaoPassportPage.xaml
+++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/HutaoPassportPage.xaml
@@ -40,6 +40,11 @@
Content="{shuxm:ResourceString Name=ViewPageSettingHutaoPassportRegisterAction}"
Style="{ThemeResource SettingButtonStyle}"
Visibility="{Binding HutaoUserOptions.IsLoggedIn, Converter={StaticResource BoolToVisibilityRevertConverter}}"/>
+