index.uts 743 字节
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 24
    const res = {
        errMsg: "getBatteryInfo:ok",
25
        level: new Int(UIDevice.current.batteryLevel * 100),
DCloud-yyl's avatar
DCloud-yyl 已提交
26 27 28 29
        isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
    };
    options.success?.(res);
    options.complete?.(res);
30
}