提交 dbc11b97 编写于 作者: A Anran Zhang

添加一些基本的账户登录流程

上级 aee8161d
using System.Reflection;
using System.Runtime.CompilerServices;
// Copyright (c) Richasy. All rights reserved.
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Lib.Interfaces")]
......@@ -14,16 +15,6 @@ using System.Runtime.InteropServices;
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: ComVisible(false)]
\ No newline at end of file
[assembly: ComVisible(false)]
<UserControl
x:Class="Richasy.Bili.Lib.Uwp.AccountManagementPane"
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:local="using:Richasy.Bili.Lib.Uwp"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="400"
mc:Ignorable="d">
<Grid
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
BorderThickness="1"
CornerRadius="{StaticResource OverlayCornerRadius}" />
</UserControl>
// Copyright (c) Richasy. All rights reserved.
using Windows.UI.Xaml.Controls;
namespace Richasy.Bili.Lib.Uwp
{
/// <summary>
/// 账户登录管理面板.
/// </summary>
public sealed partial class AccountManagementPane : UserControl
{
/// <summary>
/// Initializes a new instance of the <see cref="AccountManagementPane"/> class.
/// </summary>
public AccountManagementPane()
{
this.InitializeComponent();
}
}
}
// Copyright (c) Richasy. All rights reserved.
using System.Threading.Tasks;
using Richasy.Bili.Models.Enums;
using Richasy.Bili.Toolkit.Interfaces;
......@@ -10,8 +11,16 @@ namespace Richasy.Bili.Lib.Uwp
/// </summary>
public partial class AuthorizeProvider
{
private AuthorizeState _state;
private readonly IMD5Toolkit _md5Toolkit;
private readonly ISettingsToolkit _settingsToolkit;
private AuthorizeState _state;
private string _accessToken;
private async Task<string> ShowAccountManagementPaneAndGetResultAsync()
{
var webAccountProviderTaskCompletionSource = new TaskCompletionSource<string>();
return await webAccountProviderTaskCompletionSource.Task;
}
}
}
......@@ -5,10 +5,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Richasy.Bili.Lib.Interfaces;
using Richasy.Bili.Locator.Uwp;
using Richasy.Bili.Models.App.Args;
using Richasy.Bili.Models.App.Constants;
using Richasy.Bili.Models.Enums;
using Richasy.Bili.Toolkit.Interfaces;
using Windows.Networking.Connectivity;
namespace Richasy.Bili.Lib.Uwp
{
......@@ -20,10 +21,10 @@ namespace Richasy.Bili.Lib.Uwp
/// <summary>
/// Initializes a new instance of the <see cref="AuthorizeProvider"/> class.
/// </summary>
/// <param name="md5Toolkit">MD5工具.</param>
public AuthorizeProvider(IMD5Toolkit md5Toolkit)
public AuthorizeProvider()
{
_md5Toolkit = md5Toolkit;
ServiceLocator.Instance.LoadService(out _md5Toolkit)
.LoadService(out _settingsToolkit);
}
/// <inheritdoc/>
......@@ -83,15 +84,85 @@ namespace Richasy.Bili.Lib.Uwp
}
/// <inheritdoc/>
public Task<string> GetTokenAsync(bool silentOnly = false) => throw new NotImplementedException();
public async Task<string> GetTokenAsync(bool silentOnly = false)
{
// TODO: 检查当前Token的时效.
var internetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (internetConnectionProfile == null)
{
// 目前不在线.
return null;
}
try
{
var result = await ShowAccountManagementPaneAndGetResultAsync();
_accessToken = result;
return result;
}
catch (Exception)
{
}
await SignOutAsync();
return null;
}
/// <inheritdoc/>
public Task SignInAsync() => throw new NotImplementedException();
public async Task SignInAsync()
{
if (!string.IsNullOrEmpty(_accessToken) || State != AuthorizeState.SignedOut)
{
return;
}
State = AuthorizeState.Loading;
var token = await GetTokenAsync();
if (string.IsNullOrEmpty(token))
{
await SignOutAsync();
}
}
/// <inheritdoc/>
public Task SignOutAsync() => throw new NotImplementedException();
public Task SignOutAsync()
{
State = AuthorizeState.Loading;
_settingsToolkit.DeleteLocalSetting(SettingNames.BiliUserId);
_settingsToolkit.DeleteLocalSetting(SettingNames.AuthorizeResult);
if (!string.IsNullOrEmpty(_accessToken))
{
_accessToken = null;
}
State = AuthorizeState.SignedOut;
return Task.CompletedTask;
}
/// <inheritdoc/>
public Task<bool> TrySilentSignInAsync() => throw new NotImplementedException();
public async Task<bool> TrySilentSignInAsync()
{
if (!string.IsNullOrEmpty(_accessToken) && State == AuthorizeState.SignedIn)
{
return true;
}
State = AuthorizeState.Loading;
var token = await GetTokenAsync(true);
if (token == null)
{
State = AuthorizeState.SignedOut;
return false;
}
return true;
}
}
}
......@@ -127,7 +127,7 @@ namespace Richasy.Bili.Lib.Uwp
{
if (response != null && response.StatusCode == HttpStatusCode.NotFound)
{
errorResponse = new ServerResponse { Message = ServiceConstants.Codes.ItemNotFound };
errorResponse = new ServerResponse { Message = ServiceConstants.Messages.NotFound };
}
else
{
......
......@@ -9,8 +9,11 @@
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</PropertyGroup>
<ItemGroup>
<Compile Include="AuthorizeProvider.cs" />
<Compile Include="AuthorizeProvider.Extension.cs" />
<Compile Include="AuthorizeProvider\AccountManagementPane.xaml.cs">
<DependentUpon>AccountManagementPane.xaml</DependentUpon>
</Compile>
<Compile Include="AuthorizeProvider\AuthorizeProvider.cs" />
<Compile Include="AuthorizeProvider\AuthorizeProvider.Extension.cs" />
<Compile Include="HttpProvider\HttpProvider.cs" />
<Compile Include="HttpProvider\HttpProvider.Extension.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
......@@ -23,6 +26,9 @@
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.12</Version>
</PackageReference>
<PackageReference Include="Microsoft.UI.Xaml">
<Version>2.6.0-prerelease.210524002</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Models\Models.App\Models.App.csproj">
......@@ -50,5 +56,15 @@
<Name>Lib.Interfaces</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Page Include="AuthorizeProvider\AccountManagementPane.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Themes\Generic.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
</Project>
\ No newline at end of file
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" ControlsResourcesVersion="Version2" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
......@@ -5,7 +5,7 @@ namespace Richasy.Bili.Models.App.Constants
/// <summary>
/// 服务相关的常量.
/// </summary>
public class ServiceConstants
public static class ServiceConstants
{
#pragma warning disable SA1600 // Elements should be documented
#pragma warning disable SA1401 // Fields should be private
......@@ -34,6 +34,7 @@ namespace Richasy.Bili.Models.App.Constants
public static class Messages
{
public static string NotFound = "没有找到你所需要的资源";
public static string AuthenticationProviderMissing = "Authentication provider is required before sending a request.";
public static string BaseUrlMissing = "Base URL cannot be null or empty.";
public static string InvalidTypeForDateConverter = "DateConverter can only serialize objects of type Date.";
......
......@@ -15,7 +15,7 @@ namespace Richasy.Bili.Models.Enums
/// <summary>
/// 用户已退出.
/// </summary>
SingedOut,
SignedOut,
/// <summary>
/// 用户已登录.
......
......@@ -11,5 +11,7 @@ namespace Richasy.Bili.Models.Enums
/// Application theme.
/// </summary>
AppTheme,
BiliUserId,
AuthorizeResult,
}
}
......@@ -41,6 +41,14 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Lib\Lib.Interfaces\Lib.Interfaces.csproj">
<Project>{cae9b987-6db2-42a8-b3f2-3bb700cc4dd8}</Project>
<Name>Lib.Interfaces</Name>
</ProjectReference>
<ProjectReference Include="..\..\Lib\Lib.Uwp\Lib.Uwp.csproj">
<Project>{c8ab398f-10c2-4b97-87f0-f09b922947d6}</Project>
<Name>Lib.Uwp</Name>
</ProjectReference>
<ProjectReference Include="..\..\Models\Models.BiliBili\Models.BiliBili.csproj">
<Project>{8776a0bd-dbd1-4f11-a022-400d044ff618}</Project>
<Name>Models.BiliBili</Name>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册