You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.8 KiB

import 'dart:io' show Platform;
import 'package:device_info_plus/device_info_plus.dart';
import 'package:device_marketing_names/device_marketing_names.dart';
import '../../core/utils/logger.dart';
/// 反馈系统上报到评论后台的设备信息。
class CommentDeviceInfo {
final String? brand;
final String? model;
final String? osType;
final String? osVersion;
const CommentDeviceInfo({
this.brand,
this.model,
this.osType,
this.osVersion,
});
}
/// 读取设备信息:`model` 优先取 `device_marketing_names` 的市售名,
/// 命中失败时退化为 "品牌 + 内部型号",不影响功能。
Future<CommentDeviceInfo> readCommentDeviceInfo() async {
try {
final plugin = DeviceInfoPlugin();
final marketingNames = DeviceMarketingNames();
if (Platform.isIOS) {
final info = await plugin.iosInfo;
final code = info.utsname.machine;
final marketing = await marketingNames.getSingleName();
return CommentDeviceInfo(
brand: 'Apple',
model: marketing.isNotEmpty ? marketing : code,
osType: 'ios',
osVersion: info.systemVersion,
);
}
if (Platform.isAndroid) {
final info = await plugin.androidInfo;
final brand = _capitalize(info.brand);
final marketing = await marketingNames.getSingleName();
final fallback = '$brand ${info.model}';
return CommentDeviceInfo(
brand: brand,
model: marketing.isNotEmpty ? marketing : fallback,
osType: 'android',
osVersion: info.version.release,
);
}
} catch (e) {
Logger.error('读取设备信息失败: $e');
}
return const CommentDeviceInfo();
}
String _capitalize(String s) =>
s.isEmpty ? s : s[0].toUpperCase() + s.substring(1).toLowerCase();