/**
 * 获取系统设备信息
 */


import {requestAndroidPermission} from "@/utils/permission";

export function getSystemDeviceInfo() {
  return new Promise((resolve, reject) => {
    // 返回结果
    let resolveResult = (deviceId, imei) => {
      resolve({
        deviceId,
        imei
      })
    };

    let sysInfo = uni.getSystemInfoSync();

    // #ifdef APP-PLUS
    if (sysInfo.osName.toLowerCase() == 'android' && sysInfo.osVersion >= 10) {
      plus.device.getOAID({
        success: ({oaid}) => {
          resolveResult(oaid);
        },
        fail: (e) => {
          resolveResult(sysInfo.deviceId);
        }
      });
    } else {
      requestAndroidPermission('android.permission.READ_PHONE_STATE').then(res => {
        plus.device.getInfo({
          success: (res) => {
            let {uuid, imei} = res
            let [oaid] = uuid?.split(",") || []
            resolveResult(oaid || sysInfo.deviceId, imei)
          },
          fail: (error) => {
            resolveResult(sysInfo.deviceId);
          },
        });
      }).catch((err) => {
        resolveResult(sysInfo.deviceId);
      });
    }
    // #endif

    // #ifndef APP-PLUS
    resolveResult(sysInfo.deviceId);
    // #endif
  });
}
