feat: 添加用户行为分析功能
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
class Env {
|
||||
static String get apiUrl {
|
||||
@@ -14,13 +16,33 @@ class Env {
|
||||
return 'http://localhost:5775';
|
||||
}
|
||||
|
||||
static String get analyticsEndpoint => '$apiUrl/api/v1/analytics/events';
|
||||
|
||||
static String version = '0.1.0';
|
||||
static int build = 1;
|
||||
static String deviceId = '';
|
||||
|
||||
static Future<void> init() async {
|
||||
final info = await PackageInfo.fromPlatform();
|
||||
version = info.version;
|
||||
final buildStr = info.buildNumber.isEmpty ? '1' : info.buildNumber;
|
||||
build = int.tryParse(buildStr) ?? 1;
|
||||
|
||||
deviceId = await _getOrCreateDeviceId();
|
||||
}
|
||||
|
||||
static Future<String> _getOrCreateDeviceId() async {
|
||||
const storage = FlutterSecureStorage();
|
||||
var deviceId = await storage.read(key: 'device_id');
|
||||
if (deviceId == null || deviceId.isEmpty) {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
deviceId = prefs.getString('device_id') ?? '';
|
||||
if (deviceId.isEmpty) {
|
||||
deviceId = 'install_${DateTime.now().millisecondsSinceEpoch}';
|
||||
await prefs.setString('device_id', deviceId);
|
||||
}
|
||||
await storage.write(key: 'device_id', value: deviceId);
|
||||
}
|
||||
return deviceId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user