This commit is contained in:
DismissedLight
2023-10-29 22:56:10 +08:00
parent a03edeef3f
commit ba4a7c1447
5 changed files with 42 additions and 29 deletions

View File

@@ -0,0 +1,8 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Message;
internal sealed class HutaoUserChangedMessage
{
}

View File

@@ -2,6 +2,7 @@
// Licensed under the MIT license.
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.Options;
using Snap.Hutao.Web.Hutao;
using System.Text.RegularExpressions;
@@ -61,17 +62,25 @@ internal sealed class HutaoUserOptions : ObservableObject, IOptions<HutaoUserOpt
/// <inheritdoc/>
public HutaoUserOptions Value { get => this; }
/// <summary>
/// 登录
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="token">令牌</param>
public void LoginSucceed(string userName, string? token)
public async ValueTask<bool> PostLoginSucceedAsync(HomaPassportClient passportClient, ITaskContext taskContext, string username, string? token)
{
UserName = userName;
await taskContext.SwitchToMainThreadAsync();
UserName = username;
this.token = token;
IsLoggedIn = true;
initializedTaskCompletionSource.TrySetResult();
await taskContext.SwitchToBackgroundAsync();
Web.Response.Response<UserInfo> userInfoResponse = await passportClient.GetUserInfoAsync(default).ConfigureAwait(false);
if (userInfoResponse.IsOk())
{
await taskContext.SwitchToMainThreadAsync();
UpdateUserInfo(userInfoResponse.Data);
Ioc.Default.GetRequiredService<IMessenger>().Send(new Message.HutaoUserChangedMessage());
return true;
}
return false;
}
public void LogoutOrUnregister()
@@ -109,6 +118,12 @@ internal sealed class HutaoUserOptions : ObservableObject, IOptions<HutaoUserOpt
IsCloudServiceAllowed = IsLicensedDeveloper || userInfo.GachaLogExpireAt > DateTimeOffset.Now;
}
public async ValueTask<string?> GetTokenAsync()
{
await initializedTaskCompletionSource.Task.ConfigureAwait(false);
return token;
}
private void ClearUserInfo()
{
IsLicensedDeveloper = false;
@@ -117,10 +132,4 @@ internal sealed class HutaoUserOptions : ObservableObject, IOptions<HutaoUserOpt
GachaLogExpireAtSlim = null;
IsCloudServiceAllowed = false;
}
public async ValueTask<string?> GetTokenAsync()
{
await initializedTaskCompletionSource.Task.ConfigureAwait(false);
return token;
}
}

View File

@@ -44,15 +44,8 @@ internal sealed partial class HutaoUserService : IHutaoUserService, IHutaoUserSe
if (response.IsOk())
{
await taskContext.SwitchToMainThreadAsync();
options.LoginSucceed(userName, response.Data);
await taskContext.SwitchToBackgroundAsync();
Web.Response.Response<UserInfo> userInfoResponse = await passportClient.GetUserInfoAsync(token).ConfigureAwait(false);
if (userInfoResponse.IsOk())
if (await options.PostLoginSucceedAsync(passportClient, taskContext, userName, response.Data).ConfigureAwait(false))
{
await taskContext.SwitchToMainThreadAsync();
options.UpdateUserInfo(userInfoResponse.Data);
isInitialized = true;
}
}

View File

@@ -1,9 +1,11 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml.Controls;
using Snap.Hutao.Control.Extension;
using Snap.Hutao.Factory.Abstraction;
using Snap.Hutao.Message;
using Snap.Hutao.Model.Entity;
using Snap.Hutao.Service.GachaLog;
using Snap.Hutao.Service.Hutao;
@@ -20,7 +22,7 @@ namespace Snap.Hutao.ViewModel.GachaLog;
/// </summary>
[ConstructorGenerated]
[Injection(InjectAs.Scoped)]
internal sealed partial class HutaoCloudViewModel : Abstraction.ViewModel
internal sealed partial class HutaoCloudViewModel : Abstraction.ViewModel, IRecipient<HutaoUserChangedMessage>
{
private readonly INavigationService navigationService;
private readonly IContentDialogFactory contentDialogFactory;
@@ -47,6 +49,11 @@ internal sealed partial class HutaoCloudViewModel : Abstraction.ViewModel
/// </summary>
internal ICommand RetrieveCommand { get; set; }
public void Receive(HutaoUserChangedMessage message)
{
RefreshUidCollectionAsync().SafeForget();
}
/// <summary>
/// 异步获取祈愿记录
/// </summary>

View File

@@ -58,9 +58,7 @@ internal sealed partial class HutaoPassportViewModel : Abstraction.ViewModel
{
SaveUserNameAndPassword(username, password);
infoBarService.Information(response.Message);
await taskContext.SwitchToMainThreadAsync();
hutaoUserOptions.LoginSucceed(username, response.Data);
await hutaoUserOptions.PostLoginSucceedAsync(homaPassportClient, taskContext, username, response.Data).ConfigureAwait(false);
}
}
}
@@ -114,8 +112,7 @@ internal sealed partial class HutaoPassportViewModel : Abstraction.ViewModel
SaveUserNameAndPassword(username, password);
infoBarService.Information(response.Message);
await taskContext.SwitchToMainThreadAsync();
hutaoUserOptions.LoginSucceed(username, response.Data);
await hutaoUserOptions.PostLoginSucceedAsync(homaPassportClient, taskContext, username, response.Data).ConfigureAwait(false);
}
}
}
@@ -148,8 +145,7 @@ internal sealed partial class HutaoPassportViewModel : Abstraction.ViewModel
SaveUserNameAndPassword(username, password);
infoBarService.Information(response.Message);
await taskContext.SwitchToMainThreadAsync();
hutaoUserOptions.LoginSucceed(username, response.Data);
await hutaoUserOptions.PostLoginSucceedAsync(homaPassportClient, taskContext, username, response.Data).ConfigureAwait(false);
}
}
}