fix: windows识别邀请码

This commit is contained in:
2026-01-19 09:04:19 -08:00
parent d60f1d3c5d
commit 6da7649720
3 changed files with 81 additions and 16 deletions
+48 -13
View File
@@ -91,6 +91,15 @@ class KRDeviceUtil {
String? code = _kr_extractCode(executablePath);
if (code != null) return code;
// 策略 1.5: (Windows 独有) 检测安装目录下的 channel.txt (支持 Inno Setup 方案)
if (Platform.isWindows) {
code = await _kr_getInviteCodeFromChannelFile(executablePath);
if (code != null) {
KRLogUtil.kr_i('🎯 从 channel.txt 获取到邀请码: $code', tag: 'DeviceUtil');
return code;
}
}
if (Platform.isMacOS) {
// 策略 2: 如果在 /Volumes 下运行,尝试找 DMG 原文件名
if (executablePath.contains('/Volumes/')) {
@@ -107,29 +116,31 @@ class KRDeviceUtil {
KRLogUtil.kr_i('🎯 从 mdls (文件元数据) 获取到邀请码: $code', tag: 'DeviceUtil');
return code;
}
// 策略 4: 在下载目录下寻找最近的带有 ic- 的 DMG
code = await _kr_searchDownloadsForInviteCode();
if (code != null) {
KRLogUtil.kr_i('🎯 从下载目录搜索获取到邀请码: $code', tag: 'DeviceUtil');
return code;
}
KRLogUtil.kr_i('⚠️ 深度检测完成,未发现有效的邀请码标识', tag: 'DeviceUtil');
}
// 策略 4: 在下载目录下寻找最近的带有 ic- 的程序 (MacOS/Windows 通用)
code = await _kr_searchDownloadsForInviteCode();
if (code != null) {
KRLogUtil.kr_i('🎯 从下载目录搜索获取到邀请码: $code', tag: 'DeviceUtil');
return code;
}
KRLogUtil.kr_i('⚠️ 深度检测完成,未发现有效的邀请码标识', tag: 'DeviceUtil');
} catch (e) {
KRLogUtil.kr_e('解析桌面邀请码异常: $e', tag: 'DeviceUtil');
}
return '';
}
/// (MacOS) 在下载目录下搜寻最近的、符合命名规范的 DMG
/// (MacOS/Windows) 在下载目录下搜寻最近的、符合命名规范的安装包
Future<String?> _kr_searchDownloadsForInviteCode() async {
try {
final String home = Platform.environment['HOME'] ?? '';
// 适配不同平台的家目录环境变量
final String home = Platform.environment['HOME'] ??
Platform.environment['USERPROFILE'] ?? '';
if (home.isEmpty) return null;
final Directory downloads = Directory('$home/Downloads');
final Directory downloads = Directory('${home}${Platform.pathSeparator}Downloads');
if (!await downloads.exists()) return null;
final List<FileSystemEntity> files = await downloads.list().toList();
@@ -154,7 +165,31 @@ class KRDeviceUtil {
return null;
}
/// 正则提取 ic- 模式
/// (Windows) 从安装目录下的 channel.txt 中读取邀请码 (针对 Inno Setup 方案)
Future<String?> _kr_getInviteCodeFromChannelFile(String execPath) async {
try {
final File exeFile = File(execPath);
final String dirPath = exeFile.parent.path;
final File channelFile = File('$dirPath${Platform.pathSeparator}channel.txt');
if (await channelFile.exists()) {
final String content = await channelFile.readAsString();
final String channel = content.trim();
if (channel.isNotEmpty) {
// 兼容两种格式:纯邀请码 或 带 ic- 标识的邀请码
if (channel.contains('ic-')) {
return _kr_extractCode(channel);
}
return channel; // 如果 Inno Setup 已经截取好了纯 code,直接返回
}
}
} catch (e) {
KRLogUtil.kr_e('读取 Windows channel.txt 失败: $e', tag: 'DeviceUtil');
}
return null;
}
/// 正则提取 ic-模式
String? _kr_extractCode(String source) {
final RegExp regExp = RegExp(r'ic-([A-Za-z0-9-_]+)');
final Match? match = regExp.firstMatch(source);