提交 b23322d4 编写于 作者: lizhongyi_'s avatar lizhongyi_

uni-getbatteryinfo新增同步获取电量api

上级 aa42bb75
......@@ -32,7 +32,7 @@
},
"uni_modules": {
"uni-ext-api": {
"uni": ["getBatteryInfo"]
"uni": ["getBatteryInfo", "getBatteryInfoSync"]
},
"dependencies": [],
"encrypt": [],
......
......@@ -21,3 +21,16 @@ export const getBatteryInfo : GetBatteryInfo = function (options) {
options.success?.(res);
options.complete?.(res);
}
export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult {
// 开启电量检测
UIDevice.current.isBatteryMonitoringEnabled = true
// 返回数据
const res : GetBatteryInfoResult = {
level: Number(UIDevice.current.batteryLevel * 100),
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
};
return res;
}
......@@ -46,6 +46,17 @@ export type GetBatteryInfoOptions = {
complete ?: (res : any) => void
}
export type GetBatteryInfoResult = {
/**
* 设备电量,范围1 - 100
*/
level : number,
/**
* 是否正在充电中
*/
isCharging : boolean
}
/**
* 获取电量信息
* @param {GetBatteryInfoOptions} options
......@@ -61,6 +72,8 @@ export type GetBatteryInfoOptions = {
export type GetBatteryInfo = (options : GetBatteryInfoOptions) => void
export type GetBatteryInfoSync = () => GetBatteryInfoResult
interface Uni {
/**
......@@ -85,7 +98,34 @@ interface Uni {
* "unixVer": "3.9.0"
* },
* "ios": {
* "osVer": "8.0",
* "osVer": "9.0",
* "uniVer": "3.6.11",
* "unixVer": "3.9.0"
* }
* }
* }
* @uniVueVersion 2,3 //支持的vue版本
*
*/
getBatteryInfo : GetBatteryInfo,
/**
* 同步获取电池电量信息
* @description 获取电池电量信息
* @example
* ```typescript
* uni.getBatteryInfo()
* ```
* @remark
* - 该接口需要同步调用
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "4.4.4",
* "uniVer": "3.6.11",
* "unixVer": "3.9.0"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "3.6.11",
* "unixVer": "3.9.0"
* }
......@@ -94,6 +134,6 @@ interface Uni {
* @uniVueVersion 2,3 //支持的vue版本
*
*/
getBatteryInfo : GetBatteryInfo
getBatteryInfoSync: GetBatteryInfoSync
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册