mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
prevent add same user
This commit is contained in:
@@ -9,7 +9,7 @@ namespace Snap.Hutao.Control.Cancellable;
|
|||||||
public interface ISupportCancellation
|
public interface ISupportCancellation
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用于通知取消的取消回执
|
/// 用于通知事件取消的取消令牌
|
||||||
/// </summary>
|
/// </summary>
|
||||||
CancellationToken CancellationToken { get; set; }
|
CancellationToken CancellationToken { get; set; }
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,7 @@ internal class NavigationService : INavigationService
|
|||||||
navigationView.BackRequested -= OnBackRequested;
|
navigationView.BackRequested -= OnBackRequested;
|
||||||
}
|
}
|
||||||
|
|
||||||
navigationView = Must.NotNull(value!, "NavigationView");
|
navigationView = Must.NotNull(value!);
|
||||||
|
|
||||||
// add new listener
|
// add new listener
|
||||||
if (navigationView != null)
|
if (navigationView != null)
|
||||||
@@ -138,7 +138,7 @@ internal class NavigationService : INavigationService
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await data.WaitForCompletionAsync();
|
await data.WaitForCompletionAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch (AggregateException)
|
catch (AggregateException)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public interface IUserService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="user">待添加的用户</param>
|
/// <param name="user">待添加的用户</param>
|
||||||
/// <returns>用户初始化是否成功</returns>
|
/// <returns>用户初始化是否成功</returns>
|
||||||
Task<bool> TryAddUserAsync(User user);
|
Task<UserAddResult> TryAddUserAsync(User user);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步移除用户
|
/// 异步移除用户
|
||||||
@@ -47,4 +47,4 @@ public interface IUserService
|
|||||||
/// <param name="cookie">cookie的字符串形式</param>
|
/// <param name="cookie">cookie的字符串形式</param>
|
||||||
/// <returns>包含cookie信息的字典</returns>
|
/// <returns>包含cookie信息的字典</returns>
|
||||||
IDictionary<string, string> ParseCookie(string cookie);
|
IDictionary<string, string> ParseCookie(string cookie);
|
||||||
}
|
}
|
||||||
25
src/Snap.Hutao/Snap.Hutao/Service/User/UserAddResult.cs
Normal file
25
src/Snap.Hutao/Snap.Hutao/Service/User/UserAddResult.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
// Copyright (c) DGP Studio. All rights reserved.
|
||||||
|
// Licensed under the MIT license.
|
||||||
|
|
||||||
|
namespace Snap.Hutao.Service.Abstraction;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用户添加操作结果
|
||||||
|
/// </summary>
|
||||||
|
public enum UserAddResult
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 添加成功
|
||||||
|
/// </summary>
|
||||||
|
Ok,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 已经存在该用户
|
||||||
|
/// </summary>
|
||||||
|
AlreadyExists,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化用户失败
|
||||||
|
/// </summary>
|
||||||
|
InitializeFailed,
|
||||||
|
}
|
||||||
@@ -69,23 +69,41 @@ internal class UserService : IUserService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public async Task<bool> TryAddUserAsync(User user)
|
public async Task<UserAddResult> TryAddUserAsync(User user)
|
||||||
{
|
{
|
||||||
if (await user.InitializeAsync(userClient, userGameRoleClient))
|
string? newUsersCookie = user.Cookie;
|
||||||
|
|
||||||
|
// Prevent users add same account.
|
||||||
|
bool userAlreadyExists = await appDbContext.Users
|
||||||
|
.AnyAsync(u => u.Cookie == newUsersCookie)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (userAlreadyExists)
|
||||||
{
|
{
|
||||||
appDbContext.Users.Add(user);
|
return UserAddResult.AlreadyExists;
|
||||||
await appDbContext.SaveChangesAsync();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
bool userInitialized = await user
|
||||||
|
.InitializeAsync(userClient, userGameRoleClient)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (userInitialized)
|
||||||
|
{
|
||||||
|
appDbContext.Users.Add(user);
|
||||||
|
await appDbContext
|
||||||
|
.SaveChangesAsync()
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
return UserAddResult.Ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
return UserAddResult.InitializeFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public async Task RemoveUserAsync(User user)
|
public Task RemoveUserAsync(User user)
|
||||||
{
|
{
|
||||||
appDbContext.Users.Remove(user);
|
appDbContext.Users.Remove(user);
|
||||||
await appDbContext.SaveChangesAsync();
|
return appDbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
Title="设置米游社Cookie"
|
Title="设置米游社Cookie"
|
||||||
DefaultButton="Primary"
|
DefaultButton="Primary"
|
||||||
PrimaryButtonText="请输入Cookie"
|
PrimaryButtonText="请输入Cookie"
|
||||||
SecondaryButtonText="取消"
|
CloseButtonText="取消"
|
||||||
Style="{StaticResource DefaultContentDialogStyle}">
|
Style="{StaticResource DefaultContentDialogStyle}">
|
||||||
|
|
||||||
<StackPanel>
|
<StackPanel>
|
||||||
|
|||||||
@@ -34,9 +34,7 @@ public sealed partial class UserDialog : ContentDialog
|
|||||||
|
|
||||||
private void InputTextChanged(object sender, TextChangedEventArgs e)
|
private void InputTextChanged(object sender, TextChangedEventArgs e)
|
||||||
{
|
{
|
||||||
string text = InputText.Text;
|
bool inputEmpty = string.IsNullOrEmpty(InputText.Text);
|
||||||
|
|
||||||
bool inputEmpty = string.IsNullOrEmpty(text);
|
|
||||||
|
|
||||||
(PrimaryButtonText, IsPrimaryButtonEnabled) = inputEmpty switch
|
(PrimaryButtonText, IsPrimaryButtonEnabled) = inputEmpty switch
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
using Microsoft.UI.Xaml;
|
using Microsoft.UI.Xaml;
|
||||||
using Microsoft.UI.Xaml.Controls;
|
using Microsoft.UI.Xaml.Controls;
|
||||||
using Snap.Hutao.Core;
|
using Snap.Hutao.Core;
|
||||||
|
using Snap.Hutao.Service.Navigation;
|
||||||
|
|
||||||
namespace Snap.Hutao.View.Helper;
|
namespace Snap.Hutao.View.Helper;
|
||||||
|
|
||||||
@@ -13,7 +14,7 @@ namespace Snap.Hutao.View.Helper;
|
|||||||
public sealed class NavHelper
|
public sealed class NavHelper
|
||||||
{
|
{
|
||||||
private static readonly DependencyProperty NavigateToProperty = Property<NavHelper>.Attach<Type>("NavigateTo");
|
private static readonly DependencyProperty NavigateToProperty = Property<NavHelper>.Attach<Type>("NavigateTo");
|
||||||
private static readonly DependencyProperty ExtraDataProperty = Property<NavHelper>.Attach<object>("ExtraData");
|
private static readonly DependencyProperty ExtraDataProperty = Property<NavHelper>.Attach<INavigationExtra>("ExtraData");
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取导航项的目标页面类型
|
/// 获取导航项的目标页面类型
|
||||||
@@ -40,9 +41,9 @@ public sealed class NavHelper
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="item">待获取的导航项</param>
|
/// <param name="item">待获取的导航项</param>
|
||||||
/// <returns>目标页面类型的额外数据</returns>
|
/// <returns>目标页面类型的额外数据</returns>
|
||||||
public static object? GetExtraData(NavigationViewItem? item)
|
public static INavigationExtra? GetExtraData(NavigationViewItem? item)
|
||||||
{
|
{
|
||||||
return item?.GetValue(ExtraDataProperty);
|
return item?.GetValue(ExtraDataProperty) as INavigationExtra;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -50,7 +51,7 @@ public sealed class NavHelper
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="item">待设置的导航项</param>
|
/// <param name="item">待设置的导航项</param>
|
||||||
/// <param name="value">新的目标页面类型</param>
|
/// <param name="value">新的目标页面类型</param>
|
||||||
public static void SetExtraData(NavigationViewItem item, object value)
|
public static void SetExtraData(NavigationViewItem item, INavigationExtra value)
|
||||||
{
|
{
|
||||||
item.SetValue(ExtraDataProperty, value);
|
item.SetValue(ExtraDataProperty, value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -117,9 +117,9 @@ internal class UserViewModel : ObservableObject
|
|||||||
// user confirms the input
|
// user confirms the input
|
||||||
if (result.IsOk)
|
if (result.IsOk)
|
||||||
{
|
{
|
||||||
IDictionary<string, string> map = userService.ParseCookie(result.Value);
|
IDictionary<string, string> cookieMap = userService.ParseCookie(result.Value);
|
||||||
|
|
||||||
if (TryValidateCookie(map, out IDictionary<string, string>? filteredCookie))
|
if (TryValidateCookie(cookieMap, out IDictionary<string, string>? filteredCookie))
|
||||||
{
|
{
|
||||||
string simplifiedCookie = string.Join(';', filteredCookie.Select(kvp => $"{kvp.Key}={kvp.Value}"));
|
string simplifiedCookie = string.Join(';', filteredCookie.Select(kvp => $"{kvp.Key}={kvp.Value}"));
|
||||||
User user = new()
|
User user = new()
|
||||||
@@ -128,13 +128,17 @@ internal class UserViewModel : ObservableObject
|
|||||||
RemoveCommand = removeUserCommandCache,
|
RemoveCommand = removeUserCommandCache,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!await userService.TryAddUserAsync(user))
|
switch (await userService.TryAddUserAsync(user))
|
||||||
{
|
{
|
||||||
infoBarService.Warning("此Cookie无法获取用户信息,请重新输入");
|
case UserAddResult.Ok:
|
||||||
}
|
infoBarService.Success($"用户 [{user.UserInfo!.Nickname}] 添加成功");
|
||||||
else
|
break;
|
||||||
{
|
case UserAddResult.AlreadyExists:
|
||||||
infoBarService.Success($"成功添加用户 [{user.UserInfo!.Nickname}]");
|
infoBarService.Information($"用户 [{user.UserInfo!.Nickname}] 已经存在");
|
||||||
|
break;
|
||||||
|
case UserAddResult.InitializeFailed:
|
||||||
|
infoBarService.Warning("此Cookie无法获取用户信息,请重新输入");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -149,7 +153,7 @@ internal class UserViewModel : ObservableObject
|
|||||||
if (!User.IsNone(user))
|
if (!User.IsNone(user))
|
||||||
{
|
{
|
||||||
await userService.RemoveUserAsync(user);
|
await userService.RemoveUserAsync(user);
|
||||||
infoBarService.Success($"成功移除用户 [{user.UserInfo!.Nickname}]");
|
infoBarService.Success($"用户 [{user.UserInfo!.Nickname}] 成功移除");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user