Files
pangolin/client/lib/services/device_identity.dart
wangjia 315c343ed5
ci-pangolin / Lint — shellcheck (push) Successful in 7s
ci-pangolin / OpenAPI Sync Check (push) Successful in 17s
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Successful in 6s
ci-pangolin / Flutter — analyze + test (push) Failing after 14s
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (push) Successful in 5s
ci-pangolin / Codegen Drift — token 生成物未漂移 (push) Successful in 6s
ci-pangolin / Go — build + test (push) Successful in 11s
ci-pangolin / E2E Smoke — L4 进程级端到端 (push) Successful in 14s
ci-pangolin / Go — integration (mysql/redis testcontainers) (push) Failing after 4m3s
ci-pangolin / Golden — 视觉回归 (components + auth) (push) Successful in 14s
feat(devices): 设备默认名「平台简写·主机名」+ 支持重命名(1.0.13)
① 默认名:DeviceIdentity 改「平台简写(Win/Mac/Linux/iOS/Andr)·主机名」,主机名截
   到 8、总长≈12(替代裸主机名,更短)。
② 重命名:新端点 POST /v1/me/devices/{uuid}/rename(Service.RenameDevice 校验归属+
   截 64);store.UpdateName;客户端 account_api.renameDevice + provider.rename +
   「我的设备」⋯ 菜单加「重命名」+ 输入弹窗;l10n(zh/en)。
③ 版本 1.0.13。
测试:server RenameDevice(归属/空名/404)+ 客户端 rename widget;全量 go/flutter 绿。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-29 09:12:36 +08:00

155 lines
5.0 KiB
Dart

// device_identity.dart — 设备身份:稳定 device_id + 名称/平台/客户端版本上报
//
// device_id 首次启动随机生成(UUID v4)、写入 flutter_secure_storage 持久化,之后
// 每次读取复用 → 同一安装跨重启恒定(替换早期硬编码 'mac-001')。名称/平台/版本
// 取自系统。随 登录/注册/连接 上报给控制面,用于「我的设备」登记与会话绑定。
import 'dart:io' show Platform;
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:uuid/uuid.dart';
/// 设备元数据,随认证/连接请求上报。
class DeviceMeta {
const DeviceMeta({
required this.id,
required this.name,
required this.platform,
required this.clientVersion,
});
final String id;
final String name;
final String platform; // ios | android | windows | macos | linux
final String clientVersion;
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'platform': platform,
'client_version': clientVersion,
};
}
/// 极简键值存储接缝(便于单测注入;默认走 flutter_secure_storage)。
abstract class SecureKV {
Future<String?> read(String key);
Future<void> write(String key, String value);
}
class _SecureStorageKV implements SecureKV {
// 与 TokenStore 一致:macOS 文件式 keychain,避免未签名 app 报 -34018。
static const _s = FlutterSecureStorage(
mOptions: MacOsOptions(useDataProtectionKeyChain: false),
);
@override
Future<String?> read(String key) => _s.read(key: key);
@override
Future<void> write(String key, String value) => _s.write(key: key, value: value);
}
/// 设备身份服务。device_id 持久于安全存储;其余字段取自系统(缓存)。
class DeviceIdentity {
DeviceIdentity({SecureKV? store}) : _kv = store ?? _SecureStorageKV();
final SecureKV _kv;
static const _kDeviceId = 'pangolin_device_id';
static const _uuid = Uuid();
String? _cachedId;
DeviceMeta? _cachedMeta;
/// 稳定 device_id。读到→复用;读到空→生成并持久化;
/// 读**失败**(平台异常)≠ 不存在 → 退回进程内临时 id,**不写库**,避免冲掉真实 id。
Future<String> deviceId() async {
if (_cachedId != null) return _cachedId!;
String? existing;
try {
existing = await _kv.read(_kDeviceId);
} catch (_) {
return _cachedId ??= _uuid.v4(); // 临时,不持久化
}
if (existing != null && existing.isNotEmpty) return _cachedId = existing;
final id = _uuid.v4();
try {
await _kv.write(_kDeviceId, id);
} catch (_) {
// 写失败:本次用内存值,下次再尝试持久化。
}
return _cachedId = id;
}
/// 完整设备元数据(缓存,首次解析后复用)。
Future<DeviceMeta> meta() async {
if (_cachedMeta != null) return _cachedMeta!;
final m = DeviceMeta(
id: await deviceId(),
name: await _name(),
platform: currentPlatform(),
clientVersion: await _clientVersion(),
);
return _cachedMeta = m;
}
/// 当前平台标识(与服务端 devices.platform 枚举对齐)。
static String currentPlatform() {
if (Platform.isIOS) return 'ios';
if (Platform.isAndroid) return 'android';
if (Platform.isMacOS) return 'macos';
if (Platform.isWindows) return 'windows';
if (Platform.isLinux) return 'linux';
return 'unknown';
}
/// 平台简写(默认名前缀,保持短)。
static String _shortPlatform(String p) => switch (p) {
'windows' => 'Win',
'macos' => 'Mac',
'linux' => 'Linux',
'ios' => 'iOS',
'android' => 'Andr',
_ => p,
};
// 默认名 = 平台简写·主机名(桌面用主机名,移动用机型);主机名截到 8、总长≈12,尽量短。
// 用户可在「我的设备」改名覆盖。
Future<String> _name() async {
var host = '';
try {
if (Platform.isIOS) {
final i = await DeviceInfoPlugin().iosInfo;
host = i.name.isNotEmpty ? i.name : i.utsname.machine;
} else if (Platform.isAndroid) {
final a = await DeviceInfoPlugin().androidInfo;
host = a.model;
} else {
host = Platform.localHostname;
}
} catch (_) {
host = '';
}
host = host.trim();
if (host.length > 8) host = host.substring(0, 8);
final short = _shortPlatform(currentPlatform());
return host.isEmpty ? short : '$short·$host';
}
Future<String> _clientVersion() async {
try {
final info = await PackageInfo.fromPlatform();
return 'v${info.version}';
} catch (_) {
return '';
}
}
}
/// 单例 DeviceIdentity(测试可 override)。
final deviceIdentityProvider = Provider<DeviceIdentity>((ref) => DeviceIdentity());
/// 本机 device_id(供「本机」高亮;未就绪时为空)。
final localDeviceIdProvider =
FutureProvider<String>((ref) => ref.read(deviceIdentityProvider).deviceId());