87 lines
2.2 KiB
Dart
87 lines
2.2 KiB
Dart
// ignore: avoid_web_libraries_in_flutter
|
|
import 'dart:html' as html;
|
|
|
|
/// Web 端设备信息:浏览器无法稳定获取品牌/型号,系统名和版本从 UA 解析。
|
|
Future<void> initializeDeviceInfo() async {}
|
|
|
|
String getDeviceType() {
|
|
try {
|
|
final ua = html.window.navigator.userAgent;
|
|
if (ua.contains('iPhone') || ua.contains('iPad') || ua.contains('iPod')) {
|
|
return 'iOS';
|
|
}
|
|
if (ua.contains('Android')) return 'Android';
|
|
} catch (_) {}
|
|
return 'PC';
|
|
}
|
|
|
|
String getSystemName() => _osInfo().name;
|
|
|
|
String getSystemVersion() => _osInfo().version;
|
|
|
|
String getDeviceBrand() => '';
|
|
|
|
String getDeviceModel() => '';
|
|
|
|
String getUserAgent() {
|
|
try {
|
|
return html.window.navigator.userAgent;
|
|
} catch (_) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
_OsInfo _osInfo() {
|
|
try {
|
|
return _parseOsInfo(html.window.navigator.userAgent);
|
|
} catch (_) {
|
|
return const _OsInfo('', '');
|
|
}
|
|
}
|
|
|
|
_OsInfo _parseOsInfo(String ua) {
|
|
if (ua.contains('iPhone') || ua.contains('iPad') || ua.contains('iPod')) {
|
|
final match =
|
|
RegExp(r'(?:CPU (?:iPhone )?OS|iPhone OS) ([\d_]+)').firstMatch(ua);
|
|
if (match != null) {
|
|
return _OsInfo('iOS', _normalizeVersion(match.group(1)));
|
|
}
|
|
return const _OsInfo('iOS', '');
|
|
}
|
|
|
|
if (ua.contains('Android')) {
|
|
final match = RegExp(r'Android ([\d.]+)').firstMatch(ua);
|
|
if (match != null) return _OsInfo('Android', match.group(1) ?? '');
|
|
return const _OsInfo('Android', '');
|
|
}
|
|
|
|
final macMatch = RegExp(r'Mac OS X ([^;)]+)').firstMatch(ua);
|
|
if (macMatch != null) {
|
|
return _OsInfo('macOS', _normalizeVersion(macMatch.group(1)));
|
|
}
|
|
|
|
final windowsMatch = RegExp(r'Windows NT ([\d.]+)').firstMatch(ua);
|
|
if (windowsMatch != null) {
|
|
return _OsInfo('Windows', windowsMatch.group(1) ?? '');
|
|
}
|
|
|
|
final chromeOsMatch = RegExp(r'CrOS [^ ]+ ([\d.]+)').firstMatch(ua);
|
|
if (chromeOsMatch != null) {
|
|
return _OsInfo('ChromeOS', chromeOsMatch.group(1) ?? '');
|
|
}
|
|
|
|
if (ua.contains('Linux')) return const _OsInfo('Linux', '');
|
|
return const _OsInfo('', '');
|
|
}
|
|
|
|
String _normalizeVersion(String? version) {
|
|
return (version ?? '').replaceAll('_', '.').trim();
|
|
}
|
|
|
|
class _OsInfo {
|
|
final String name;
|
|
final String version;
|
|
|
|
const _OsInfo(this.name, this.version);
|
|
}
|