index.uts 793 字节
Newer Older
1
// 引用 iOS 原生平台 api
DCloud-yyl's avatar
DCloud-yyl 已提交
2
import { UIDevice } from "UIKit";
3
import { Int } from 'Swift';
DCloud-yyl's avatar
DCloud-yyl 已提交
4

5 6 7
/**
 * 定义 接口参数
 */
DCloud-yyl's avatar
DCloud-yyl 已提交
8
type GetBatteryInfoOptions = {
9 10 11
    success?: (res: object) => void;
    fail?: (res: object) => void;
    complete?: (res: object) => void;
DCloud-yyl's avatar
DCloud-yyl 已提交
12 13
};

14 15 16
/**
 * 导出 获取电量方法 
 */
DCloud-yyl's avatar
DCloud-yyl 已提交
17 18
export default function getBatteryInfo(options: GetBatteryInfoOptions) {
	
19
	// 开启电量检测
DCloud-yyl's avatar
DCloud-yyl 已提交
20 21
	UIDevice.current.isBatteryMonitoringEnabled = true
	
22
	// 返回数据
DCloud-yyl's avatar
DCloud-yyl 已提交
23
    const res = {
24 25
		errCode: 0,
		errSubject: "uni-getBatteryInfo",
DCloud-yyl's avatar
DCloud-yyl 已提交
26
        errMsg: "getBatteryInfo:ok",
27
        level: new Int(UIDevice.current.batteryLevel * 100),
DCloud-yyl's avatar
DCloud-yyl 已提交
28 29 30 31
        isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
    };
    options.success?.(res);
    options.complete?.(res);
32
}