chatapp3-flutter/lib/shared/tools/sc_deviceId_utils.dart
2026-04-09 21:32:23 +08:00

64 lines
2.1 KiB
Dart

import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:yumi/app/constants/sc_global_config.dart';
import 'package:yumi/shared/data_sources/sources/local/data_persistence.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:uuid/uuid.dart';
class SCDeviceIdUtils {
static void initDeviceinfo() {
PackageInfo.fromPlatform().then((info) {
SCGlobalConfig.version = info.version;
SCGlobalConfig.build = info.buildNumber;
if (Platform.isAndroid) {
SCGlobalConfig.channel = "Google";
DeviceInfoPlugin().androidInfo.then((dev) {
SCGlobalConfig.model = dev.model;
SCGlobalConfig.sysVersion = dev.version.release;
SCGlobalConfig.sdkInt = dev.version.sdkInt;
});
} else if (Platform.isIOS) {
SCGlobalConfig.channel = "AppStore";
DeviceInfoPlugin().iosInfo.then((dev) {
SCGlobalConfig.model = dev.model;
SCGlobalConfig.sysVersion = dev.systemVersion;
});
}
});
}
// 获取持久化的唯一设备ID
static Future<String> getDeviceId({bool forceRefresh = false}) async {
// 如果强制刷新或配置中不存在,则重新生成
if (forceRefresh || SCGlobalConfig.imei.isEmpty) {
final newId = await _g();
DataPersistence.setUniqueId(newId);
SCGlobalConfig.imei = newId;
return newId;
}
// 检查存储中的ID
String storedId = DataPersistence.getUniqueId();
if (storedId.isNotEmpty) {
SCGlobalConfig.imei = storedId;
return storedId;
}
// 生成新ID并存储
final newId = await _g();
DataPersistence.setUniqueId(newId);
SCGlobalConfig.imei = newId;
return newId;
}
// 生成组合唯一ID
static Future<String> _g() async {
final packageInfo = await PackageInfo.fromPlatform();
// 生成新 UUID 并存储
final newId = '${const Uuid().v4()}-${packageInfo.packageName}';
DataPersistence.setUniqueId(newId);
return newId;
}
}