Files
pangolin/client/lib/services/device_identity.dart
T
wangjia 58526b09e6
ci-pangolin / Lint — shellcheck (push) Successful in 7s
ci-pangolin / OpenAPI Sync Check (push) Successful in 19s
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Successful in 7s
ci-pangolin / Flutter — analyze + test (push) Successful in 23s
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (push) Successful in 5s
ci-pangolin / Codegen Drift — token 生成物未漂移 (push) Successful in 5s
ci-pangolin / Go — build + test (push) Successful in 9s
ci-pangolin / E2E Smoke — L4 进程级端到端 (push) Successful in 14s
ci-pangolin / Go — integration (mysql/redis testcontainers) (push) Failing after 4m12s
ci-pangolin / Golden — 视觉回归 (components + auth) (push) Successful in 15s
feat(client): P6 DevicesScreen 重做 + DevicePlatform 枚举
回应「状态码用枚举更好」:新增 DevicePlatform 枚举(线格式 fromWire 解析,
UI 只碰枚举,穷尽 switch),Device.platformKind getter;platform 图标按枚举 switch。
DevicesScreen 按 §7 视觉稿重做:行内 名称+「本机」标 / 平台·客户端版本·最后登录 /
在线绿点光晕·离线灰点 + 行尾 ⋯ 菜单(强制退出 / 清除登录信息·危险红)+ 危险二次
确认弹窗;本机行不显示 ⋯(不自我踢)。l10n 新增在线/离线/最后登录/从未登录/强制
退出/清除登录信息/取消 + 确认文案(中英);「当前设备」改「本机」。
新图标 moreVertical/trash/alertTriangle。localDeviceIdProvider 供本机判定。
测试:devices_screen widget 3 例(本机标+在线/离线+版本渲染 / ⋯菜单→强制退出→
确认→POST logout / 清除→DELETE);全量 flutter test 58 绿。

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

139 lines
4.5 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';
}
Future<String> _name() async {
try {
if (Platform.isIOS) {
final i = await DeviceInfoPlugin().iosInfo;
return i.name.isNotEmpty ? i.name : i.utsname.machine;
}
if (Platform.isAndroid) {
final a = await DeviceInfoPlugin().androidInfo;
return '${a.manufacturer} ${a.model}'.trim();
}
// 桌面:主机名最有意义(MacBook-Pro / DESKTOP-XXXX)。
return Platform.localHostname;
} catch (_) {
return currentPlatform();
}
}
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());