index.uts 759 B

1234567891011121314151617181920
  1. import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess } from '../interface.uts'
  2. export const getBatteryInfo: GetBatteryInfo = function (options: GetBatteryInfoOptions) {
  3. if (navigator.getBattery) {
  4. navigator.getBattery().then(battery => {
  5. const res = {
  6. errCode: 0,
  7. errSubject: "uni-getBatteryInfo",
  8. errMsg: 'getBatteryInfo:ok',
  9. level: battery.level * 100,
  10. isCharging: battery.charging
  11. } as GetBatteryInfoSuccess
  12. options.success && options.success(res)
  13. options.complete && options.complete(res)
  14. })
  15. } else {
  16. const res = new UniError("uni-getBatteryInfo", 1002, "getBatteryInfo:fail navigator.getBattery is unsupported")
  17. options.fail && options.fail(res)
  18. options.complete && options.complete(res)
  19. }
  20. }