device_utils.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import 'dart:io';
  2. import 'package:device_info_plus/device_info_plus.dart';
  3. import 'package:package_info_plus/package_info_plus.dart';
  4. /// 设备工具类
  5. class DeviceUtils {
  6. static final DeviceInfoPlugin _deviceInfo = DeviceInfoPlugin();
  7. static PackageInfo? _packageInfo;
  8. /// 初始化(在应用启动时调用)
  9. static Future<void> init() async {
  10. _packageInfo = await PackageInfo.fromPlatform();
  11. }
  12. // ==================== 平台信息 ====================
  13. /// 是否为 Android 平台
  14. static bool get isAndroid => Platform.isAndroid;
  15. /// 是否为 iOS 平台
  16. static bool get isIOS => Platform.isIOS;
  17. /// 是否为 Web 平台
  18. static bool get isWeb => Platform.isWindows || Platform.isLinux || Platform.isMacOS;
  19. // ==================== 设备信息 ====================
  20. /// 获取设备 ID
  21. static Future<String?> getDeviceId() async {
  22. try {
  23. if (isAndroid) {
  24. final androidInfo = await _deviceInfo.androidInfo;
  25. return androidInfo.id;
  26. } else if (isIOS) {
  27. final iosInfo = await _deviceInfo.iosInfo;
  28. return iosInfo.identifierForVendor;
  29. }
  30. } catch (e) {
  31. return null;
  32. }
  33. return null;
  34. }
  35. /// 获取设备型号
  36. static Future<String?> getDeviceModel() async {
  37. try {
  38. if (isAndroid) {
  39. final androidInfo = await _deviceInfo.androidInfo;
  40. return androidInfo.model;
  41. } else if (isIOS) {
  42. final iosInfo = await _deviceInfo.iosInfo;
  43. return iosInfo.model;
  44. }
  45. } catch (e) {
  46. return null;
  47. }
  48. return null;
  49. }
  50. /// 获取设备品牌
  51. static Future<String?> getDeviceBrand() async {
  52. try {
  53. if (isAndroid) {
  54. final androidInfo = await _deviceInfo.androidInfo;
  55. return androidInfo.brand;
  56. } else if (isIOS) {
  57. final iosInfo = await _deviceInfo.iosInfo;
  58. return 'Apple';
  59. }
  60. } catch (e) {
  61. return null;
  62. }
  63. return null;
  64. }
  65. /// 获取系统版本
  66. static Future<String?> getSystemVersion() async {
  67. try {
  68. if (isAndroid) {
  69. final androidInfo = await _deviceInfo.androidInfo;
  70. return 'Android ${androidInfo.version.release}';
  71. } else if (isIOS) {
  72. final iosInfo = await _deviceInfo.iosInfo;
  73. return 'iOS ${iosInfo.systemVersion}';
  74. }
  75. } catch (e) {
  76. return null;
  77. }
  78. return null;
  79. }
  80. // ==================== 应用信息 ====================
  81. /// 获取应用版本号
  82. static String? get appVersion => _packageInfo?.version;
  83. /// 获取构建号
  84. static String? get buildNumber => _packageInfo?.buildNumber;
  85. /// 获取包名
  86. static String? get packageName => _packageInfo?.packageName;
  87. /// 获取应用名称
  88. static String? get appName => _packageInfo?.appName;
  89. /// 获取完整版本信息
  90. static String? get fullVersion =>
  91. _packageInfo != null
  92. ? '${_packageInfo!.version}+${_packageInfo!.buildNumber}'
  93. : null;
  94. // ==================== 设备标识 ====================
  95. /// 获取用户代理字符串
  96. static Future<String> getUserAgent() async {
  97. final deviceId = await getDeviceId() ?? 'unknown';
  98. final deviceModel = await getDeviceModel() ?? 'unknown';
  99. final systemVersion = await getSystemVersion() ?? 'unknown';
  100. final appVersion = DeviceUtils.appVersion ?? 'unknown';
  101. return 'SinoMedCloud/$appVersion ($deviceModel; $systemVersion; DeviceID: $deviceId)';
  102. }
  103. }