index.js 754 字节
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4
export default function getBatteryInfo(options) {
    if (navigator.getBattery) {
        navigator.getBattery().then(battery => {
            const res = {
5 6
				errCode: 0,
				errSubject: "uni-getBatteryInfo",
DCloud-yyl's avatar
DCloud-yyl 已提交
7 8 9 10 11 12 13 14 15
                errMsg: 'getBatteryInfo:ok',
                level: battery.level * 100,
                isCharging: battery.charging
            }
            options.success && options.success(res)
            options.complete && options.complete(res)
        })
    } else {
        const res = {
16 17
			errCode: 1002,
			errSubject: "uni-getBatteryInfo",
DCloud-yyl's avatar
DCloud-yyl 已提交
18 19 20 21 22 23
            errMsg: 'getBatteryInfo:fail navigator.getBattery is unsupported'
        }
        options.fail && options.fail(res)
        options.complete && options.complete(res)
    }
}