diff --git a/pages.json b/pages.json index 1cc5043ee7498559475e3c05d98db6cb58227008..4f1b196a7c64972f626c3e834eba88c204eb4c7a 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 0000000000000000000000000000000000000000..84c733122cd8c9ecef08ac92427092724fb49184 --- /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 de145b42473350931c6a3d1b59d99dee735db0a5..f5631220c2270a4a6e45fd4a3b36fe66c0657d1d 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 3143b7090e0f48440b94f6d8893ce9a4ee713cbe..7004d69369603f3976e21100d1884a97a1fcda8a 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 0000000000000000000000000000000000000000..f17dd2de1cee9bcbaddcc6a219af2bbf5055a9dd --- /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 0000000000000000000000000000000000000000..60b511f68f86e4fff522ee27ff04f3d5713a9645 --- /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 0000000000000000000000000000000000000000..cd789cb132dc3c8d379a9f31f561dafbf879d995 --- /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 0000000000000000000000000000000000000000..0609fee923541fa7d9811988b87562df7439dc36 --- /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 0000000000000000000000000000000000000000..bf9592567f563664ee930692b3282879db6d5007 --- /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 0000000000000000000000000000000000000000..a978085f0578f4cd28de9f2b593945bffeb5515d --- /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 0000000000000000000000000000000000000000..721b81e0240705a87ec0761aa9eb6f4e188769cb --- /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 0000000000000000000000000000000000000000..7d9a52372e459e68cdde47317e038cd7c0274890 --- /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 0000000000000000000000000000000000000000..d64e60860de6405b9a417afd00d17d64e479a314 --- /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 0000000000000000000000000000000000000000..4fdced2ee3aa9d80eafdffdc5f06d522c7a1f08a --- /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 0000000000000000000000000000000000000000..5944a774bdc27de75cbe441c1af274dfef4ab8ac --- /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 0000000000000000000000000000000000000000..06287bc468d46e1c12f79e455a934fcbea7c52c5 --- /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 0000000000000000000000000000000000000000..5124b3de21a08a351d2d7eb631aaf96a600aa147 --- /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 0000000000000000000000000000000000000000..4049f5043cf63a84780d2477cee0a590f2fa8ba8 --- /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) + } +}