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