Jelajahi Sumber

[new]增加设备信息获取

zk 1 tahun lalu
induk
melakukan
f6251b87d8

+ 21 - 7
lib/base/base_request.dart

@@ -1,7 +1,8 @@
 import 'dart:io';
 
-import 'package:electronic_assistant/utils/android_device_info.dart';
+import 'package:electronic_assistant/device/atmob_platform_info.dart';
 import 'package:electronic_assistant/utils/app_info_util.dart';
+import 'package:electronic_assistant/device/device_info_util.dart';
 import 'package:electronic_assistant/utils/mmkv_util.dart';
 import 'package:flutter/cupertino.dart';
 import 'package:json_annotation/json_annotation.dart';
@@ -48,6 +49,8 @@ class BaseRequest {
   String? simImei0;
   @JsonKey(name: "simImei1")
   String? simImei1;
+  @JsonKey(name: "mac")
+  String? mac;
 
   /// iOS特有
   @JsonKey(name: "idfa")
@@ -57,13 +60,11 @@ class BaseRequest {
 
   /// 公共设备信息
   @JsonKey(name: "machineId")
-  String? machineId;
+  String? machineId; //给web使用
   @JsonKey(name: "brand")
   String? brand;
   @JsonKey(name: "model")
   String? model;
-  @JsonKey(name: "mac")
-  String? mac;
   @JsonKey(name: "wifiName")
   String? wifiName;
 
@@ -116,8 +117,21 @@ class BaseRequest {
   }
 
   void initDeviceInfo() {
-    if (Platform.isAndroid) {
-      androidId = androidDeviceInfo.androidId;
-    } else if (Platform.isIOS) {}
+    oaid = atmobPlatformInfo.oaid;
+    aaid = atmobPlatformInfo.aaid;
+    androidId = atmobPlatformInfo.androidId;
+    imei = atmobPlatformInfo.imei;
+    simImei0 = atmobPlatformInfo.simImei0;
+    simImei1 = atmobPlatformInfo.simImei1;
+    mac = atmobPlatformInfo.mac;
+    idfa = atmobPlatformInfo.idfa;
+    idfv = atmobPlatformInfo.idfv;
+    machineId = atmobPlatformInfo.machineId;
+    brand = atmobPlatformInfo.brand;
+    model = atmobPlatformInfo.model;
+    wifiName = atmobPlatformInfo.wifiName;
+    region = atmobPlatformInfo.region;
+    locLng = atmobPlatformInfo.locLng;
+    locLat = atmobPlatformInfo.locLat;
   }
 }

+ 142 - 0
lib/device/atmob_platform_info.dart

@@ -0,0 +1,142 @@
+class AtmobPlatformInfo {
+  AtmobPlatformInfo();
+
+  /// 设备信息
+  /// Android特有
+  String? _oaid;
+  String? _aaid;
+  String? _androidId;
+  String? _imei;
+  String? _simImei0;
+  String? _simImei1;
+  String? _mac;
+
+  /// iOS特有
+  String? _idfa;
+  String? _idfv;
+
+  /// 公共设备信息
+  String? _machineId; //给web使用
+  String? _brand;
+  String? _model;
+  String? _wifiName;
+
+  /// 地理位置信息
+  String? _region;
+  double? _locLng;
+  double? _locLat;
+
+  AtmobPlatformInfo setOaid(String? oaid) {
+    _oaid = oaid;
+    return this;
+  }
+
+  AtmobPlatformInfo setAaid(String? aaid) {
+    _aaid = aaid;
+    return this;
+  }
+
+  AtmobPlatformInfo setAndroidId(String? androidId) {
+    _androidId = androidId;
+    return this;
+  }
+
+  AtmobPlatformInfo setImei(String? imei) {
+    _imei = imei;
+    return this;
+  }
+
+  AtmobPlatformInfo setSimImei0(String? simImei0) {
+    _simImei0 = simImei0;
+    return this;
+  }
+
+  AtmobPlatformInfo setSimImei1(String? simImei1) {
+    _simImei1 = simImei1;
+    return this;
+  }
+
+  AtmobPlatformInfo setMac(String? mac) {
+    _mac = mac;
+    return this;
+  }
+
+  AtmobPlatformInfo setIdfa(String? idfa) {
+    _idfa = idfa;
+    return this;
+  }
+
+  AtmobPlatformInfo setIdfv(String? idfv) {
+    _idfv = idfv;
+    return this;
+  }
+
+  AtmobPlatformInfo setMachineId(String? machineId) {
+    _machineId = machineId;
+    return this;
+  }
+
+  AtmobPlatformInfo setBrand(String? brand) {
+    _brand = brand;
+    return this;
+  }
+
+  AtmobPlatformInfo setModel(String? model) {
+    _model = model;
+    return this;
+  }
+
+  AtmobPlatformInfo setWifiName(String? wifiName) {
+    _wifiName = wifiName;
+    return this;
+  }
+
+  AtmobPlatformInfo setRegion(String? region) {
+    _region = region;
+    return this;
+  }
+
+  AtmobPlatformInfo setLocLng(double? locLng) {
+    _locLng = locLng;
+    return this;
+  }
+
+  AtmobPlatformInfo setLocLat(double? locLat) {
+    _locLat = locLat;
+    return this;
+  }
+
+  double? get locLat => _locLat;
+
+  double? get locLng => _locLng;
+
+  String? get region => _region;
+
+  String? get wifiName => _wifiName;
+
+  String? get model => _model;
+
+  String? get brand => _brand;
+
+  String? get machineId => _machineId;
+
+  String? get idfv => _idfv;
+
+  String? get idfa => _idfa;
+
+  String? get mac => _mac;
+
+  String? get simImei1 => _simImei1;
+
+  String? get simImei0 => _simImei0;
+
+  String? get imei => _imei;
+
+  String? get androidId => _androidId;
+
+  String? get aaid => _aaid;
+
+  String? get oaid => _oaid;
+}
+
+final AtmobPlatformInfo atmobPlatformInfo = AtmobPlatformInfo();

+ 20 - 0
lib/device/device_info_util.dart

@@ -0,0 +1,20 @@
+import 'package:electronic_assistant/device/platform_ios_info.dart';
+
+import 'platform_android_info.dart';
+import '../utils/common_utils.dart';
+
+class DeviceInfoUtil {
+  DeviceInfoUtil._();
+
+  final String privacyPolicyKey = stringToUnicode('privacyPolicyKey');
+
+  init() async {
+    // if (!KVUtil.getBool(privacyPolicyKey, false)) {
+    //   return;
+    // }
+    await PlatformAndroidInfo().init();
+    await PlatformIosInfo().init();
+  }
+}
+
+final deviceInfoUtil = DeviceInfoUtil._();

+ 19 - 0
lib/device/platform_android_info.dart

@@ -0,0 +1,19 @@
+import 'dart:io';
+
+import 'package:device_info_plus/device_info_plus.dart';
+import 'package:electronic_assistant/device/atmob_platform_info.dart';
+import 'package:custom_platform_device_id/platform_device_id.dart';
+
+class PlatformAndroidInfo {
+  init() async {
+    if (Platform.isAndroid) {
+      DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
+      AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
+      String? deviceId = await PlatformDeviceId.getDeviceId;
+      atmobPlatformInfo
+          .setAndroidId(deviceId)
+          .setBrand(androidInfo.brand)
+          .setModel(androidInfo.model);
+    }
+  }
+}

+ 17 - 0
lib/device/platform_ios_info.dart

@@ -0,0 +1,17 @@
+import 'dart:io';
+
+import 'package:device_info_plus/device_info_plus.dart';
+
+import 'atmob_platform_info.dart';
+
+class PlatformIosInfo {
+  init() async {
+    if (Platform.isIOS) {
+      DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
+      IosDeviceInfo iosInfo = await deviceInfoPlugin.iosInfo;
+      atmobPlatformInfo
+          .setModel(iosInfo.model)
+          .setIdfv(iosInfo.identifierForVendor);
+    }
+  }
+}

+ 5 - 2
lib/main.dart

@@ -5,6 +5,7 @@ import 'package:electronic_assistant/resource/string.gen.dart';
 import 'package:electronic_assistant/resource/string_source.dart';
 import 'package:electronic_assistant/router/app_pages.dart';
 import 'package:electronic_assistant/utils/app_info_util.dart';
+import 'package:electronic_assistant/device/device_info_util.dart';
 import 'package:electronic_assistant/utils/mmkv_util.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter_foreground_task/flutter_foreground_task.dart';
@@ -21,10 +22,12 @@ void main() async {
 
   //全局配置smartDialog
   smartConfig();
-  //获取包信息
-  await appInfoUtil.init();
   //mmkv
   await KVUtil.init();
+  //获取包信息
+  await appInfoUtil.init();
+  //获取设备信息
+  await deviceInfoUtil.init();
 
   runApp(const MyApp());
 }

+ 0 - 30
lib/utils/android_device_info.dart

@@ -1,30 +0,0 @@
-import 'package:electronic_assistant/utils/common_utils.dart';
-import 'package:electronic_assistant/utils/mmkv_util.dart';
-import 'package:uuid/uuid.dart';
-
-class AndroidDeviceInfo {
-  String? _androidId;
-
-  AndroidDeviceInfo._() {
-    _androidId = KVUtil.getString(AndroidConstants.keyAndroidIdCache, null);
-  }
-
-  String? get androidId {
-    if (_androidId != null && _androidId!.isNotEmpty) {
-      return _androidId;
-    }
-    _androidId = getAndroidId();
-    KVUtil.putString(AndroidConstants.keyAndroidIdCache, _androidId);
-    return _androidId;
-  }
-
-  String getAndroidId() {
-    return const Uuid().v4();
-  }
-}
-
-class AndroidConstants {
-  static final String keyAndroidIdCache = stringToUnicode("keyAndroidIdCache");
-}
-
-final androidDeviceInfo = AndroidDeviceInfo._();

+ 0 - 1
lib/utils/device_info_util.dart

@@ -1 +0,0 @@
-class DeviceInfo {}

+ 4 - 0
pubspec.yaml

@@ -83,6 +83,10 @@ dependencies:
   #前台任务
   flutter_foreground_task: ^8.8.1+1
 
+  #获取设备信息
+  device_info_plus: ^10.1.2
+  custom_platform_device_id: ^1.0.8
+
 dev_dependencies:
   flutter_test:
     sdk: flutter