getDeviceType.ts
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
interface DeviceInfo {
platform: 'android' | 'ios' | 'unknown';
}
export const getDeviceType = (): Promise<string> => {
return new Promise((resolve, reject) => {
let deviceType = uni.getStorageSync('deviceType');
if (deviceType) {
resolve(deviceType);
} else {
uni.getSystemInfo({
success: function(res: DeviceInfo) {
if (res.platform === 'android') {
uni.setStorageSync('deviceType', 'android');
resolve('android');
} else if (res.platform === 'ios') {
uni.setStorageSync('deviceType', 'ios');
resolve('ios');
} else {
uni.setStorageSync('deviceType', '未知');
reject(new Error('无法识别当前设备类型'));
}
},
fail: function(err: any) {
reject(err);
}
});
}
});
}
// 调用工具函数获取当前设备类型
// getDeviceType().then(deviceType => {
// console.log('当前设备类型是:', deviceType);
// }).catch(err => {
// console.error('获取设备类型失败:', err);
// });