mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
fix hoyolab user creation
This commit is contained in:
@@ -123,7 +123,7 @@ internal sealed class User : ObservableObject
|
|||||||
internal static async Task<User?> CreateAsync(Cookie cookie, bool isOversea, CancellationToken token = default)
|
internal static async Task<User?> CreateAsync(Cookie cookie, bool isOversea, CancellationToken token = default)
|
||||||
{
|
{
|
||||||
// 这里只负责创建实体用户,稍后在用户服务中保存到数据库
|
// 这里只负责创建实体用户,稍后在用户服务中保存到数据库
|
||||||
EntityUser entity = EntityUser.Create(cookie);
|
EntityUser entity = EntityUser.Create(cookie, isOversea);
|
||||||
|
|
||||||
entity.Aid = cookie.GetValueOrDefault(Cookie.STUID);
|
entity.Aid = cookie.GetValueOrDefault(Cookie.STUID);
|
||||||
entity.Mid = isOversea ? entity.Aid : cookie.GetValueOrDefault(Cookie.MID);
|
entity.Mid = isOversea ? entity.Aid : cookie.GetValueOrDefault(Cookie.MID);
|
||||||
|
|||||||
@@ -64,24 +64,11 @@ internal sealed class User : ISelectable
|
|||||||
/// 创建一个新的用户
|
/// 创建一个新的用户
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cookie">cookie</param>
|
/// <param name="cookie">cookie</param>
|
||||||
|
/// <param name="isOversea">是否为国际服</param>
|
||||||
/// <returns>新创建的用户</returns>
|
/// <returns>新创建的用户</returns>
|
||||||
public static User Create(Cookie cookie)
|
public static User Create(Cookie cookie, bool isOversea)
|
||||||
{
|
{
|
||||||
_ = cookie.TryGetAsSToken(out Cookie? stoken);
|
_ = cookie.TryGetAsSToken(isOversea, out Cookie? stoken);
|
||||||
_ = cookie.TryGetAsLToken(out Cookie? ltoken);
|
|
||||||
_ = cookie.TryGetAsCookieToken(out Cookie? cookieToken);
|
|
||||||
|
|
||||||
return new() { SToken = stoken, LToken = ltoken, CookieToken = cookieToken };
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 创建一个国际服用户
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="cookie">cookie</param>
|
|
||||||
/// <returns>新创建的用户</returns>
|
|
||||||
public static User CreateOs(Cookie cookie)
|
|
||||||
{
|
|
||||||
_ = cookie.TryGetAsLegacySToken(out Cookie? stoken);
|
|
||||||
_ = cookie.TryGetAsLToken(out Cookie? ltoken);
|
_ = cookie.TryGetAsLToken(out Cookie? ltoken);
|
||||||
_ = cookie.TryGetAsCookieToken(out Cookie? cookieToken);
|
_ = cookie.TryGetAsCookieToken(out Cookie? cookieToken);
|
||||||
|
|
||||||
|
|||||||
@@ -194,7 +194,7 @@ internal class UserService : IUserService
|
|||||||
public async Task<ValueResult<UserOptionResult, string>> ProcessInputCookieAsync(Cookie cookie, bool isOversea)
|
public async Task<ValueResult<UserOptionResult, string>> ProcessInputCookieAsync(Cookie cookie, bool isOversea)
|
||||||
{
|
{
|
||||||
await ThreadHelper.SwitchToBackgroundAsync();
|
await ThreadHelper.SwitchToBackgroundAsync();
|
||||||
string? mid = cookie.GetValueOrDefault(Cookie.MID);
|
string? mid = cookie.GetValueOrDefault(isOversea ? Cookie.MID : Cookie.STUID);
|
||||||
|
|
||||||
if (mid == null)
|
if (mid == null)
|
||||||
{
|
{
|
||||||
@@ -208,7 +208,7 @@ internal class UserService : IUserService
|
|||||||
{
|
{
|
||||||
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||||
|
|
||||||
if (cookie.TryGetAsSToken(out Cookie? stoken))
|
if (cookie.TryGetAsSToken(isOversea, out Cookie? stoken))
|
||||||
{
|
{
|
||||||
user.SToken = stoken;
|
user.SToken = stoken;
|
||||||
user.LToken = cookie.TryGetAsLToken(out Cookie? ltoken) ? ltoken : user.LToken;
|
user.LToken = cookie.TryGetAsLToken(out Cookie? ltoken) ? ltoken : user.LToken;
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ internal sealed partial class LoginHoyoverseUserPage : Microsoft.UI.Xaml.Control
|
|||||||
}
|
}
|
||||||
|
|
||||||
Cookie loginTicketCookie = Cookie.FromCoreWebView2Cookies(cookies);
|
Cookie loginTicketCookie = Cookie.FromCoreWebView2Cookies(cookies);
|
||||||
loginTicketCookie["login_uid"] = uid;
|
loginTicketCookie[Cookie.LOGIN_UID] = uid;
|
||||||
|
|
||||||
// 使用 loginTicket 获取 stoken
|
// 使用 loginTicket 获取 stoken
|
||||||
Response<ListWrapper<NameToken>> multiTokenResponse = await Ioc.Default
|
Response<ListWrapper<NameToken>> multiTokenResponse = await Ioc.Default
|
||||||
@@ -80,12 +80,12 @@ internal sealed partial class LoginHoyoverseUserPage : Microsoft.UI.Xaml.Control
|
|||||||
}
|
}
|
||||||
|
|
||||||
Dictionary<string, string> multiTokenMap = multiTokenResponse.Data.List.ToDictionary(n => n.Name, n => n.Token);
|
Dictionary<string, string> multiTokenMap = multiTokenResponse.Data.List.ToDictionary(n => n.Name, n => n.Token);
|
||||||
Cookie hoyoLabCookie = Cookie.Parse($"{Cookie.STUID}={uid};{Cookie.STOKEN}={multiTokenMap[Cookie.STOKEN]}");
|
Cookie hoyolabCookie = Cookie.FromSToken(uid, multiTokenMap[Cookie.STOKEN]);
|
||||||
|
|
||||||
// 处理 cookie 并添加用户
|
// 处理 cookie 并添加用户
|
||||||
(UserOptionResult result, string nickname) = await Ioc.Default
|
(UserOptionResult result, string nickname) = await Ioc.Default
|
||||||
.GetRequiredService<IUserService>()
|
.GetRequiredService<IUserService>()
|
||||||
.ProcessInputCookieAsync(hoyoLabCookie, true)
|
.ProcessInputCookieAsync(hoyolabCookie, true)
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
Ioc.Default.GetRequiredService<INavigationService>().GoBack();
|
Ioc.Default.GetRequiredService<INavigationService>().GoBack();
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ internal sealed partial class LoginMihoyoUserPage : Microsoft.UI.Xaml.Controls.P
|
|||||||
|
|
||||||
Dictionary<string, string> multiTokenMap = multiTokenResponse.Data.List.ToDictionary(n => n.Name, n => n.Token);
|
Dictionary<string, string> multiTokenMap = multiTokenResponse.Data.List.ToDictionary(n => n.Name, n => n.Token);
|
||||||
|
|
||||||
Cookie stokenV1 = Cookie.Parse($"{Cookie.STUID}={loginTicketCookie[Cookie.LOGIN_UID]};{Cookie.STOKEN}={multiTokenMap[Cookie.STOKEN]}");
|
Cookie stokenV1 = Cookie.FromSToken(loginTicketCookie[Cookie.LOGIN_UID], multiTokenMap[Cookie.STOKEN]);
|
||||||
|
|
||||||
Response<LoginResult> loginResultResponse = await Ioc.Default
|
Response<LoginResult> loginResultResponse = await Ioc.Default
|
||||||
.GetRequiredService<PassportClient>()
|
.GetRequiredService<PassportClient>()
|
||||||
|
|||||||
@@ -201,7 +201,7 @@
|
|||||||
Style="{StaticResource SubtitleTextBlockStyle}"
|
Style="{StaticResource SubtitleTextBlockStyle}"
|
||||||
Text="{shcm:ResourceString Name=ViewUserDefaultDescription}"
|
Text="{shcm:ResourceString Name=ViewUserDefaultDescription}"
|
||||||
Visibility="{Binding Users.Count, Converter={StaticResource Int32ToVisibilityRevertConverter}}"/>
|
Visibility="{Binding Users.Count, Converter={StaticResource Int32ToVisibilityRevertConverter}}"/>
|
||||||
<Grid>
|
<Grid Margin="6,0">
|
||||||
<Grid.ColumnDefinitions>
|
<Grid.ColumnDefinitions>
|
||||||
<ColumnDefinition/>
|
<ColumnDefinition/>
|
||||||
<ColumnDefinition/>
|
<ColumnDefinition/>
|
||||||
|
|||||||
@@ -94,52 +94,20 @@ internal sealed partial class Cookie
|
|||||||
return new(cookieMap);
|
return new(cookieMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryGetAsSToken([NotNullWhen(true)] out Cookie? cookie)
|
public static Cookie FromSToken(string stuid, string stoken)
|
||||||
{
|
{
|
||||||
bool hasMid = TryGetValue(MID, out string? mid);
|
SortedDictionary<string, string> cookieMap = new()
|
||||||
bool hasSToken = TryGetValue(STOKEN, out string? stoken);
|
|
||||||
bool hasSTuid = TryGetValue(STUID, out string? stuid);
|
|
||||||
|
|
||||||
if (hasMid && hasSToken && hasSTuid)
|
|
||||||
{
|
{
|
||||||
cookie = new Cookie(new()
|
[STUID] = stuid,
|
||||||
{
|
[STOKEN] = stoken,
|
||||||
[MID] = mid!,
|
};
|
||||||
[STOKEN] = stoken!,
|
|
||||||
[STUID] = stuid!,
|
|
||||||
});
|
|
||||||
|
|
||||||
return true;
|
return new(cookieMap);
|
||||||
}
|
|
||||||
|
|
||||||
cookie = null;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public bool TryGetAsSToken(bool isOversea, [NotNullWhen(true)] out Cookie? cookie)
|
||||||
/// 提取其中的 stoken 信息
|
|
||||||
/// Used for hoyolab account.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="cookie">A cookie contains stoken and stuid, without mid.</param>
|
|
||||||
/// <returns>是否获取成功</returns>
|
|
||||||
public bool TryGetAsLegacySToken([NotNullWhen(true)] out Cookie? cookie)
|
|
||||||
{
|
{
|
||||||
bool hasSToken = TryGetValue(STOKEN, out string? stoken);
|
return isOversea ? TryGetAsLegacySToken(out cookie) : TryGetAsSToken(out cookie);
|
||||||
bool hasSTuid = TryGetValue(STUID, out string? stuid);
|
|
||||||
|
|
||||||
if (hasSToken && hasSTuid)
|
|
||||||
{
|
|
||||||
cookie = new Cookie(new()
|
|
||||||
{
|
|
||||||
[STOKEN] = stoken!,
|
|
||||||
[STUID] = stuid!,
|
|
||||||
});
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
cookie = null;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryGetAsLToken([NotNullWhen(true)] out Cookie? cookie)
|
public bool TryGetAsLToken([NotNullWhen(true)] out Cookie? cookie)
|
||||||
@@ -206,4 +174,46 @@ internal sealed partial class Cookie
|
|||||||
{
|
{
|
||||||
return string.Join(';', inner.Select(kvp => $"{kvp.Key}={kvp.Value}"));
|
return string.Join(';', inner.Select(kvp => $"{kvp.Key}={kvp.Value}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool TryGetAsSToken([NotNullWhen(true)] out Cookie? cookie)
|
||||||
|
{
|
||||||
|
bool hasMid = TryGetValue(MID, out string? mid);
|
||||||
|
bool hasSToken = TryGetValue(STOKEN, out string? stoken);
|
||||||
|
bool hasSTuid = TryGetValue(STUID, out string? stuid);
|
||||||
|
|
||||||
|
if (hasMid && hasSToken && hasSTuid)
|
||||||
|
{
|
||||||
|
cookie = new Cookie(new()
|
||||||
|
{
|
||||||
|
[MID] = mid!,
|
||||||
|
[STOKEN] = stoken!,
|
||||||
|
[STUID] = stuid!,
|
||||||
|
});
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool TryGetAsLegacySToken([NotNullWhen(true)] out Cookie? cookie)
|
||||||
|
{
|
||||||
|
bool hasSToken = TryGetValue(STOKEN, out string? stoken);
|
||||||
|
bool hasSTuid = TryGetValue(STUID, out string? stuid);
|
||||||
|
|
||||||
|
if (hasSToken && hasSTuid)
|
||||||
|
{
|
||||||
|
cookie = new Cookie(new()
|
||||||
|
{
|
||||||
|
[STOKEN] = stoken!,
|
||||||
|
[STUID] = stuid!,
|
||||||
|
});
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user