提交 cb82131b 编写于 作者: W wanganxp

规整了组件和API的导航分类;提交了电量

上级 f1354bf3
......@@ -598,6 +598,14 @@
"navigationBarTitleText": ""
}
}
,{
"path" : "pages/API/get-battery-info/get-battery-info",
"style" :
{
"navigationBarTitleText": "电量"
}
}
],
"globalStyle": {
"pageOrientation": "portrait",
......@@ -606,12 +614,7 @@
"navigationBarBackgroundColor": "#007AFF",
"backgroundColor": "#efeff4",
"backgroundColorTop": "#F4F5F6",
"backgroundColorBottom": "#F4F5F6",
"h5": {
"maxWidth": 1190,
"navigationBarTextStyle": "black",
"navigationBarBackgroundColor": "#F1F1F1"
}
"backgroundColorBottom": "#F4F5F6"
},
"tabBar": {
"color": "#7A7E83",
......@@ -644,7 +647,6 @@
}
]
},
"uniIdRouter": {},
"condition": { //模式配置,仅开发期间生效
"current": 0, //当前激活的模式(list 的索引项)
"list": [{
......
<template>
<view>
<text>当前电量:{{level}}%</text>
<text>是否充电中:{{isCharging}}</text>
</view>
</template>
<script>
export default {
data() {
return {
level: 0,
isCharging: false
}
},
onLoad() {
uni.getBatteryInfo({
success: res => {
this.level = res.level;
this.isCharging = res.isCharging;
}
});
}
}
</script>
<style>
</style>
\ No newline at end of file
......@@ -42,7 +42,29 @@
export default {
data() {
return {
list: [{
list: [
{
id: "base",
name: "基础",
open: false,
pages: [
{
name: "getApp",
url: "getApp",
enable: false
},
{
name: "事件总线event-bus",
url: "event-bus",
},
{
name: "获取启动参数",
url: "get-launch-options-sync",
enable: false
}
] as Page[],
},
{
id: "page",
name: "页面",
open: false,
......@@ -50,10 +72,6 @@
{
name: "页面跳转",
url: "navigator",
},
{
name: "页面通信event-bus",
url: "event-bus",
},
{
name: "页面栈管理",
......@@ -79,11 +97,6 @@
name: "将页面滚动到指定位置",
url: "page-scroll-to",
},
{
name: "获取启动参数",
url: "get-launch-options-sync",
enable: false
},
] as Page[],
},
{
......@@ -91,7 +104,7 @@
name: "界面",
open: false,
pages: [
{
/* {
name: "创建动画",
url: "animation",
enable: false
......@@ -101,15 +114,15 @@
url: "canvas",
enable: false
},
{
name: "节点信息",
url: "get-node-info",
enable: false
},
{
name: "节点布局交互状态",
url: "intersection-observer",
enable: false
}, */
{
name: "节点信息",
url: "get-node-info",
enable: false
},
{
name: "显示操作菜单",
......@@ -157,12 +170,10 @@
{
name: "获取APP授权设置",
url: "get-app-authorize-setting",
enable:true
},
{
name: "电量",
url: "get-battery",
enable:false
url: "get-battery-info",
},
/* {
name: "打电话",
......@@ -406,17 +417,6 @@
] as Page[],
}, */
{
id: "view-event",
name: "视图层事件",
open: false,
pages: [
{
name: "视图层事件",
url: "view-event",
enable: false
}
] as Page[],
},{
id: "exit",
name: "退出",
open: false,
......
......@@ -67,12 +67,12 @@
},
*/
{
name:'list(新闻)',
url:"pages/component/list/list",
name: 'list(新闻)',
url: "pages/component/list/list",
},
{
name:'list',
url:"pages/component/long-list/long-list",
name: 'list',
url: "pages/component/long-list/long-list",
}
] as Page[]
}, {
......@@ -122,7 +122,7 @@
name: 'slider',
}, {
name: 'slider-100',
},{
}, {
name: 'switch',
}, {
name: 'textarea',
......@@ -207,6 +207,22 @@
pages: [] as Page[]
}
*/
{
id: "general-attr-event",
name: "通用属性和事件",
open: false,
pages: [
{
name: "通用属性",
url: "general-attr",
enable: false
},{
name: "通用事件",
url: "general-event",
enable: false
},
] as Page[],
},
] as ListItem[],
arrowUpIcon: '/static/icons/arrow-up.png',
arrowDownIcon: '/static/icons/arrow-down.png',
......
## 1.3.0(2023-05-30)
+ 新增 同步获取电量api
## 1.2.0(2022-10-17)
实现百度小程序/支付宝小程序/QQ小程序获取电量
## 1.1.0(2022-10-17)
实现ios平台获取电量
## 1.0.0(2022-09-01)
实现android/web/微信小程序平台获取电量
declare namespace UniNamespace {
interface GetBatteryInfoSuccessCallbackResult {
/**
* 是否正在充电中
*/
isCharging: boolean;
/**
* 设备电量,范围 1 - 100
*/
level: number;
errMsg: string;
}
interface GetBatteryInfoOption {
/**
* 接口调用结束的回调函数(调用成功、失败都会执行)
*/
complete?: Function
/**
* 接口调用失败的回调函数
*/
fail?: Function
/**
* 接口调用成功的回调函数
*/
success?: (result: GetBatteryInfoSuccessCallbackResult) => void
}
}
declare interface Uni {
/**
* 获取设备电量
*
* 文档: [https://uniapp.dcloud.net.cn/api/system/batteryInfo.html](https://uniapp.dcloud.net.cn/api/system/batteryInfo.html)
*/
getBatteryInfo(option?: UniNamespace.GetBatteryInfoOption): void;
}
{
"id": "uni-getbatteryinfo",
"displayName": "uni-getbatteryinfo",
"version": "1.3.0",
"description": "使用uts开发,实现在多个平台获取电池电量功能",
"keywords": [
"battery"
],
"repository": "",
"engines": {
"HBuilderX": "^3.6.0"
},
"dcloudext": {
"type": "uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "无",
"data": "插件不采集任何数据",
"permissions": "无"
},
"npmurl": ""
},
"uni_modules": {
"uni-ext-api": {
"uni": [
"getBatteryInfo",
"getBatteryInfoSync"
]
},
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "y",
"aliyun": "y"
},
"client": {
"Vue": {
"vue2": "n",
"vue3": "y"
},
"App": {
"app-android": {
"minVersion": "21"
},
"app-ios": {
"minVersion": "9"
}
},
"H5-mobile": {
"Safari": "y",
"Android Browser": "y",
"微信浏览器(Android)": "y",
"QQ浏览器(Android)": "y"
},
"H5-pc": {
"Chrome": "y",
"IE": "y",
"Edge": "y",
"Firefox": "y",
"Safari": "y"
},
"小程序": {
"微信": "y",
"阿里": "y",
"百度": "y",
"字节跳动": "u",
"QQ": "y",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
\ No newline at end of file
# uni-getbatteryinfo
## 使用文档
```ts
// 获取电量信息
uni.getBatteryInfo({
success(res) {
console.log(res);
uni.showToast({
title: "当前电量:" + res.level + '%',
icon: 'none'
});
}
})
```
### 参数
Object object
|属性|类型|必填|说明|
|----|---|----|----|
|success|function|否|接口调用成功的回调函数|
|fail|function|否|接口调用失败的回调函数|
|complete|function|否|接口调用结束的回调函数(调用成功、失败都会执行)|
object.success 回调函数
|属性|类型|说明|
|----|---|----|
|level|number|设备电量,范围 1 - 100|
|isCharging|boolean|是否正在充电中|
{
"minSdkVersion": "21"
}
\ No newline at end of file
import Context from "android.content.Context";
import BatteryManager from "android.os.BatteryManager";
import { UTSAndroid } from "io.dcloud.uts";
import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoFail, GetBatteryInfoResult,GetBatteryInfoSync } from '../interface.uts'
/**
* 异步获取电量
* @param {Object} options
*/
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 = {
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)
}
}
/**
* 同步获取电量示例
*/
export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryInfoResult {
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 : GetBatteryInfoResult = {
level: level,
isCharging: manager.isCharging()
};
return res;
} else {
/**
* 无有效上下文
*/
const res : GetBatteryInfoResult = {
level: -1,
isCharging: false
};
return res;
}
}
\ No newline at end of file
{
"deploymentTarget": "9"
}
\ No newline at end of file
// 引用 iOS 原生平台 api
import { UIDevice } from "UIKit";
import { Int } from 'Swift';
import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoResult, GetBatteryInfoSync } from '../interface.uts';
/**
* 导出 获取电量方法
*/
export const getBatteryInfo : GetBatteryInfo = function (options) {
// 开启电量检测
UIDevice.current.isBatteryMonitoringEnabled = true
// 返回数据
const res : GetBatteryInfoSuccess = {
errMsg: "getBatteryInfo:ok",
level: new Int(UIDevice.current.batteryLevel * 100),
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
};
options.success?.(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
export type GetBatteryInfoSuccess = {
errMsg : string,
/**
* 设备电量,范围1 - 100
*/
level : number,
/**
* 是否正在充电中
*/
isCharging : boolean
}
export type GetBatteryInfoFail = {
/**
* 错误码
*/
errCode : number,
/**
* 调用API的名称
*/
errSubject : string,
/**
* 错误的详细信息
*/
errMsg : string,
/**
* 错误来源
*/
cause : any | null
}
export type GetBatteryInfoOptions = {
/**
* 接口调用结束的回调函数(调用成功、失败都会执行)
*/
success ?: (res : GetBatteryInfoSuccess) => void
/**
* 接口调用失败的回调函数
*/
fail ?: (res : GetBatteryInfoFail) => void
/**
* 接口调用成功的回调
*/
complete ?: (res : any) => void
}
export type GetBatteryInfoResult = {
/**
* 设备电量,范围1 - 100
*/
level : number,
/**
* 是否正在充电中
*/
isCharging : boolean
}
/**
* 获取电量信息
* @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
export type GetBatteryInfoSync = () => GetBatteryInfoResult
interface Uni {
/**
* 获取电池电量信息
* @description 获取电池电量信息
* @param {GetBatteryInfoOptions} options
* @example
* ```typescript
* uni.getBatteryInfo({
* success(res) {
* console.log(res);
* }
* })
* ```
* @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版本
*
*/
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
export function getBatteryInfo(options) {
return my.getBatteryInfo(options)
}
export function getBatteryInfo(options) {
return swan.getBatteryInfo(options)
}
export function getBatteryInfo(options) {
return qq.getBatteryInfo(options)
}
export function getBatteryInfo(options) {
return wx.getBatteryInfo(options)
}
export function getBatteryInfo(options) {
if (navigator.getBattery) {
navigator.getBattery().then(battery => {
const res = {
errCode: 0,
errSubject: "uni-getBatteryInfo",
errMsg: 'getBatteryInfo:ok',
level: battery.level * 100,
isCharging: battery.charging
}
options.success && options.success(res)
options.complete && options.complete(res)
})
} else {
const res = {
errCode: 1002,
errSubject: "uni-getBatteryInfo",
errMsg: 'getBatteryInfo:fail navigator.getBattery is unsupported'
}
options.fail && options.fail(res)
options.complete && options.complete(res)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册