getDeviceType.ts 1.1 KB
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);
// });