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

uni-getBatteryInfo增加同步api

上级 5b734e04
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
}, },
"uni_modules": { "uni_modules": {
"uni-ext-api": { "uni-ext-api": {
"uni": ["getBatteryInfo"] "uni": ["getBatteryInfo", "getBatteryInfoSync"]
}, },
"dependencies": [], "dependencies": [],
"encrypt": [], "encrypt": [],
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import { UIDevice } from "UIKit"; import { UIDevice } from "UIKit";
import { Int } from 'Swift'; import { Int } from 'Swift';
import { GetBatteryInfo, GetBatteryInfoSuccess } from '../interface.uts'; import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoResult, GetBatteryInfoSync } from '../interface.uts';
/** /**
* 导出 获取电量方法 * 导出 获取电量方法
...@@ -20,4 +20,17 @@ export const getBatteryInfo : GetBatteryInfo = function (options) { ...@@ -20,4 +20,17 @@ export const getBatteryInfo : GetBatteryInfo = function (options) {
}; };
options.success?.(res); options.success?.(res);
options.complete?.(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;
} }
\ No newline at end of file
...@@ -44,6 +44,17 @@ export type GetBatteryInfoOptions = { ...@@ -44,6 +44,17 @@ export type GetBatteryInfoOptions = {
* 接口调用成功的回调 * 接口调用成功的回调
*/ */
complete ?: (res : any) => void complete ?: (res : any) => void
}
export type GetBatteryInfoResult = {
/**
* 设备电量,范围1 - 100
*/
level : number,
/**
* 是否正在充电中
*/
isCharging : boolean
} }
/** /**
...@@ -58,8 +69,10 @@ export type GetBatteryInfoOptions = { ...@@ -58,8 +69,10 @@ export type GetBatteryInfoOptions = {
* @assert () => success({errCode: 0, errSubject: "uni-getBatteryInfo", errMsg: "getBatteryInfo:ok", level: 60, isCharging: false }) * @assert () => success({errCode: 0, errSubject: "uni-getBatteryInfo", errMsg: "getBatteryInfo:ok", level: 60, isCharging: false })
* @assert () => fail({errCode: 1001, errSubject: "uni-getBatteryInfo", errMsg: "getBatteryInfo:fail getAppContext is null" }) * @assert () => fail({errCode: 1001, errSubject: "uni-getBatteryInfo", errMsg: "getBatteryInfo:fail getAppContext is null" })
*/ */
export type GetBatteryInfo = (options : GetBatteryInfoOptions) => void export type GetBatteryInfo = (options : GetBatteryInfoOptions) => void
export type GetBatteryInfoSync = () => GetBatteryInfoResult
interface Uni { interface Uni {
...@@ -85,7 +98,7 @@ interface Uni { ...@@ -85,7 +98,7 @@ interface Uni {
* "unixVer": "3.9.0" * "unixVer": "3.9.0"
* }, * },
* "ios": { * "ios": {
* "osVer": "8.0", * "osVer": "9.0",
* "uniVer": "3.6.11", * "uniVer": "3.6.11",
* "unixVer": "3.9.0" * "unixVer": "3.9.0"
* } * }
...@@ -94,6 +107,33 @@ interface Uni { ...@@ -94,6 +107,33 @@ interface Uni {
* @uniVueVersion 2,3 //支持的vue版本 * @uniVueVersion 2,3 //支持的vue版本
* *
*/ */
getBatteryInfo : GetBatteryInfo 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"
* }
* }
* }
* @uniVueVersion 2,3 //支持的vue版本
*
*/
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.
先完成此消息的编辑!
想要评论请 注册