Support website login for adding hoyoverse account

This commit is contained in:
Xhichn
2023-03-13 12:36:38 +08:00
parent b0b3553d0c
commit fdf2311f0c
5 changed files with 156 additions and 2 deletions

View File

@@ -49,9 +49,9 @@ internal interface IUserService
Task<ValueResult<UserOptionResult, string>> ProcessInputCookieAsync(Cookie cookie);
/// <summary>
/// 尝试异步处理输入的国际服 Cookie
/// 尝试异步处理国际服 Cookie
/// </summary>
/// <param name="cookie">Cookie</param>
/// <param name="cookie">来自网页 www.hoyolab.com 的 Cookie需包含 ltuid, ltoken 和 cookie_token 字段</param>
/// <returns>处理的结果</returns>
Task<ValueResult<UserOptionResult, string>> ProcessInputOsCookieAsync(Cookie cookie);

View File

@@ -0,0 +1,31 @@
<Page
x:Class="Snap.Hutao.View.Page.LoginHoyoverseUserPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:shcm="using:Snap.Hutao.Control.Markup"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Grid Loaded="OnRootLoaded">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock
Grid.Row="0"
Margin="12,0,0,0"
VerticalAlignment="Center"
Text="{shcm:ResourceString Name=ViewPageLoginMihoyoUserTitle}"/>
<Button
Margin="16"
HorizontalAlignment="Right"
Click="CookieButtonClick"
Content="{shcm:ResourceString Name=ViewPageLoginMihoyoUserLoggedInAction}"/>
<WebView2
x:Name="WebView"
Grid.Row="2"
Margin="0,0,0,0"/>
</Grid>
</Page>

View File

@@ -0,0 +1,98 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Microsoft.UI.Xaml;
using Microsoft.Web.WebView2.Core;
using Snap.Hutao.Service.Abstraction;
using Snap.Hutao.Service.Navigation;
using Snap.Hutao.Service.User;
using Snap.Hutao.Web.Hoyolab;
using Snap.Hutao.Web.Hoyolab.Passport;
using Snap.Hutao.Web.Hoyolab.Takumi.Auth;
using Snap.Hutao.Web.Response;
namespace Snap.Hutao.View.Page;
/// <summary>
/// <20><>¼<EFBFBD>׹<EFBFBD><D7B9><EFBFBD>ͨ<EFBFBD><CDA8>֤ҳ<D6A4><D2B3>
/// </summary>
[HighQuality]
internal sealed partial class LoginHoyoverseUserPage : Microsoft.UI.Xaml.Controls.Page
{
/// <summary>
/// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>µĵ<C2B5>¼<EFBFBD>׹<EFBFBD><D7B9><EFBFBD>ͨ<EFBFBD><CDA8>֤ҳ<D6A4><D2B3>
/// </summary>
public LoginHoyoverseUserPage()
{
InitializeComponent();
}
[SuppressMessage("", "VSTHRD100")]
private async void OnRootLoaded(object sender, RoutedEventArgs e)
{
try
{
await WebView.EnsureCoreWebView2Async();
CoreWebView2CookieManager manager = WebView.CoreWebView2.CookieManager;
IReadOnlyList<CoreWebView2Cookie> cookies = await manager.GetCookiesAsync("https://www.hoyolab.com");
foreach (CoreWebView2Cookie item in cookies)
{
manager.DeleteCookie(item);
}
WebView.CoreWebView2.Navigate("https://www.hoyolab.com");
}
catch (Exception ex)
{
Ioc.Default.GetRequiredService<IInfoBarService>().Error(ex);
}
}
private async Task HandleCurrentCookieAsync(CancellationToken token)
{
CoreWebView2CookieManager manager = WebView.CoreWebView2.CookieManager;
IReadOnlyList<CoreWebView2Cookie> cookies = await manager.GetCookiesAsync("https://www.hoyolab.com");
Cookie hoyoLabCookie = Cookie.FromCoreWebView2Cookies(cookies);
// <20><><EFBFBD><EFBFBD> cookie <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>
(UserOptionResult result, string nickname) = await Ioc.Default
.GetRequiredService<IUserService>()
.ProcessInputOsCookieAsync(hoyoLabCookie)
.ConfigureAwait(false);
Ioc.Default.GetRequiredService<INavigationService>().GoBack();
IInfoBarService infoBarService = Ioc.Default.GetRequiredService<IInfoBarService>();
switch (result)
{
case UserOptionResult.Added:
ViewModel.UserViewModel vm = Ioc.Default.GetRequiredService<ViewModel.UserViewModel>();
if (vm.Users!.Count == 1)
{
await ThreadHelper.SwitchToMainThreadAsync();
vm.SelectedUser = vm.Users.Single();
}
infoBarService.Success($"<22>û<EFBFBD> [{nickname}] <20><><EFBFBD>ӳɹ<D3B3>");
break;
case UserOptionResult.Incomplete:
infoBarService.Information($"<22><> Cookie <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>");
break;
case UserOptionResult.Invalid:
infoBarService.Information($"<22><> Cookie <20><>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>");
break;
case UserOptionResult.Updated:
infoBarService.Success($"<22>û<EFBFBD> [{nickname}] <20><><EFBFBD>³ɹ<C2B3>");
break;
default:
throw Must.NeverHappen();
}
}
private void CookieButtonClick(object sender, RoutedEventArgs e)
{
HandleCurrentCookieAsync(CancellationToken.None).SafeForget();
}
}

View File

@@ -233,6 +233,10 @@
Margin="0,0,6,0"
HorizontalAlignment="Right"
Orientation="Horizontal">
<AppBarButton
Command="{Binding LoginHoyoverseUserCommand}"
Icon="{shcm:FontIcon Glyph=&#xEB41;}"
Label="{shcm:ResourceString Name=ViewUserCookieOperationLoginMihoyoUserAction}"/>
<AppBarButton
Command="{Binding AddOsUserCommand}"
Icon="{shcm:FontIcon Glyph=&#xE710;}"

View File

@@ -48,6 +48,7 @@ internal sealed class UserViewModel : ObservableObject
AddUserCommand = new AsyncRelayCommand(AddUserAsync);
AddOsUserCommand = new AsyncRelayCommand(AddOsUserAsync);
LoginMihoyoUserCommand = new RelayCommand(LoginMihoyoUser);
LoginHoyoverseUserCommand = new RelayCommand(LoginHoyoverseUser);
RemoveUserCommand = new AsyncRelayCommand<User>(RemoveUserAsync);
CopyCookieCommand = new RelayCommand<User>(CopyCookie);
RefreshCookieTokenCommand = new AsyncRelayCommand(RefreshCookieTokenAsync);
@@ -98,6 +99,11 @@ internal sealed class UserViewModel : ObservableObject
/// </summary>
public ICommand LoginMihoyoUserCommand { get; }
/// <summary>
/// 登录米游社命令
/// </summary>
public ICommand LoginHoyoverseUserCommand { get; }
/// <summary>
/// 移除用户命令
/// </summary>
@@ -220,6 +226,21 @@ internal sealed class UserViewModel : ObservableObject
}
}
/// <summary>
/// 打开浏览器登录 hoyolab 以获取 cookie
/// </summary>
private void LoginHoyoverseUser()
{
if (Core.WebView2Helper.IsSupported)
{
serviceProvider.GetRequiredService<INavigationService>().Navigate<LoginHoyoverseUserPage>(INavigationAwaiter.Default);
}
else
{
infoBarService.Warning(SH.CoreWebView2HelperVersionUndetected);
}
}
private async Task RemoveUserAsync(User? user)
{
if (user != null)