Skip to content

Commit

Permalink
适配器登录完成
Browse files Browse the repository at this point in the history
  • Loading branch information
Miaoyww committed Jul 24, 2024
1 parent aa39649 commit 0a7778c
Show file tree
Hide file tree
Showing 30 changed files with 618 additions and 240 deletions.
12 changes: 4 additions & 8 deletions NonsPlayer.Core/Contracts/Adapters/IAccountAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ public interface IAccountAdapter : ISubAdapter
List<IPlaylist> CreatedPlaylists { get; set; }
List<IPlaylist> SavedPlaylists { get; set; }

string Key { get; set; }
Task<Uri> GetQrCodeUrlAsync();
Task<Tuple<Uri, string>> GetQrCodeUrlAsync();
Task<QrCodeResult> CheckLoginAsync(string key);

Task<IAccount> GetAccountAsync(string token);

IAccount GetAccount();
Task<string> GetTokenAsync(string response);

Task GetUserPlaylists();
Task GetFavoritePlaylist();
Task<bool> GetUserPlaylists();
Task<bool> GetFavoritePlaylist();
}

public class QrCodeResult
Expand Down
12 changes: 10 additions & 2 deletions NonsPlayer.Core/Contracts/Models/Nons/IAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ namespace NonsPlayer.Core.Contracts.Models.Nons;

public interface IAccount : INonsModel
{
string NickName { get; set; }
string Name { get; set; }

Check warning on line 7 in NonsPlayer.Core/Contracts/Models/Nons/IAccount.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

'IAccount.Name' hides inherited member 'INonsModel.Name'. Use the new keyword if hiding was intended.

Check warning on line 7 in NonsPlayer.Core/Contracts/Models/Nons/IAccount.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

'IAccount.Name' hides inherited member 'INonsModel.Name'. Use the new keyword if hiding was intended.

Check warning on line 7 in NonsPlayer.Core/Contracts/Models/Nons/IAccount.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

'IAccount.Name' hides inherited member 'INonsModel.Name'. Use the new keyword if hiding was intended.

Check warning on line 7 in NonsPlayer.Core/Contracts/Models/Nons/IAccount.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

'IAccount.Name' hides inherited member 'INonsModel.Name'. Use the new keyword if hiding was intended.
string Token { get; set; }
string FaceUrl { get; set; }
string AvatarUrl { get; set; }
bool IsLoggedIn { get; set; }
string CacheAvatarId => Id + "_avatar";
string CacheSmallAvatarId => CacheAvatarId + "_small";
string CacheMiddleAvatarId => CacheAvatarId + "_middle";
string Key { get; set; }

Task<bool> LoginByToken(string token);
Task<string> GetAvatarUrl();
Task<bool> Refresh();
}
32 changes: 13 additions & 19 deletions NonsPlayer.Core/Resources/LocalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,29 @@ namespace NonsPlayer.Core.Resources
{
public class LocalSettings
{
[JsonIgnore]
public string DataPath { get; set; }
[JsonIgnore] public string DataPath { get; set; }

[JsonIgnore]
public string ConfigFilePath;
[JsonIgnore] public string ConfigFilePath;

[JsonPropertyName("adapter_path")]
public string AdapterPath { get; set; }
[JsonPropertyName("adapter_path")] public string AdapterPath { get; set; }

[JsonPropertyName("plugin_path")]
public string PluginPath { get; set; }
[JsonPropertyName("plugin_path")] public string PluginPath { get; set; }

[JsonPropertyName("data_path")]
public string Data { get; set; }
[JsonPropertyName("data_path")] public string Data { get; set; }

[JsonPropertyName("theme")]
public string Theme { get; set; }
[JsonPropertyName("theme")] public string Theme { get; set; }

[JsonPropertyName("volume")]
public double Volume { get; set; }
[JsonPropertyName("volume")] public double Volume { get; set; }

[JsonPropertyName("default_adapter")]
public string DefaultAdapter { get; set; }
[JsonPropertyName("adapter_account_tokens")]
public Dictionary<string, string> AdapterAccountTokens { get; set; }

[JsonPropertyName("default_adapter")] public string DefaultAdapter { get; set; }

[JsonPropertyName("disabled_adapters")]
public string DisabledAdapters { get; set; }

[JsonPropertyName("disabled_plugins")]
public string DisabledPlugins { get; set; }
[JsonPropertyName("disabled_plugins")] public string DisabledPlugins { get; set; }

public LocalSettings()
{
Expand All @@ -49,10 +43,10 @@ public LocalSettings()
Data = DataPath + "/Data";
Theme = "Light";
Volume = 50;
AdapterAccountTokens = new Dictionary<string, string>();
DefaultAdapter = string.Empty;
DisabledAdapters = string.Empty;
DisabledPlugins = string.Empty;
}

}
}
1 change: 1 addition & 0 deletions NonsPlayer/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ResourceDictionary Source="/Styles/Resource.xaml" />
<ResourceDictionary Source="/Styles/Thickness.xaml" />
<ResourceDictionary Source="/Styles/TextBlock.xaml" />
<ResourceDictionary Source="/Styles/FlipView.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Expand Down
10 changes: 10 additions & 0 deletions NonsPlayer/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public App()
services.AddTransient<ShellViewModel>();
services.AddTransient<PersonalCenterPage>();
services.AddTransient<PersonalCenterViewModel>();
services.AddTransient<PersonalLibaryPage>();
services.AddTransient<PersonalLibaryViewModel>();
services.AddTransient<LoginViewModel>();
services.AddTransient<LoginPage>();
services.AddTransient<SearchViewModel>();
Expand Down Expand Up @@ -131,6 +133,14 @@ public App()
ConfigManager.Instance.LoadConfig();
AdapterService.Instance.AdapterLoadFailed += OnAdapterLoadFailed;
AdapterService.Instance.Init();
foreach (var key in ConfigManager.Instance.Settings.AdapterAccountTokens.Keys)
{
var adapter = AdapterService.Instance.GetAdapter(key);
if (adapter != null)
{
adapter.Account.GetAccount().LoginByToken(ConfigManager.Instance.Settings.AdapterAccountTokens[key]);
}
}
UnhandledException += App_UnhandledException;
GetService<PlayCounterService>().Init();
GetService<SMTCService>().Init();
Expand Down
14 changes: 14 additions & 0 deletions NonsPlayer/Components/ViewModels/LoginCardViewModel.cs
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
{
}
30 changes: 30 additions & 0 deletions NonsPlayer/Components/Views/Login/LoginQrCard.xaml
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>
128 changes: 128 additions & 0 deletions NonsPlayer/Components/Views/Login/LoginQrCard.xaml.cs
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();
}
}
27 changes: 27 additions & 0 deletions NonsPlayer/Components/Views/Login/LoginTokenCard.xaml
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>
30 changes: 30 additions & 0 deletions NonsPlayer/Components/Views/Login/LoginTokenCard.xaml.cs
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;
}
}
Loading

0 comments on commit 0a7778c

Please sign in to comment.