-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
618 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
using Microsoft.UI.Xaml; | ||
using NonsPlayer.Core.Contracts.Adapters; | ||
using NonsPlayer.Core.Nons; | ||
using NonsPlayer.Core.Services; | ||
using NonsPlayer.Helpers; | ||
|
||
namespace NonsPlayer.Components.ViewModels; | ||
|
||
[INotifyPropertyChanged] | ||
public partial class LoginCardViewModel | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<UserControl | ||
x:Class="NonsPlayer.Components.Views.LoginQrCard" | ||
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:winUi="using:CommunityToolkit.WinUI" | ||
mc:Ignorable="d" | ||
Unloaded="LoginQrCard_OnUnloaded"> | ||
<Border Name="Login" | ||
CornerRadius="{StaticResource CustomCornerRadius}" | ||
Height="350" Width="350"> | ||
<Grid Grid.Row="1" Name="QrCodeCard"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="Auto" /> | ||
</Grid.RowDefinitions> | ||
<Border Grid.Row="0" Name="QrCard" | ||
Height="250" Width="250" Margin="0,30,0,0" Padding="0" | ||
CornerRadius="{StaticResource CustomCornerRadius}" | ||
Background="{x:Bind QrCode, Mode=OneWay}" /> | ||
<TextBlock Grid.Row="1" Name="QrState" | ||
HorizontalAlignment="Center" | ||
Margin="0,10,0,0" | ||
Text="{x:Bind QrCodeState, Mode=OneWay}" | ||
FontSize="20" FontWeight="Bold" | ||
Style="{StaticResource CommonTextStyle}" /> | ||
</Grid> | ||
</Border> | ||
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using System.Drawing.Imaging; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
using Microsoft.UI.Xaml.Media; | ||
using Microsoft.UI.Xaml.Media.Imaging; | ||
using NonsPlayer.Components.ViewModels; | ||
using NonsPlayer.Core.Contracts.Adapters; | ||
using NonsPlayer.Core.Contracts.Models.Nons; | ||
using NonsPlayer.Core.Services; | ||
using NonsPlayer.Helpers; | ||
using QRCoder; | ||
|
||
namespace NonsPlayer.Components.Views; | ||
|
||
[INotifyPropertyChanged] | ||
public sealed partial class LoginQrCard : UserControl | ||
{ | ||
[ObservableProperty] private ImageBrush qrCode; | ||
[ObservableProperty] private string qrCodeState; | ||
private IAccount account; | ||
private IAdapter adapter; | ||
private CancellationTokenSource tokenSource; | ||
private CancellationToken cancellationToken; | ||
public LoginQrCard() | ||
{ | ||
ViewModel = App.GetService<LoginCardViewModel>(); | ||
InitializeComponent(); | ||
QrCodeState = "QrCodeState_Waiting".GetLocalized(); | ||
tokenSource = new(); | ||
cancellationToken = tokenSource.Token; | ||
} | ||
public LoginCardViewModel ViewModel { get; } | ||
|
||
public IAdapter Adapter | ||
{ | ||
set | ||
{ | ||
adapter = value; | ||
account = adapter.Account.GetAccount(); | ||
Init(); | ||
} | ||
} | ||
|
||
private async void Init() | ||
{ | ||
if (account != null) | ||
{ | ||
if (account.IsLoggedIn) | ||
{ | ||
await RefreshAccountInfo().ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
await GetQrCode(); | ||
await CheckKey().ConfigureAwait(false); | ||
} | ||
} | ||
|
||
} | ||
|
||
private async Task GetQrCode() | ||
{ | ||
var data = await adapter.Account.GetQrCodeUrlAsync(); | ||
if (data.Item1 == null) return; | ||
account.Key = data.Item2; | ||
var qrCodeImage = new QRCode(new QRCodeGenerator().CreateQrCode( | ||
new PayloadGenerator.Url(data.Item1.ToString()).ToString(), | ||
QRCodeGenerator.ECCLevel.M)).GetGraphic(10); | ||
var ms = new MemoryStream(); | ||
qrCodeImage.Save(ms, ImageFormat.Png); | ||
ms.Seek(0, SeekOrigin.Begin); | ||
var result = new BitmapImage(); | ||
result.SetSource(ms.AsRandomAccessStream()); | ||
|
||
DispatcherQueue.TryEnqueue(() => | ||
{ | ||
QrCode = new ImageBrush | ||
{ | ||
ImageSource = result | ||
}; | ||
QrCodeState = "QrCodeState_Waiting".GetLocalized(); | ||
}); | ||
|
||
} | ||
|
||
private async Task CheckKey() | ||
{ | ||
// 重复检查二维码状态 | ||
await Task.Run(async () => | ||
{ | ||
while (!cancellationToken.IsCancellationRequested) | ||
{ | ||
var result = await adapter.Account.CheckLoginAsync(account.Key); | ||
if (result.Status == QrCodeStatus.Timeout) | ||
{ | ||
await GetQrCode(); | ||
} | ||
else if (result.Status == QrCodeStatus.Scanned) | ||
{ | ||
DispatcherQueue.TryEnqueue(() => { QrCodeState = "QrCodeState_Scanned".GetLocalized(); }); | ||
} | ||
else if (result.Status == QrCodeStatus.Confirmed) | ||
{ | ||
// 二维码登录成功 | ||
DispatcherQueue.TryEnqueue(() => { QrCodeState = "QrCodeState_Done".GetLocalized(); }); | ||
account = result.Account; | ||
ConfigManager.Instance.Settings.AdapterAccountTokens.Add(adapter.GetMetadata().Name, account.Token); | ||
ConfigManager.Instance.SaveConfig(); | ||
break; | ||
} | ||
await Task.Delay(1000); | ||
} | ||
}); | ||
} | ||
|
||
private async Task RefreshAccountInfo() | ||
{ | ||
|
||
} | ||
|
||
private void LoginQrCard_OnUnloaded(object sender, RoutedEventArgs e) | ||
{ | ||
tokenSource.Cancel(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<UserControl | ||
x:Class="NonsPlayer.Components.Views.LoginTokenCard" | ||
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:winUi="using:CommunityToolkit.WinUI" | ||
mc:Ignorable="d"> | ||
<Border Name="Login" | ||
Height="350" Width="350" CornerRadius="{StaticResource CustomCornerRadius}" | ||
VerticalAlignment="Center" HorizontalAlignment="Center" | ||
Padding="0,40,0,0"> | ||
<Grid Name="TokenCard"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="*" /> | ||
</Grid.RowDefinitions> | ||
<TextBlock Grid.Row="0" x:Uid="LoginTokenTip" | ||
Style="{StaticResource CommonTextStyle}" | ||
FontSize="20" FontWeight="Bold" | ||
Margin="0,20,0,0" | ||
HorizontalAlignment="Center" /> | ||
<RichEditBox Grid.Row="1" x:Name="TokenTextBox" Margin="40" /> | ||
</Grid> | ||
|
||
</Border> | ||
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
using Microsoft.UI.Xaml.Media; | ||
using Microsoft.UI.Xaml.Media.Imaging; | ||
using NonsPlayer.Components.ViewModels; | ||
using NonsPlayer.Core.Contracts.Adapters; | ||
using NonsPlayer.Core.Contracts.Models.Nons; | ||
using NonsPlayer.Helpers; | ||
|
||
namespace NonsPlayer.Components.Views; | ||
|
||
[INotifyPropertyChanged] | ||
public sealed partial class LoginTokenCard : UserControl | ||
{ | ||
private IAccount account; | ||
private IAdapter adapter; | ||
public LoginTokenCard() | ||
{ | ||
ViewModel = App.GetService<LoginCardViewModel>(); | ||
InitializeComponent(); | ||
} | ||
public LoginCardViewModel ViewModel { get; } | ||
|
||
public IAdapter Adapter | ||
{ | ||
set => adapter = value; | ||
} | ||
} |
Oops, something went wrong.