提交 6c8538df 编写于 作者: fxy060608's avatar fxy060608

电量:重构

上级 36a07560
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
}, },
"uni_modules": { "uni_modules": {
"uni-ext-api": { "uni-ext-api": {
"uni": "getBatteryInfo" "uni": ["getBatteryInfo"]
}, },
"dependencies": [], "dependencies": [],
"encrypt": [], "encrypt": [],
......
...@@ -2,14 +2,9 @@ import Context from "android.content.Context"; ...@@ -2,14 +2,9 @@ import Context from "android.content.Context";
import BatteryManager from "android.os.BatteryManager"; import BatteryManager from "android.os.BatteryManager";
import { UTSAndroid } from "io.dcloud.uts"; import { UTSAndroid } from "io.dcloud.uts";
import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoFail, GetBatteryInfoOptions } from '../interface.uts'
type GetBatteryInfoOptions = { export const getBatteryInfo : GetBatteryInfo = function (options) {
success?: (res: UTSJSONObject) => void
fail?: (res: UTSJSONObject) => void
complete?: (res: UTSJSONObject) => void
}
export default function getBatteryInfo(options: GetBatteryInfoOptions) {
const context = UTSAndroid.getAppContext(); const context = UTSAndroid.getAppContext();
if (context != null) { if (context != null) {
const manager = context.getSystemService( const manager = context.getSystemService(
...@@ -18,7 +13,7 @@ export default function getBatteryInfo(options: GetBatteryInfoOptions) { ...@@ -18,7 +13,7 @@ export default function getBatteryInfo(options: GetBatteryInfoOptions) {
const level = manager.getIntProperty( const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY BatteryManager.BATTERY_PROPERTY_CAPACITY
); );
const res = { const res : GetBatteryInfoSuccess = {
errCode: 0, errCode: 0,
errSubject: "uni-getBatteryInfo", errSubject: "uni-getBatteryInfo",
errMsg: 'getBatteryInfo:ok', errMsg: 'getBatteryInfo:ok',
...@@ -28,10 +23,11 @@ export default function getBatteryInfo(options: GetBatteryInfoOptions) { ...@@ -28,10 +23,11 @@ export default function getBatteryInfo(options: GetBatteryInfoOptions) {
options.success?.(res) options.success?.(res)
options.complete?.(res) options.complete?.(res)
} else { } else {
const res = { const res : GetBatteryInfoFail = {
errSubject: "uni-getBatteryInfo", errSubject: "uni-getBatteryInfo",
errCode: 1001, errCode: 1001,
errMsg: 'getBatteryInfo:fail getAppContext is null' errMsg: 'getBatteryInfo:fail getAppContext is null',
cause: null
} }
options.fail?.(res) options.fail?.(res)
options.complete?.(res) options.complete?.(res)
......
...@@ -2,25 +2,18 @@ ...@@ -2,25 +2,18 @@
import { UIDevice } from "UIKit"; import { UIDevice } from "UIKit";
import { Int } from 'Swift'; import { Int } from 'Swift';
/** import { GetBatteryInfo, GetBatteryInfoSuccess } from '../interface.uts';
* 定义 接口参数
*/
type GetBatteryInfoOptions = {
success?: (res: object) => void;
fail?: (res: object) => void;
complete?: (res: object) => void;
};
/** /**
* 导出 获取电量方法 * 导出 获取电量方法
*/ */
export default function getBatteryInfo(options: GetBatteryInfoOptions) { export const getBatteryInfo : GetBatteryInfo = function (options) {
// 开启电量检测 // 开启电量检测
UIDevice.current.isBatteryMonitoringEnabled = true UIDevice.current.isBatteryMonitoringEnabled = true
// 返回数据 // 返回数据
const res = { const res : GetBatteryInfoSuccess = {
errCode: 0, errCode: 0,
errSubject: "uni-getBatteryInfo", errSubject: "uni-getBatteryInfo",
errMsg: "getBatteryInfo:ok", errMsg: "getBatteryInfo:ok",
......
type GetBatteryInfoResult = { export type GetBatteryInfoSuccess = {
/** /**
* 错误码 * 错误码
* 0:成功 * 0:成功
...@@ -24,7 +24,7 @@ type GetBatteryInfoResult = { ...@@ -24,7 +24,7 @@ type GetBatteryInfoResult = {
isCharging : boolean isCharging : boolean
} }
interface UniError<T> { export type GetBatteryInfoFail = {
/** /**
* 错误码 * 错误码
*/ */
...@@ -40,7 +40,7 @@ interface UniError<T> { ...@@ -40,7 +40,7 @@ interface UniError<T> {
/** /**
* 错误来源 * 错误来源
*/ */
cause : any cause : any | null
} }
...@@ -49,28 +49,27 @@ export type GetBatteryInfoOptions = { ...@@ -49,28 +49,27 @@ export type GetBatteryInfoOptions = {
/** /**
* 接口调用结束的回调函数(调用成功、失败都会执行) * 接口调用结束的回调函数(调用成功、失败都会执行)
*/ */
success ?: (res : GetBatteryInfoResult) => void success ?: (res : GetBatteryInfoSuccess) => void
/** /**
* 接口调用失败的回调函数 * 接口调用失败的回调函数
*/ */
fail ?: (res : UniError) => void fail ?: (res : GetBatteryInfoFail) => void
/** /**
* 接口调用成功的回调 * 接口调用成功的回调
*/ */
complete ?: (res : object) => void complete ?: (res : any) => void
} }
/** /**
* 获取电量信息 * 获取电量信息
* @param {GetBatteryInfoOptions} options * @param {GetBatteryInfoOptions} options
* *
* *
* @tutorial https://uniapp.dcloud.net.cn/api/system/batteryInfo.html * @tutorial https://uniapp.dcloud.net.cn/api/system/batteryInfo.html
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22 * @platforms APP-IOS = ^9.0,APP-ANDROID = ^22
* @since 3.6.11 * @since 3.6.11
* *
* @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 default function getBatteryInfo(options : GetBatteryInfoOptions) : void export type GetBatteryInfo = (options : GetBatteryInfoOptions) => void
\ No newline at end of file
export default function getBatteryInfo(options) { export function getBatteryInfo(options) {
return my.getBatteryInfo(options) return my.getBatteryInfo(options)
} }
export default function getBatteryInfo(options) { export function getBatteryInfo(options) {
return swan.getBatteryInfo(options) return swan.getBatteryInfo(options)
} }
export default function getBatteryInfo(options) { export function getBatteryInfo(options) {
return qq.getBatteryInfo(options) return qq.getBatteryInfo(options)
} }
export default function getBatteryInfo(options) { export function getBatteryInfo(options) {
return wx.getBatteryInfo(options) return wx.getBatteryInfo(options)
} }
export default function getBatteryInfo(options) { export function getBatteryInfo(options) {
if (navigator.getBattery) { if (navigator.getBattery) {
navigator.getBattery().then(battery => { navigator.getBattery().then(battery => {
const res = { const res = {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册