From cb82131b171bd8decda618b274aca384b6432d7f Mon Sep 17 00:00:00 2001 From: wanganxp Date: Wed, 12 Jul 2023 04:25:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E6=95=B4=E4=BA=86=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=92=8CAPI=E7=9A=84=E5=AF=BC=E8=88=AA=E5=88=86=E7=B1=BB?= =?UTF-8?q?=EF=BC=9B=E6=8F=90=E4=BA=A4=E4=BA=86=E7=94=B5=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages.json | 18 +- .../get-battery-info/get-battery-info.uvue | 28 + pages/tabBar/API.uvue | 60 +-- pages/tabBar/component.uvue | 502 +++++++++--------- uni_modules/uni-getbatteryinfo/changelog.md | 9 + uni_modules/uni-getbatteryinfo/index.d.ts | 37 ++ uni_modules/uni-getbatteryinfo/package.json | 91 ++++ uni_modules/uni-getbatteryinfo/readme.md | 38 ++ .../utssdk/app-android/config.json | 3 + .../utssdk/app-android/index.uts | 69 +++ .../utssdk/app-ios/config.json | 3 + .../utssdk/app-ios/index.uts | 36 ++ .../uni-getbatteryinfo/utssdk/interface.uts | 139 +++++ .../utssdk/mp-alipay/index.js | 3 + .../utssdk/mp-baidu/index.js | 3 + .../uni-getbatteryinfo/utssdk/mp-qq/index.js | 3 + .../utssdk/mp-weixin/index.js | 3 + .../uni-getbatteryinfo/utssdk/web/index.js | 23 + 18 files changed, 787 insertions(+), 281 deletions(-) create mode 100644 pages/API/get-battery-info/get-battery-info.uvue create mode 100644 uni_modules/uni-getbatteryinfo/changelog.md create mode 100644 uni_modules/uni-getbatteryinfo/index.d.ts create mode 100644 uni_modules/uni-getbatteryinfo/package.json create mode 100644 uni_modules/uni-getbatteryinfo/readme.md create mode 100644 uni_modules/uni-getbatteryinfo/utssdk/app-android/config.json create mode 100644 uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts create mode 100644 uni_modules/uni-getbatteryinfo/utssdk/app-ios/config.json create mode 100644 uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts create mode 100644 uni_modules/uni-getbatteryinfo/utssdk/interface.uts create mode 100644 uni_modules/uni-getbatteryinfo/utssdk/mp-alipay/index.js create mode 100644 uni_modules/uni-getbatteryinfo/utssdk/mp-baidu/index.js create mode 100644 uni_modules/uni-getbatteryinfo/utssdk/mp-qq/index.js create mode 100644 uni_modules/uni-getbatteryinfo/utssdk/mp-weixin/index.js create mode 100644 uni_modules/uni-getbatteryinfo/utssdk/web/index.js diff --git a/pages.json b/pages.json index 1cc5043e..4f1b196a 100644 --- a/pages.json +++ b/pages.json @@ -598,7 +598,15 @@ "navigationBarTitleText": "" } } - ], + ,{ + "path" : "pages/API/get-battery-info/get-battery-info", + "style" : + { + "navigationBarTitleText": "电量" + } + + } + ], "globalStyle": { "pageOrientation": "portrait", "navigationBarTitleText": "Hello uniapp x", @@ -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": [{ diff --git a/pages/API/get-battery-info/get-battery-info.uvue b/pages/API/get-battery-info/get-battery-info.uvue new file mode 100644 index 00000000..84c73312 --- /dev/null +++ b/pages/API/get-battery-info/get-battery-info.uvue @@ -0,0 +1,28 @@ + + + + \ No newline at end of file diff --git a/pages/tabBar/API.uvue b/pages/tabBar/API.uvue index de145b42..f5631220 100644 --- a/pages/tabBar/API.uvue +++ b/pages/tabBar/API.uvue @@ -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, @@ -51,10 +73,6 @@ name: "页面跳转", url: "navigator", }, - { - name: "页面通信event-bus", - url: "event-bus", - }, { name: "页面栈管理", url: "getCurrentPages", @@ -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,16 +114,16 @@ url: "canvas", enable: false }, + { + name: "节点布局交互状态", + url: "intersection-observer", + enable: false + }, */ { name: "节点信息", url: "get-node-info", enable: false }, - { - name: "节点布局交互状态", - url: "intersection-observer", - enable: false - }, { name: "显示操作菜单", url: "action-sheet", @@ -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, diff --git a/pages/tabBar/component.uvue b/pages/tabBar/component.uvue index 3143b709..7004d693 100644 --- a/pages/tabBar/component.uvue +++ b/pages/tabBar/component.uvue @@ -1,254 +1,270 @@ \ No newline at end of file diff --git a/uni_modules/uni-getbatteryinfo/changelog.md b/uni_modules/uni-getbatteryinfo/changelog.md new file mode 100644 index 00000000..f17dd2de --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/changelog.md @@ -0,0 +1,9 @@ +## 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/微信小程序平台获取电量 diff --git a/uni_modules/uni-getbatteryinfo/index.d.ts b/uni_modules/uni-getbatteryinfo/index.d.ts new file mode 100644 index 00000000..60b511f6 --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/index.d.ts @@ -0,0 +1,37 @@ +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; +} diff --git a/uni_modules/uni-getbatteryinfo/package.json b/uni_modules/uni-getbatteryinfo/package.json new file mode 100644 index 00000000..cd789cb1 --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/package.json @@ -0,0 +1,91 @@ +{ + "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 diff --git a/uni_modules/uni-getbatteryinfo/readme.md b/uni_modules/uni-getbatteryinfo/readme.md new file mode 100644 index 00000000..0609fee9 --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/readme.md @@ -0,0 +1,38 @@ +# 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|是否正在充电中| diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-android/config.json b/uni_modules/uni-getbatteryinfo/utssdk/app-android/config.json new file mode 100644 index 00000000..bf959256 --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-android/config.json @@ -0,0 +1,3 @@ +{ + "minSdkVersion": "21" +} \ No newline at end of file diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts new file mode 100644 index 00000000..a978085f --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-android/index.uts @@ -0,0 +1,69 @@ +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 diff --git a/uni_modules/uni-getbatteryinfo/utssdk/app-ios/config.json b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/config.json new file mode 100644 index 00000000..721b81e0 --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/config.json @@ -0,0 +1,3 @@ +{ + "deploymentTarget": "9" +} \ 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 new file mode 100644 index 00000000..7d9a5237 --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/utssdk/app-ios/index.uts @@ -0,0 +1,36 @@ +// 引用 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 diff --git a/uni_modules/uni-getbatteryinfo/utssdk/interface.uts b/uni_modules/uni-getbatteryinfo/utssdk/interface.uts new file mode 100644 index 00000000..d64e6086 --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/utssdk/interface.uts @@ -0,0 +1,139 @@ +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 diff --git a/uni_modules/uni-getbatteryinfo/utssdk/mp-alipay/index.js b/uni_modules/uni-getbatteryinfo/utssdk/mp-alipay/index.js new file mode 100644 index 00000000..4fdced2e --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/utssdk/mp-alipay/index.js @@ -0,0 +1,3 @@ +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 new file mode 100644 index 00000000..5944a774 --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/utssdk/mp-baidu/index.js @@ -0,0 +1,3 @@ +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 new file mode 100644 index 00000000..06287bc4 --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/utssdk/mp-qq/index.js @@ -0,0 +1,3 @@ +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 new file mode 100644 index 00000000..5124b3de --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/utssdk/mp-weixin/index.js @@ -0,0 +1,3 @@ +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 new file mode 100644 index 00000000..4049f504 --- /dev/null +++ b/uni_modules/uni-getbatteryinfo/utssdk/web/index.js @@ -0,0 +1,23 @@ +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) + } +} -- GitLab