提交 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": [],
......
import Context from "android.content.Context"; 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 = {
success?: (res: UTSJSONObject) => void export const getBatteryInfo : GetBatteryInfo = function (options) {
fail?: (res: UTSJSONObject) => void const context = UTSAndroid.getAppContext();
complete?: (res: UTSJSONObject) => void if (context != null) {
} const manager = context.getSystemService(
Context.BATTERY_SERVICE
export default function getBatteryInfo(options: GetBatteryInfoOptions) { ) as BatteryManager;
const context = UTSAndroid.getAppContext(); const level = manager.getIntProperty(
if (context != null) { BatteryManager.BATTERY_PROPERTY_CAPACITY
const manager = context.getSystemService( );
Context.BATTERY_SERVICE const res : GetBatteryInfoSuccess = {
) as BatteryManager; errCode: 0,
const level = manager.getIntProperty( errSubject: "uni-getBatteryInfo",
BatteryManager.BATTERY_PROPERTY_CAPACITY errMsg: 'getBatteryInfo:ok',
); level,
const res = { isCharging: manager.isCharging()
errCode: 0, }
errSubject: "uni-getBatteryInfo", options.success?.(res)
errMsg: 'getBatteryInfo:ok', options.complete?.(res)
level, } else {
isCharging: manager.isCharging() const res : GetBatteryInfoFail = {
} errSubject: "uni-getBatteryInfo",
options.success?.(res) errCode: 1001,
options.complete?.(res) errMsg: 'getBatteryInfo:fail getAppContext is null',
} else { cause: null
const res = { }
errSubject: "uni-getBatteryInfo", options.fail?.(res)
errCode: 1001, options.complete?.(res)
errMsg: 'getBatteryInfo:fail getAppContext is null' }
} }
options.fail?.(res) \ No newline at end of file
options.complete?.(res)
}
}
// 引用 iOS 原生平台 api // 引用 iOS 原生平台 api
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; export const getBatteryInfo : GetBatteryInfo = function (options) {
complete?: (res: object) => void;
}; // 开启电量检测
UIDevice.current.isBatteryMonitoringEnabled = true
/**
* 导出 获取电量方法 // 返回数据
*/ const res : GetBatteryInfoSuccess = {
export default function getBatteryInfo(options: GetBatteryInfoOptions) { errCode: 0,
errSubject: "uni-getBatteryInfo",
// 开启电量检测 errMsg: "getBatteryInfo:ok",
UIDevice.current.isBatteryMonitoringEnabled = true level: new Int(UIDevice.current.batteryLevel * 100),
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
// 返回数据 };
const res = { options.success?.(res);
errCode: 0, options.complete?.(res);
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
type GetBatteryInfoResult = { export type GetBatteryInfoSuccess = {
/** /**
* 错误码 * 错误码
* 0:成功 * 0:成功
* 1001:getAppContext is null * 1001:getAppContext is null
* 1002:navigator.getBattery is unsupported * 1002:navigator.getBattery is unsupported
*/ */
errCode : number, errCode : number,
/** /**
* 调用API的名称 * 调用API的名称
*/ */
errSubject : string, errSubject : string,
/** /**
* 错误的详细信息 * 错误的详细信息
*/ */
errMsg : string, errMsg : string,
/** /**
* 设备电量,范围1 - 100 * 设备电量,范围1 - 100
*/ */
level : number, level : number,
/** /**
* 是否正在充电中 * 是否正在充电中
*/ */
isCharging : boolean isCharging : boolean
} }
interface UniError<T> { export type GetBatteryInfoFail = {
/** /**
* 错误码 * 错误码
*/ */
errCode : number, errCode : number,
/** /**
* 调用API的名称 * 调用API的名称
*/ */
errSubject : string, errSubject : string,
/** /**
* 错误的详细信息 * 错误的详细信息
*/ */
errMsg : string, errMsg : string,
/** /**
* 错误来源 * 错误来源
*/ */
cause : any cause : any | null
} }
export type GetBatteryInfoOptions = { 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.
先完成此消息的编辑!
想要评论请 注册