| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import 'dart:io';
- import 'package:device_info_plus/device_info_plus.dart';
- import 'package:package_info_plus/package_info_plus.dart';
- /// 设备工具类
- class DeviceUtils {
- static final DeviceInfoPlugin _deviceInfo = DeviceInfoPlugin();
- static PackageInfo? _packageInfo;
- /// 初始化(在应用启动时调用)
- static Future<void> init() async {
- _packageInfo = await PackageInfo.fromPlatform();
- }
- // ==================== 平台信息 ====================
- /// 是否为 Android 平台
- static bool get isAndroid => Platform.isAndroid;
- /// 是否为 iOS 平台
- static bool get isIOS => Platform.isIOS;
- /// 是否为 Web 平台
- static bool get isWeb => Platform.isWindows || Platform.isLinux || Platform.isMacOS;
- // ==================== 设备信息 ====================
- /// 获取设备 ID
- static Future<String?> getDeviceId() async {
- try {
- if (isAndroid) {
- final androidInfo = await _deviceInfo.androidInfo;
- return androidInfo.id;
- } else if (isIOS) {
- final iosInfo = await _deviceInfo.iosInfo;
- return iosInfo.identifierForVendor;
- }
- } catch (e) {
- return null;
- }
- return null;
- }
- /// 获取设备型号
- static Future<String?> getDeviceModel() async {
- try {
- if (isAndroid) {
- final androidInfo = await _deviceInfo.androidInfo;
- return androidInfo.model;
- } else if (isIOS) {
- final iosInfo = await _deviceInfo.iosInfo;
- return iosInfo.model;
- }
- } catch (e) {
- return null;
- }
- return null;
- }
- /// 获取设备品牌
- static Future<String?> getDeviceBrand() async {
- try {
- if (isAndroid) {
- final androidInfo = await _deviceInfo.androidInfo;
- return androidInfo.brand;
- } else if (isIOS) {
- final iosInfo = await _deviceInfo.iosInfo;
- return 'Apple';
- }
- } catch (e) {
- return null;
- }
- return null;
- }
- /// 获取系统版本
- static Future<String?> getSystemVersion() async {
- try {
- if (isAndroid) {
- final androidInfo = await _deviceInfo.androidInfo;
- return 'Android ${androidInfo.version.release}';
- } else if (isIOS) {
- final iosInfo = await _deviceInfo.iosInfo;
- return 'iOS ${iosInfo.systemVersion}';
- }
- } catch (e) {
- return null;
- }
- return null;
- }
- // ==================== 应用信息 ====================
- /// 获取应用版本号
- static String? get appVersion => _packageInfo?.version;
- /// 获取构建号
- static String? get buildNumber => _packageInfo?.buildNumber;
- /// 获取包名
- static String? get packageName => _packageInfo?.packageName;
- /// 获取应用名称
- static String? get appName => _packageInfo?.appName;
- /// 获取完整版本信息
- static String? get fullVersion =>
- _packageInfo != null
- ? '${_packageInfo!.version}+${_packageInfo!.buildNumber}'
- : null;
- // ==================== 设备标识 ====================
- /// 获取用户代理字符串
- static Future<String> getUserAgent() async {
- final deviceId = await getDeviceId() ?? 'unknown';
- final deviceModel = await getDeviceModel() ?? 'unknown';
- final systemVersion = await getSystemVersion() ?? 'unknown';
- final appVersion = DeviceUtils.appVersion ?? 'unknown';
-
- return 'SinoMedCloud/$appVersion ($deviceModel; $systemVersion; DeviceID: $deviceId)';
- }
- }
|