From 98343906f9bd2c457341ec0e21de7283f30cf327 Mon Sep 17 00:00:00 2001 From: meoacgx Date: Tue, 6 Jan 2026 16:48:13 +0800 Subject: [PATCH] Fix import: infer phone when missing --- .../Services/Telegram/AccountImportService.cs | 15 ++++++++++++++- src/TelegramPanel.Web/Program.cs | 10 ++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/TelegramPanel.Core/Services/Telegram/AccountImportService.cs b/src/TelegramPanel.Core/Services/Telegram/AccountImportService.cs index 7dbe927..bd8fd78 100644 --- a/src/TelegramPanel.Core/Services/Telegram/AccountImportService.cs +++ b/src/TelegramPanel.Core/Services/Telegram/AccountImportService.cs @@ -347,7 +347,10 @@ public class AccountImportService return new ImportResult(false, null, null, null, null, $"json 缺少 api_hash: {jsonPath}"); if (!TryGetString(root, out var phone, "phone", "phone_number", "phoneNumber") || string.IsNullOrWhiteSpace(phone)) - return new ImportResult(false, null, null, null, null, $"json 缺少 phone: {jsonPath}"); + { + if (!TryInferPhone(root, jsonPath, out phone)) + return new ImportResult(false, null, null, null, null, $"json 缺少 phone: {jsonPath}"); + } phone = PhoneNumberFormatter.NormalizeToDigits(phone); if (string.IsNullOrWhiteSpace(phone)) @@ -513,6 +516,16 @@ public class AccountImportService return string.IsNullOrWhiteSpace(username) ? null : username.Trim(); } + private static bool TryInferPhone(System.Text.Json.JsonElement root, string jsonPath, out string? phone) + { + // 兼容一些导出:json 里 phone 可能为 null,但文件名/ session_file 会带 +手机号 + if (!TryGetString(root, out phone, "session_file", "sessionFile") || string.IsNullOrWhiteSpace(phone)) + phone = Path.GetFileNameWithoutExtension(jsonPath); + + phone = string.IsNullOrWhiteSpace(phone) ? null : phone.Trim(); + return !string.IsNullOrWhiteSpace(phone); + } + private static bool LooksLikeSqliteSession(string filePath) { try diff --git a/src/TelegramPanel.Web/Program.cs b/src/TelegramPanel.Web/Program.cs index 1ad2844..2432880 100644 --- a/src/TelegramPanel.Web/Program.cs +++ b/src/TelegramPanel.Web/Program.cs @@ -72,8 +72,14 @@ if (args.Length >= 2 && string.Equals(args[0], "--diag-session-dir", StringCompa if (!TryGetString(root, out var phone, "phone") || string.IsNullOrWhiteSpace(phone)) { - Console.WriteLine($"[FAIL] {Path.GetFileName(jsonPath)}: 缺少 phone"); - continue; + if (!TryGetString(root, out phone, "session_file", "sessionFile") || string.IsNullOrWhiteSpace(phone)) + phone = Path.GetFileNameWithoutExtension(jsonPath); + + if (string.IsNullOrWhiteSpace(phone)) + { + Console.WriteLine($"[FAIL] {Path.GetFileName(jsonPath)}: 缺少 phone"); + continue; + } } _ = TryGetLong(root, out var userId, "user_id", "uid");