diff --git a/app/index/view/mobile/public/header.html b/app/index/view/mobile/public/header.html index 46aad917..abe27cac 100644 --- a/app/index/view/mobile/public/header.html +++ b/app/index/view/mobile/public/header.html @@ -107,20 +107,73 @@ $('.top-country .close-icon').click(function(){ $(".mask,.action-sheet").hide(); }) - var sUserAgent = navigator.userAgent.toLowerCase(); - var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";//判断是否为iPad - var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";//判断是否为iPhone用户 - var bIsMidp = sUserAgent.match(/midp/i) == "midp"; - var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; - var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb"; - var bIsAndroid = sUserAgent.match(/android/i) == "android"; - var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"; - var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile"; - - if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM)) { - alert("当前是电脑打开") - }else{ - alert("当前是手机打开") -} + +// 缓存检测结果 + let _deviceType = null; + + /** + * 检测当前设备类型 + * @returns {'mobile'|'tablet'|'desktop'} + */ + function detectDevice() { + if (_deviceType) return _deviceType; + + const isTouch = 'ontouchstart' in window || navigator.maxTouchPoints > 0; + const screenWidth = window.innerWidth || document.documentElement.clientWidth; + const userAgent = navigator.userAgent.toLowerCase(); + + const isTablet = + userAgent.includes('tablet') || + (userAgent.includes('android') && !userAgent.includes('mobile')) || + userAgent.includes('ipad') || + userAgent.includes('kindle'); + + _deviceType = + isTouch && (screenWidth < 768 || !isTablet) ? 'mobile' : + isTouch && screenWidth >= 768 ? 'tablet' : 'desktop'; + + return _deviceType; + } + + /** + * 显示设备类型提示 + */ + function showDeviceAlert() { + const device = detectDevice(); + const message = `您正在使用${ + device === 'mobile' ? '手机' : + device === 'tablet' ? '平板' : '电脑' + }访问本页面`; + + // 使用原生alert + alert(message); + + // 或者使用自定义样式的提示(需要引入相关CSS) + /* + $('
' + message + '
') + .css({ + position: 'fixed', + top: '10px', + right: '10px', + padding: '10px 15px', + background: '#333', + color: 'white', + borderRadius: '5px', + zIndex: '9999' + }) + .appendTo('body') + .fadeIn() + .delay(3000) + .fadeOut(function() { + $(this).remove(); + }); + */ + } + + // 页面加载完成后自动检测并提示 + $(document).ready(showDeviceAlert); + + // 暴露检测方法供外部使用 + $.detectDevice = detectDevice; }) \ No newline at end of file diff --git a/app/index/view/pc/public/header.html b/app/index/view/pc/public/header.html index b9738257..abda5fd7 100644 --- a/app/index/view/pc/public/header.html +++ b/app/index/view/pc/public/header.html @@ -201,21 +201,72 @@ $('.closecountrybt').click(function () { $('#top-country').hide(); }); - - var sUserAgent = navigator.userAgent.toLowerCase(); - var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";//判断是否为iPad - var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";//判断是否为iPhone用户 - var bIsMidp = sUserAgent.match(/midp/i) == "midp"; - var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; - var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb"; - var bIsAndroid = sUserAgent.match(/android/i) == "android"; - var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"; - var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile"; - - if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM)) { - alert("当前是电脑打开") - }else{ - alert("当前是手机打开") -} +// 缓存检测结果 + let _deviceType = null; + + /** + * 检测当前设备类型 + * @returns {'mobile'|'tablet'|'desktop'} + */ + function detectDevice() { + if (_deviceType) return _deviceType; + + const isTouch = 'ontouchstart' in window || navigator.maxTouchPoints > 0; + const screenWidth = window.innerWidth || document.documentElement.clientWidth; + const userAgent = navigator.userAgent.toLowerCase(); + + const isTablet = + userAgent.includes('tablet') || + (userAgent.includes('android') && !userAgent.includes('mobile')) || + userAgent.includes('ipad') || + userAgent.includes('kindle'); + + _deviceType = + isTouch && (screenWidth < 768 || !isTablet) ? 'mobile' : + isTouch && screenWidth >= 768 ? 'tablet' : 'desktop'; + + return _deviceType; + } + + /** + * 显示设备类型提示 + */ + function showDeviceAlert() { + const device = detectDevice(); + const message = `您正在使用${ + device === 'mobile' ? '手机' : + device === 'tablet' ? '平板' : '电脑' + }访问本页面`; + + // 使用原生alert + alert(message); + + // 或者使用自定义样式的提示(需要引入相关CSS) + /* + $('
' + message + '
') + .css({ + position: 'fixed', + top: '10px', + right: '10px', + padding: '10px 15px', + background: '#333', + color: 'white', + borderRadius: '5px', + zIndex: '9999' + }) + .appendTo('body') + .fadeIn() + .delay(3000) + .fadeOut(function() { + $(this).remove(); + }); + */ + } + + // 页面加载完成后自动检测并提示 + $(document).ready(showDeviceAlert); + + // 暴露检测方法供外部使用 + $.detectDevice = detectDevice; }); \ No newline at end of file