提交 2e102333 编写于 作者: DCloud_iOS_XHY's avatar DCloud_iOS_XHY

Merge branch 'dev' of gitcode.net:dcloud/hello-uts into dev

...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
</view> </view>
</template> </template>
<script> <script>
import getBatteryInfo from "@/uni_modules/uni-getbatteryinfo";
import gotoDemoActivity from "@/uni_modules/uts-nativepage"; import gotoDemoActivity from "@/uni_modules/uts-nativepage";
...@@ -36,7 +35,7 @@ ...@@ -36,7 +35,7 @@
onUnload: function() {}, onUnload: function() {},
methods: { methods: {
testGetBatteryCapacity() { testGetBatteryCapacity() {
getBatteryInfo({ uni.getBatteryInfo({
success(res) { success(res) {
uni.showToast({ uni.showToast({
title: "当前电量:" + res.level + '%', title: "当前电量:" + res.level + '%',
......
...@@ -30,7 +30,10 @@ ...@@ -30,7 +30,10 @@
}, },
"npmurl": "" "npmurl": ""
}, },
"uni_modules": { "uni_modules": {
"exports": {
"uni": "getBatteryInfo"
},
"dependencies": [], "dependencies": [],
"encrypt": [], "encrypt": [],
"platforms": { "platforms": {
......
...@@ -9,41 +9,27 @@ type GetBatteryInfoOptions = { ...@@ -9,41 +9,27 @@ type GetBatteryInfoOptions = {
complete?: (res: UTSJSONObject) => void complete?: (res: UTSJSONObject) => void
} }
export default async function getBatteryInfo(options: GetBatteryInfoOptions) { export default function getBatteryInfo(options: GetBatteryInfoOptions) {
const context = getAppContext(); const context = getAppContext();
console.log(context);
if (context != null) { if (context != null) {
const manager = context.getSystemService( const manager = context.getSystemService(
Context.BATTERY_SERVICE Context.BATTERY_SERVICE
) as BatteryManager; ) as BatteryManager;
console.log(manager);
const level = manager.getIntProperty( const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY BatteryManager.BATTERY_PROPERTY_CAPACITY
); );
const res = { const res = {
errMsg: 'getBatteryInfo:ok', errMsg: 'getBatteryInfo:ok',
level, level,
isCharging: manager.isCharging() isCharging: manager.isCharging()
} }
console.log(res); options.success?.(res)
if (options.success != null) { options.complete?.(res)
options.success!(res)
}
console.log("options.success");
if (options.complete != null) {
options.complete!(res)
}
console.log("options.complete");
} else { } else {
const res = { const res = {
errMsg: 'getBatteryInfo:fail getAppContext is null' errMsg: 'getBatteryInfo:fail getAppContext is null'
} }
if (options.fail != null) { options.fail?.(res)
options.fail!(res) options.complete?.(res)
}
if (options.complete != null) {
options.complete!(res)
}
} }
} }
\ No newline at end of file
...@@ -15,10 +15,6 @@ export default function getBatteryInfo(options: GetBatteryInfoOptions) { ...@@ -15,10 +15,6 @@ export default function getBatteryInfo(options: GetBatteryInfoOptions) {
level: Number(UIDevice.current.batteryLevel * 100), level: Number(UIDevice.current.batteryLevel * 100),
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging, isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
}; };
if (options.success != null) { options.success?.(res);
options.success!(res); options.complete?.(res);
}
if (options.complete != null) {
options.complete!(res);
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册