Files
wx-win-agent/node-agent/WxAgent.Core/AccountBindingMatcher.cs
T
rogee e8abb15f68
Build web service image / build (push) Successful in 1m6s
fix: authorize verified offline data sync
2026-09-30 13:30:48 +08:00

44 lines
2.0 KiB
C#

namespace WxAgent.Core;
public sealed record UiAccountIdentity(string? WechatId, string? Nickname);
public sealed record DatabaseAccountIdentity(string AccountId, string? WechatId, string? Nickname);
public sealed record AccountMatch(string? AccountId, string Strategy, bool IsUnique)
{
public bool IsMatched => AccountId is not null && IsUnique;
}
public static class AccountBindingMatcher
{
public static AccountMatch Match(UiAccountIdentity ui, IReadOnlyList<DatabaseAccountIdentity> accounts)
{
ArgumentNullException.ThrowIfNull(ui);
ArgumentNullException.ThrowIfNull(accounts);
var wechatId = Normalize(ui.WechatId);
if (wechatId is not null)
{
var matches = accounts.Where(a => string.Equals(Normalize(a.WechatId), wechatId, StringComparison.OrdinalIgnoreCase)).ToArray();
if (matches.Length == 1) return new AccountMatch(matches[0].AccountId, "wechatId", true);
if (matches.Length > 1) return new AccountMatch(null, "wechatId", false);
}
var nickname = Normalize(ui.Nickname);
if (nickname is null) return new AccountMatch(null, "none", false);
var nicknameMatches = accounts.Where(a => string.Equals(Normalize(a.Nickname), nickname, StringComparison.Ordinal)).ToArray();
return nicknameMatches.Length == 1
? new AccountMatch(nicknameMatches[0].AccountId, "nickname", true)
: new AccountMatch(null, nicknameMatches.Length == 0 ? "none" : "nickname", false);
}
public static bool IsUniquelyMatchedToAccount(
string expectedAccountId,
UiAccountIdentity boundIdentity,
IReadOnlyList<DatabaseAccountIdentity> databaseAccounts)
{
if (string.IsNullOrWhiteSpace(expectedAccountId)) return false;
var match = Match(boundIdentity, databaseAccounts);
return match.IsMatched && string.Equals(match.AccountId, expectedAccountId, StringComparison.OrdinalIgnoreCase);
}
private static string? Normalize(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim();
}