|
1 | 1 | import { Ref, ref } from 'vue';
|
2 |
| -import DeviceDetector from 'device-detector-js'; |
3 | 2 |
|
4 | 3 | type osType = 'linux' | 'windows' | 'apple' | 'other';
|
5 | 4 |
|
6 |
| -const deviceDetector = new DeviceDetector(); |
7 | 5 |
|
8 |
| -export const useDetectDeviceType = () => { |
9 |
| - const deviceType = ref<osType>('other'); |
10 |
| - const useAgent = navigator.userAgent; |
11 |
| - const name = deviceDetector.parse(useAgent).os?.name; |
12 |
| - if (name === 'Mac') { |
13 |
| - deviceType.value = 'apple'; |
14 |
| - } else if (name?.includes('Windows')) { |
15 |
| - deviceType.value = 'windows'; |
16 |
| - } else if (name?.includes('Linux') || name?.includes('Debian') || name?.includes('Ubuntu')) { |
17 |
| - deviceType.value = 'linux'; |
| 6 | + |
| 7 | +function getOS(): osType { |
| 8 | + const userAgent = window.navigator.userAgent, |
| 9 | + |
| 10 | + platform = (window.navigator as any)?.userAgentData?.platform || window.navigator.platform, |
| 11 | + macosPlatforms = ['macOS', 'Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'], |
| 12 | + windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'], |
| 13 | + iosPlatforms = ['iPhone', 'iPad', 'iPod']; |
| 14 | + let os:osType = 'other'; |
| 15 | + |
| 16 | + if (macosPlatforms.indexOf(platform) !== -1) { |
| 17 | + os = 'apple'; |
| 18 | + } else if (iosPlatforms.indexOf(platform) !== -1) { |
| 19 | + os = 'other'; |
| 20 | + } else if (windowsPlatforms.indexOf(platform) !== -1) { |
| 21 | + os = 'windows'; |
| 22 | + } else if (/Android/.test(userAgent)) { |
| 23 | + os = 'other'; |
| 24 | + } else if (/Linux/.test(platform)) { |
| 25 | + os = 'linux'; |
18 | 26 | }
|
| 27 | + |
| 28 | + return os; |
| 29 | +} |
| 30 | + |
| 31 | +export const useDetectDeviceType = () => { |
| 32 | + const deviceType = ref<osType>(getOS()); |
19 | 33 | return deviceType;
|
20 | 34 | };
|
0 commit comments