From ac91b49ba48007080b7666b6c0d16a8ce26c21be Mon Sep 17 00:00:00 2001 From: lq Date: Tue, 3 Dec 2024 16:35:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E8=BE=85?= =?UTF-8?q?=E5=8A=A9=E5=87=BD=E6=95=B0=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helpers/helpers.php | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/helpers/helpers.php b/helpers/helpers.php index 29eb154..a5715dd 100644 --- a/helpers/helpers.php +++ b/helpers/helpers.php @@ -26,4 +26,68 @@ if (!function_exists('ase_decode')) { { return base64_decode(substr(base64_decode($data), 30)); } +} + +// 获取用户设备类型 +if (!function_exists('get_device_type')) { + function get_device_type(): array + { + // 获取HTTP请求头中的User-Agent字段 + $userAgent = $_SERVER['HTTP_USER_AGENT']; + // 初始化设备信息数组 + $deviceInfo = [ + 'isMobile' => false, + 'isTablet' => false, + 'deviceType' => 'Desktop', + 'browser' => '', + ]; + + // 检查是否为移动设备 + if (preg_match('/android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $userAgent)) { + $deviceInfo['isMobile'] = true; + $deviceInfo['deviceType'] = 'Mobile'; + } + + // 检查是否为平板设备 + if (preg_match('/tablet|ipad|playbook|silk/i', $userAgent)) { + $deviceInfo['isTablet'] = true; + $deviceInfo['deviceType'] = 'Tablet'; + } + + // 确定浏览器类型 + if (strpos($userAgent, 'Firefox') !== false) { + $deviceInfo['browser'] = 'Mozilla Firefox'; + } elseif (strpos($userAgent, 'Opera Mini') !== false || strpos($userAgent, 'Opera') !== false) { + $deviceInfo['browser'] = 'Opera'; + } elseif (strpos($userAgent, 'Chrome') !== false) { + $deviceInfo['browser'] = 'Google Chrome'; + } elseif (strpos($userAgent, 'Safari') !== false && strpos($userAgent, 'Version/') !== false) { + $deviceInfo['browser'] = 'Apple Safari'; + } elseif (strpos($userAgent, 'MSIE') !== false || strpos($userAgent, 'Trident/7') !== false) { + $deviceInfo['browser'] = 'Internet Explorer'; + } + + // 确定操作系统平台 + if (strpos($userAgent, 'Windows NT 10.0') !== false) { + $deviceInfo['platform'] = 'Windows 10'; + } elseif (strpos($userAgent, 'Windows NT 6.3') !== false) { + $deviceInfo['platform'] = 'Windows 8.1'; + } elseif (strpos($userAgent, 'Windows NT 6.2') !== false) { + $deviceInfo['platform'] = 'Windows 8'; + } elseif (strpos($userAgent, 'Windows NT 6.1') !== false) { + $deviceInfo['platform'] = 'Windows 7'; + } elseif (strpos($userAgent, 'Macintosh; Intel Mac OS X') !== false) { + $deviceInfo['platform'] = 'Mac OS X'; + } elseif (strpos($userAgent, 'iPad') !== false) { + $deviceInfo['platform'] = 'iOS'; + } elseif (strpos($userAgent, 'iPhone') !== false) { + $deviceInfo['platform'] = 'iOS'; + } elseif (strpos($userAgent, 'Android') !== false) { + $deviceInfo['platform'] = 'Android'; + } else { + $deviceInfo['platform'] = '未知'; + } + + return $deviceInfo; + } } \ No newline at end of file