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 Future initDeviceinfo() async { final info = await PackageInfo.fromPlatform(); SCGlobalConfig.version = info.version; SCGlobalConfig.build = info.buildNumber; final processorCount = Platform.numberOfProcessors; if (Platform.isAndroid) { SCGlobalConfig.channel = "Google"; final dev = await DeviceInfoPlugin().androidInfo; SCGlobalConfig.model = dev.model; SCGlobalConfig.sysVersion = dev.version.release; SCGlobalConfig.sdkInt = dev.version.sdkInt; SCGlobalConfig.applyDevicePerformanceProfile( isLowRamDevice: dev.isLowRamDevice, processorCount: processorCount, ); } else if (Platform.isIOS) { SCGlobalConfig.channel = "AppStore"; final dev = await DeviceInfoPlugin().iosInfo; SCGlobalConfig.model = dev.model; SCGlobalConfig.sysVersion = dev.systemVersion; SCGlobalConfig.applyDevicePerformanceProfile( isLowRamDevice: false, processorCount: processorCount, ); } } // 获取持久化的唯一设备ID static Future 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 _g() async { final packageInfo = await PackageInfo.fromPlatform(); // 生成新 UUID 并存储 final newId = '${const Uuid().v4()}-${packageInfo.packageName}'; DataPersistence.setUniqueId(newId); return newId; } }