From b23322d4e0337ce4dc50c6e99156494831b81c85 Mon Sep 17 00:00:00 2001 From: lizhongyi Date: Mon, 29 May 2023 20:08:19 +0800 Subject: [PATCH] =?UTF-8?q?uni-getbatteryinfo=E6=96=B0=E5=A2=9E=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E8=8E=B7=E5=8F=96=E7=94=B5=E9=87=8Fapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uni-getbatteryinfo/package.json | 2 +- .../utssdk/app-ios/index.uts | 15 +++++- .../uni-getbatteryinfo/utssdk/interface.uts | 46 +++++++++++++++++-- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/uni_modules/uni-getbatteryinfo/package.json b/uni_modules/uni-getbatteryinfo/package.json index 0731fe3..094b6ba 100644 --- a/uni_modules/uni-getbatteryinfo/package.json +++ b/uni_modules/uni-getbatteryinfo/package.json @@ -32,7 +32,7 @@ }, "uni_modules": { "uni-ext-api": { - "uni": ["getBatteryInfo"] + "uni": ["getBatteryInfo", "getBatteryInfoSync"] }, "dependencies": [], "encrypt": [], diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts index 755250f..4c0362d 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts @@ -20,4 +20,17 @@ export const getBatteryInfo : GetBatteryInfo = function (options) { }; options.success?.(res); options.complete?.(res); -} \ No newline at end of file +} + +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; +} diff --git a/uni_modules/uni-getbatteryinfo/utssdk/interface.uts b/uni_modules/uni-getbatteryinfo/utssdk/interface.uts index 7f58b1e..b62d62c 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/interface.uts +++ b/uni_modules/uni-getbatteryinfo/utssdk/interface.uts @@ -44,6 +44,17 @@ export type GetBatteryInfoOptions = { * 接口调用成功的回调 */ complete ?: (res : any) => void +} + +export type GetBatteryInfoResult = { + /** + * 设备电量,范围1 - 100 + */ + level : number, + /** + * 是否正在充电中 + */ + isCharging : boolean } /** @@ -58,8 +69,10 @@ export type GetBatteryInfoOptions = { * @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" }) */ -export type GetBatteryInfo = (options : GetBatteryInfoOptions) => void +export type GetBatteryInfo = (options : GetBatteryInfoOptions) => void + +export type GetBatteryInfoSync = () => GetBatteryInfoResult interface Uni { @@ -85,7 +98,7 @@ interface Uni { * "unixVer": "3.9.0" * }, * "ios": { - * "osVer": "8.0", + * "osVer": "9.0", * "uniVer": "3.6.11", * "unixVer": "3.9.0" * } @@ -94,6 +107,33 @@ interface Uni { * @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 -- GitLab