From 6c8538df94afe05960128f35377f89dcc964abe9 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Tue, 14 Mar 2023 16:21:55 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=B5=E9=87=8F=EF=BC=9A=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uni-getbatteryinfo/package.json | 2 +- .../utssdk/app-android/index.uts | 74 +++++----- .../utssdk/app-ios/index.uts | 55 ++++---- .../uni-getbatteryinfo/utssdk/interface.uts | 131 +++++++++--------- .../utssdk/mp-alipay/index.js | 2 +- .../utssdk/mp-baidu/index.js | 2 +- .../uni-getbatteryinfo/utssdk/mp-qq/index.js | 2 +- .../utssdk/mp-weixin/index.js | 2 +- .../uni-getbatteryinfo/utssdk/web/index.js | 2 +- 9 files changed, 130 insertions(+), 142 deletions(-) diff --git a/uni_modules/uni-getbatteryinfo/package.json b/uni_modules/uni-getbatteryinfo/package.json index 0eb0aac..0731fe3 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"] }, "dependencies": [], "encrypt": [], diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts index 2b7f0f7..28e2a9f 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts @@ -1,39 +1,35 @@ -import Context from "android.content.Context"; -import BatteryManager from "android.os.BatteryManager"; -import { UTSAndroid } from "io.dcloud.uts"; - - -type GetBatteryInfoOptions = { - success?: (res: UTSJSONObject) => void - fail?: (res: UTSJSONObject) => void - complete?: (res: UTSJSONObject) => void -} - -export default function getBatteryInfo(options: GetBatteryInfoOptions) { - const context = UTSAndroid.getAppContext(); - if (context != null) { - const manager = context.getSystemService( - Context.BATTERY_SERVICE - ) as BatteryManager; - const level = manager.getIntProperty( - BatteryManager.BATTERY_PROPERTY_CAPACITY - ); - const res = { - errCode: 0, - errSubject: "uni-getBatteryInfo", - errMsg: 'getBatteryInfo:ok', - level, - isCharging: manager.isCharging() - } - options.success?.(res) - options.complete?.(res) - } else { - const res = { - errSubject: "uni-getBatteryInfo", - errCode: 1001, - errMsg: 'getBatteryInfo:fail getAppContext is null' - } - options.fail?.(res) - options.complete?.(res) - } -} +import Context from "android.content.Context"; +import BatteryManager from "android.os.BatteryManager"; +import { UTSAndroid } from "io.dcloud.uts"; + +import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoFail, GetBatteryInfoOptions } from '../interface.uts' + +export const getBatteryInfo : GetBatteryInfo = function (options) { + const context = UTSAndroid.getAppContext(); + if (context != null) { + const manager = context.getSystemService( + Context.BATTERY_SERVICE + ) as BatteryManager; + const level = manager.getIntProperty( + BatteryManager.BATTERY_PROPERTY_CAPACITY + ); + const res : GetBatteryInfoSuccess = { + errCode: 0, + errSubject: "uni-getBatteryInfo", + errMsg: 'getBatteryInfo:ok', + level, + isCharging: manager.isCharging() + } + options.success?.(res) + options.complete?.(res) + } else { + const res : GetBatteryInfoFail = { + errSubject: "uni-getBatteryInfo", + errCode: 1001, + errMsg: 'getBatteryInfo:fail getAppContext is null', + cause: null + } + options.fail?.(res) + options.complete?.(res) + } +} \ No newline at end of file diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts index 8652189..8813af8 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts @@ -1,32 +1,25 @@ -// 引用 iOS 原生平台 api -import { UIDevice } from "UIKit"; -import { Int } from 'Swift'; - -/** - * 定义 接口参数 - */ -type GetBatteryInfoOptions = { - success?: (res: object) => void; - fail?: (res: object) => void; - complete?: (res: object) => void; -}; - -/** - * 导出 获取电量方法 - */ -export default function getBatteryInfo(options: GetBatteryInfoOptions) { - - // 开启电量检测 - UIDevice.current.isBatteryMonitoringEnabled = true - - // 返回数据 - const res = { - errCode: 0, - errSubject: "uni-getBatteryInfo", - errMsg: "getBatteryInfo:ok", - level: new Int(UIDevice.current.batteryLevel * 100), - isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging, - }; - options.success?.(res); - options.complete?.(res); +// 引用 iOS 原生平台 api +import { UIDevice } from "UIKit"; +import { Int } from 'Swift'; + +import { GetBatteryInfo, GetBatteryInfoSuccess } from '../interface.uts'; + +/** + * 导出 获取电量方法 + */ +export const getBatteryInfo : GetBatteryInfo = function (options) { + + // 开启电量检测 + UIDevice.current.isBatteryMonitoringEnabled = true + + // 返回数据 + const res : GetBatteryInfoSuccess = { + errCode: 0, + errSubject: "uni-getBatteryInfo", + errMsg: "getBatteryInfo:ok", + level: new Int(UIDevice.current.batteryLevel * 100), + isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging, + }; + options.success?.(res); + options.complete?.(res); } \ No newline at end of file diff --git a/uni_modules/uni-getbatteryinfo/utssdk/interface.uts b/uni_modules/uni-getbatteryinfo/utssdk/interface.uts index 975fc6a..6e0d246 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/interface.uts +++ b/uni_modules/uni-getbatteryinfo/utssdk/interface.uts @@ -1,76 +1,75 @@ -type GetBatteryInfoResult = { - /** - * 错误码 - * 0:成功 - * 1001:getAppContext is null - * 1002:navigator.getBattery is unsupported - */ - errCode : number, - /** - * 调用API的名称 - */ - errSubject : string, - /** - * 错误的详细信息 - */ - errMsg : string, - /** - * 设备电量,范围1 - 100 - */ - level : number, - /** - * 是否正在充电中 - */ - isCharging : boolean +export type GetBatteryInfoSuccess = { + /** + * 错误码 + * 0:成功 + * 1001:getAppContext is null + * 1002:navigator.getBattery is unsupported + */ + errCode : number, + /** + * 调用API的名称 + */ + errSubject : string, + /** + * 错误的详细信息 + */ + errMsg : string, + /** + * 设备电量,范围1 - 100 + */ + level : number, + /** + * 是否正在充电中 + */ + isCharging : boolean } -interface UniError { - /** - * 错误码 - */ - errCode : number, - /** - * 调用API的名称 - */ - errSubject : string, - /** - * 错误的详细信息 - */ - errMsg : string, - /** - * 错误来源 - */ - cause : any +export type GetBatteryInfoFail = { + /** + * 错误码 + */ + errCode : number, + /** + * 调用API的名称 + */ + errSubject : string, + /** + * 错误的详细信息 + */ + errMsg : string, + /** + * 错误来源 + */ + cause : any | null } export type GetBatteryInfoOptions = { - /** - * 接口调用结束的回调函数(调用成功、失败都会执行) - */ - success ?: (res : GetBatteryInfoResult) => void - /** - * 接口调用失败的回调函数 - */ - fail ?: (res : UniError) => void - /** - * 接口调用成功的回调 - */ - complete ?: (res : object) => void + /** + * 接口调用结束的回调函数(调用成功、失败都会执行) + */ + success ?: (res : GetBatteryInfoSuccess) => void + /** + * 接口调用失败的回调函数 + */ + fail ?: (res : GetBatteryInfoFail) => void + /** + * 接口调用成功的回调 + */ + complete ?: (res : any) => void } /** - * 获取电量信息 - * @param {GetBatteryInfoOptions} options - * - * - * @tutorial https://uniapp.dcloud.net.cn/api/system/batteryInfo.html - * @platforms APP-IOS = ^9.0,APP-ANDROID = ^22 - * @since 3.6.11 - * - * @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 default function getBatteryInfo(options : GetBatteryInfoOptions) : void - +* 获取电量信息 +* @param {GetBatteryInfoOptions} options +* +* +* @tutorial https://uniapp.dcloud.net.cn/api/system/batteryInfo.html +* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22 +* @since 3.6.11 +* +* @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 \ No newline at end of file diff --git a/uni_modules/uni-getbatteryinfo/utssdk/mp-alipay/index.js b/uni_modules/uni-getbatteryinfo/utssdk/mp-alipay/index.js index d40f4e5..4fdced2 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/mp-alipay/index.js +++ b/uni_modules/uni-getbatteryinfo/utssdk/mp-alipay/index.js @@ -1,3 +1,3 @@ -export default function getBatteryInfo(options) { +export function getBatteryInfo(options) { return my.getBatteryInfo(options) } diff --git a/uni_modules/uni-getbatteryinfo/utssdk/mp-baidu/index.js b/uni_modules/uni-getbatteryinfo/utssdk/mp-baidu/index.js index d727cd4..5944a77 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/mp-baidu/index.js +++ b/uni_modules/uni-getbatteryinfo/utssdk/mp-baidu/index.js @@ -1,3 +1,3 @@ -export default function getBatteryInfo(options) { +export function getBatteryInfo(options) { return swan.getBatteryInfo(options) } diff --git a/uni_modules/uni-getbatteryinfo/utssdk/mp-qq/index.js b/uni_modules/uni-getbatteryinfo/utssdk/mp-qq/index.js index 8ca0071..06287bc 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/mp-qq/index.js +++ b/uni_modules/uni-getbatteryinfo/utssdk/mp-qq/index.js @@ -1,3 +1,3 @@ -export default function getBatteryInfo(options) { +export function getBatteryInfo(options) { return qq.getBatteryInfo(options) } diff --git a/uni_modules/uni-getbatteryinfo/utssdk/mp-weixin/index.js b/uni_modules/uni-getbatteryinfo/utssdk/mp-weixin/index.js index c85d6b4..5124b3d 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/mp-weixin/index.js +++ b/uni_modules/uni-getbatteryinfo/utssdk/mp-weixin/index.js @@ -1,3 +1,3 @@ -export default function getBatteryInfo(options) { +export function getBatteryInfo(options) { return wx.getBatteryInfo(options) } diff --git a/uni_modules/uni-getbatteryinfo/utssdk/web/index.js b/uni_modules/uni-getbatteryinfo/utssdk/web/index.js index 33782f4..4049f50 100644 --- a/uni_modules/uni-getbatteryinfo/utssdk/web/index.js +++ b/uni_modules/uni-getbatteryinfo/utssdk/web/index.js @@ -1,4 +1,4 @@ -export default function getBatteryInfo(options) { +export function getBatteryInfo(options) { if (navigator.getBattery) { navigator.getBattery().then(battery => { const res = { -- GitLab