Skip to content

Commit

Permalink
support sign in bbs
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Nov 27, 2022
1 parent 8803704 commit 8f273e6
Show file tree
Hide file tree
Showing 35 changed files with 763 additions and 613 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ protected override void OnDetaching()

base.OnDetaching();
}
}
}
5 changes: 5 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Core/CoreEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ internal static class CoreEnvironment
/// </summary>
public const string HoyolabUA = $"Mozilla/5.0 (Windows NT 10.0; Win64; x64) miHoYoBBS/{HoyolabXrpcVersion}";

/// <summary>
/// 米游社移动端请求UA
/// </summary>
public const string HoyolabMobileUA = $"Mozilla/5.0 (Linux; Android 12) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/106.0.5249.126 Mobile Safari/537.36 miHoYoBBS/{HoyolabXrpcVersion}";

/// <summary>
/// 米游社 Rpc 版本
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Core/TaskSchedulerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using Microsoft.Win32.TaskScheduler;
using System.Runtime.InteropServices;
using SchedulerTask = Microsoft.Win32.TaskScheduler.Task;

namespace Snap.Hutao.Core;
Expand Down Expand Up @@ -39,5 +40,9 @@ public static bool RegisterForDailyNoteRefresh(int interval)
{
return false;
}
catch (COMException)
{
return false;
}
}
}
13 changes: 0 additions & 13 deletions src/Snap.Hutao/Snap.Hutao/Core/Validation/Must.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,6 @@ public static void Range([DoesNotReturnIf(false)] bool condition, string? messag
}
}

/// <summary>
/// 任务异常
/// </summary>
/// <typeparam name="T">任务结果类型</typeparam>
/// <param name="message">异常消息</param>
/// <returns>异常的任务</returns>
[SuppressMessage("", "VSTHRD200")]
public static Task<T> Fault<T>(string message)
{
InvalidOperationException exception = new(message);
return Task.FromException<T>(exception);
}

/// <summary>
/// Unconditionally throws an <see cref="NotSupportedException"/>.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Snap.Hutao/Snap.Hutao/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Identity
Name="7f0db578-026f-4e0b-a75b-d5d06bb0a74d"
Publisher="CN=DGP Studio"
Version="1.2.4.0" />
Version="1.2.5.0" />

<Properties>
<DisplayName>胡桃</DisplayName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public async ValueTask RefreshDailyNotesAsync(bool notify)

// cache
await ThreadHelper.SwitchToMainThreadAsync();
entries?.Single(e => e.UserId == entry.UserId && e.Uid == entry.Uid).UpdateDailyNote(dailyNote);
entries?.SingleOrDefault(e => e.UserId == entry.UserId && e.Uid == entry.Uid)?.UpdateDailyNote(dailyNote);

if (notify)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ internal interface IGameLocator : INamed
/// 路径应当包含启动器文件名称
/// </summary>
/// <returns>游戏启动器位置</returns>
[Obsolete("不应定位启动器位置")]
Task<ValueResult<bool, string>> LocateLauncherPathAsync();
}
2 changes: 2 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Service/InfoBarService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.Xaml.Interactivity;
using Snap.Hutao.Control.Behavior;
using Snap.Hutao.Service.Abstraction;

namespace Snap.Hutao.Service;
Expand Down
12 changes: 8 additions & 4 deletions src/Snap.Hutao/Snap.Hutao/View/Dialog/SignInWebViewDialog.xaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<ContentDialog
<ContentDialog
x:Class="Snap.Hutao.View.Dialog.SignInWebViewDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Style="{StaticResource DefaultContentDialogStyle}">
Closed="OnContentDialogClosed"
Style="{StaticResource DefaultContentDialogStyle}"
Title="米游社每日签到"
PrimaryButtonText="完成"
DefaultButton="Primary">

<Grid Loaded="OnGridLoaded" Height="600" Width="380">
<WebView2 Name="WebView"/>
<Grid Loaded="OnGridLoaded">
<WebView2 Name="WebView" Height="456" Width="380"/>
</Grid>
</ContentDialog>
49 changes: 23 additions & 26 deletions src/Snap.Hutao/Snap.Hutao/View/Dialog/SignInWebViewDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Web.WebView2.Core;
using Snap.Hutao.Model.Binding.User;
using Snap.Hutao.Service.Abstraction;
using Snap.Hutao.Service.User;
using Snap.Hutao.Web.Bridge;
using Snap.Hutao.Web.Bridge.Model;
using Snap.Hutao.Web.Bridge.Model.Event;
using Snap.Hutao.Web.Hoyolab.DynamicSecret;
using Windows.UI.Popups;

namespace Snap.Hutao.View.Dialog;

Expand All @@ -19,6 +15,10 @@ namespace Snap.Hutao.View.Dialog;
/// </summary>
public sealed partial class SignInWebViewDialog : ContentDialog
{
private readonly IServiceScope scope;
[SuppressMessage("", "IDE0052")]
private SignInJsInterface? signInJsInterface;

/// <summary>
/// 构造一个新的签到网页视图对话框
/// </summary>
Expand All @@ -27,6 +27,7 @@ public SignInWebViewDialog(MainWindow window)
{
InitializeComponent();
XamlRoot = window.Content.XamlRoot;
scope = Ioc.Default.CreateScope();
}

private void OnGridLoaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
Expand All @@ -38,31 +39,27 @@ private async Task InitializeAsync()
{
await WebView.EnsureCoreWebView2Async();
CoreWebView2 coreWebView2 = WebView.CoreWebView2;
IUserService userService = Ioc.Default.GetRequiredService<IUserService>();
IInfoBarService infoBarService = Ioc.Default.GetRequiredService<IInfoBarService>();
ILogger<MiHoYoJsBridge> logger = Ioc.Default.GetRequiredService<ILogger<MiHoYoJsBridge>>();
IUserService userService = scope.ServiceProvider.GetRequiredService<IUserService>();

User? user = userService.Current;

coreWebView2.SetCookie(user?.CookieToken, user?.Ltoken);
coreWebView2.SetMobileUserAgent();
coreWebView2.InitializeBridge(logger, false)
.Register<JsEventClosePage>(e => Hide())
.Register<JsEventRealPersonValidation>(e => infoBarService.Information("无法使用此功能", "请前往米游社进行实名认证后重试"))
.Register<JsEventGetStatusBarHeight>(s => s.Callback(result => result.Data["statusBarHeight"] = 0))
.Register<JsEventGetDynamicSecretV1>(s => s.Callback(result =>
{
result.Data["DS"] = DynamicSecretHandler.GetDynamicSecret(nameof(SaltType.K2), nameof(DynamicSecretVersion.Gen1), includeChars: true);
}))
.Register<JsEventGetUserInfo>(s => s.Callback(result =>
{
result.Data["id"] = "111";
result.Data["gender"] = 0;
result.Data["nickname"] = "222";
result.Data["introduce"] = "333";
result.Data["avatar_url"] = "https://img-static.mihoyo.com/communityweb/upload/52de23f1b1a060e4ccaa8b24c1305dd9.png";
}));
if (user == null)
{
return;
}

coreWebView2.SetCookie(user.CookieToken, user.Ltoken, null).SetMobileUserAgent();
signInJsInterface = new(coreWebView2, scope.ServiceProvider);

#if DEBUG
coreWebView2.OpenDevToolsWindow();
#endif
coreWebView2.Navigate("https://webstatic.mihoyo.com/bbs/event/signin-ys/index.html?act_id=e202009291139501");
}

private void OnContentDialogClosed(ContentDialog sender, ContentDialogClosedEventArgs args)
{
signInJsInterface = null;
scope.Dispose();
}
}
45 changes: 39 additions & 6 deletions src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
Icon="&#xECAA;"
Header="胡桃"
Description="{Binding AppVersion}"/>
<sc:Setting
Icon="&#xE975;"
Header="设备ID">
<sc:Setting.Description>
<TextBlock
IsTextSelectionEnabled="True"
Text="{Binding DeviceId}"
Style="{StaticResource CaptionTextBlockStyle}"/>
</sc:Setting.Description>
</sc:Setting>
<sc:Setting
Icon="&#xED15;"
Header="反馈"
Expand Down Expand Up @@ -159,14 +169,23 @@
</sc:Setting>

<sc:Setting
Icon="&#xE756;"
Header="删除所有用户"
Description="直接删除用户表的所有记录,用于修复特定的账号冲突问题"
Background="#80800000">
Icon="&#xE9D5;"
Header="米游社每日签到">
<sc:Setting.Description>
<StackPanel>
<TextBlock
Style="{StaticResource CaptionTextBlockStyle}"
Text="对当前选中的账号进行签到"/>
<TextBlock
Style="{StaticResource CaptionTextBlockStyle}"
Foreground="Yellow"
Text="可能需要重启应用以对其他账号签到"/>
</StackPanel>
</sc:Setting.Description>
<sc:Setting.ActionContent>
<Button
Content="执行"
Command="{Binding Experimental.DeleteUsersCommand}"/>
Content="打开签到对话框"
Command="{Binding ShowSignInWebViewDialogCommand}"/>
</sc:Setting.ActionContent>
</sc:Setting>

Expand All @@ -179,6 +198,20 @@
</sc:Setting.ActionContent>
</sc:Setting>
</sc:SettingsGroup>

<sc:SettingsGroup Header="危险功能" Foreground="#FF800000">
<sc:Setting
Icon="&#xE756;"
Header="删除所有用户"
Description="直接删除用户表的所有记录,用于修复特定的账号冲突问题"
Background="#80800000">
<sc:Setting.ActionContent>
<Button
Content="执行"
Command="{Binding Experimental.DeleteUsersCommand}"/>
</sc:Setting.ActionContent>
</sc:Setting>
</sc:SettingsGroup>
</StackPanel>
</Grid>
</ScrollViewer>
Expand Down
16 changes: 1 addition & 15 deletions src/Snap.Hutao/Snap.Hutao/View/UserView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,12 @@
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<TextBlock
Margin="10,6,0,6"
Style="{StaticResource BaseTextBlockStyle}"
Text="Webview"/>
<CommandBar DefaultLabelPosition="Right">
<AppBarButton Label="米游社签到" Icon="{shcm:FontIcon Glyph=&#xEB41;}" Command="{Binding ShowSignInWebViewDialogCommand}"/>
</CommandBar>
<TextBlock
Margin="10,6,0,6"
Style="{StaticResource BaseTextBlockStyle}"
Text="Cookie 操作"/>
<CommandBar DefaultLabelPosition="Right">
<AppBarButton Label="登录米哈游通行证" Icon="{shcm:FontIcon Glyph=&#xEB41;}" Command="{Binding LoginMihoyoUserCommand}">
<!--<AppBarButton.Flyout>
<MenuFlyout>
<MenuFlyoutItem Icon="{shcm:FontIcon Glyph=&#xF4A5;}" Text="网页米游社" Command="{Binding LoginMihoyoBBSCommand}"/>
<MenuFlyoutItem Icon="{shcm:FontIcon Glyph=&#xF4A5;}" Text="米游社" Command="{Binding LoginMihoyoBBS2Command}"/>
</MenuFlyout>
</AppBarButton.Flyout>-->
</AppBarButton>
<AppBarButton Label="登录米哈游通行证" Icon="{shcm:FontIcon Glyph=&#xEB41;}" Command="{Binding LoginMihoyoUserCommand}"/>
<AppBarButton
Icon="{shcm:FontIcon Glyph=&#xE710;}"
Label="手动输入"
Expand Down
21 changes: 21 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/ViewModel/SettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public SettingViewModel(
SetGamePathCommand = asyncRelayCommandFactory.Create(SetGamePathAsync);
DebugExceptionCommand = asyncRelayCommandFactory.Create(DebugThrowExceptionAsync);
DeleteGameWebCacheCommand = new RelayCommand(DeleteGameWebCache);
ShowSignInWebViewDialogCommand = asyncRelayCommandFactory.Create(ShowSignInWebViewDialogAsync);
}

/// <summary>
Expand All @@ -92,6 +93,15 @@ public string AppVersion
get => Core.CoreEnvironment.Version.ToString();
}

/// <summary>
/// 设备Id
/// </summary>
[SuppressMessage("", "CA1822")]
public string DeviceId
{
get => Core.CoreEnvironment.HutaoDeviceId;
}

/// <summary>
/// 空的历史卡池是否可见
/// </summary>
Expand Down Expand Up @@ -161,6 +171,11 @@ public NamedValue<BackdropType> SelectedBackdropType
/// </summary>
public ICommand DeleteGameWebCacheCommand { get; }

/// <summary>
/// 签到对话框命令
/// </summary>
public ICommand ShowSignInWebViewDialogCommand { get; }

private async Task SetGamePathAsync()
{
IGameLocator locator = Ioc.Default.GetRequiredService<IEnumerable<IGameLocator>>()
Expand Down Expand Up @@ -188,6 +203,12 @@ private void DeleteGameWebCache()
}
}

private async Task ShowSignInWebViewDialogAsync()
{
MainWindow mainWindow = Ioc.Default.GetRequiredService<MainWindow>();
await new SignInWebViewDialog(mainWindow).ShowAsync();
}

private async Task DebugThrowExceptionAsync()
{
#if DEBUG
Expand Down
9 changes: 0 additions & 9 deletions src/Snap.Hutao/Snap.Hutao/ViewModel/UserViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public UserViewModel(IUserService userService, IInfoBarService infoBarService, I
LoginMihoyoUserCommand = new RelayCommand(LoginMihoyoUser);
RemoveUserCommand = asyncRelayCommandFactory.Create<User>(RemoveUserAsync);
CopyCookieCommand = new RelayCommand<User>(CopyCookie);
ShowSignInWebViewDialogCommand = asyncRelayCommandFactory.Create(ShowSignInWebViewDialogAsync);
}

/// <summary>
Expand Down Expand Up @@ -84,8 +83,6 @@ public User? SelectedUser
/// </summary>
public ICommand LoginMihoyoUserCommand { get; }

public ICommand ShowSignInWebViewDialogCommand { get; }

/// <summary>
/// 移除用户命令
/// </summary>
Expand Down Expand Up @@ -168,10 +165,4 @@ private void CopyCookie(User? user)
infoBarService.Error(e);
}
}

private async Task ShowSignInWebViewDialogAsync()
{
MainWindow mainWindow = Ioc.Default.GetRequiredService<MainWindow>();
await new SignInWebViewDialog(mainWindow).ShowAsync();
}
}
Loading

0 comments on commit 8f273e6

Please sign in to comment.