四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.7 KiB

  1. <template>
  2. <view class="yingbing-battery">
  3. <view class="yingbing-battery-wrapper" :style="{
  4. 'border-color': color
  5. }">
  6. <view class="yingbing-battery-content">
  7. <view class="yingbing-battery-content-value" :style="{
  8. 'background-color': color,
  9. width: value + 'px'
  10. }">
  11. </view>
  12. </view>
  13. </view>
  14. <view class="yingbing-battery-top" :style="{
  15. 'background-color': color
  16. }"></view>
  17. </view>
  18. </template>
  19. <script>
  20. const max = 16
  21. export default {
  22. inject: ['getColor'],
  23. computed: {
  24. color () {
  25. return this.getColor()
  26. }
  27. },
  28. data () {
  29. return {
  30. value: max
  31. }
  32. },
  33. // #ifdef APP-PLUS
  34. beforeDestroy() {
  35. if ( this.recevier ) {
  36. plus.android.runtimeMainActivity().unregisterReceiver(this.recevier)
  37. }
  38. },
  39. // #endif
  40. created () {
  41. this.getBattery()
  42. },
  43. methods: {
  44. getBattery () {
  45. // #ifdef H5
  46. //window.navigator.getBattery只能在安全环境下(比如:https file:///url)使用,判断一下避免报错
  47. window.navigator.getBattery && window.navigator.getBattery().then((res) => {
  48. // 电池电量在0到1之间,因此我们将其乘以100得出百分比
  49. this.value = res.level * max
  50. });
  51. // #endif
  52. // #ifdef MP-WEIXIN
  53. wx.getBatteryInfo({
  54. success: (res) => {
  55. this.value = (res.level / 100) * max
  56. }
  57. })
  58. // #endif
  59. // #ifdef APP-PLUS
  60. uni.getSystemInfo({
  61. success: (res) => {
  62. if ( res.osName == 'android' ) {
  63. const main = plus.android.runtimeMainActivity()
  64. const Intent = plus.android.importClass('android.content.Intent');
  65. this.recevier = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', { 
  66. onReceive: (context, intent) => { 
  67.        let action = intent.getAction(); 
  68.        if (action == Intent.ACTION_BATTERY_CHANGED) { 
  69.             let level = intent.getIntExtra("level", 0); //电量 B5教程网 
  70. this.value = (level / 100) * max
  71. main.unregisterReceiver(this.recevier)//销毁注册广播
  72. //let voltage = intent.getIntExtra("voltage", 0); //电池电压 
  73. //let temperature = intent.getIntExtra("temperature", 0); //电池温度 
  74.            //如需获取别的,在这里继续写,此代码只提供获取电量 
  75.        } 
  76.     } 
  77. }); 
  78. const filter = plus.android.newObject('android.content.IntentFilter', Intent.ACTION_BATTERY_CHANGED); 
  79. main.registerReceiver(this.recevier, filter);
  80. } else if ( res.osName == 'ios' ) {
  81. const UIDevice = plus.ios.import("UIDevice");
  82. const dev = UIDevice.currentDevice(); 
  83. if (!dev.isBatteryMonitoringEnabled()) { 
  84.     dev.setBatteryMonitoringEnabled(true); 
  85. } 
  86. let level = dev.batteryLevel();
  87. this.value = level * max
  88. }
  89. }
  90. })
  91. // #endif
  92. }
  93. }
  94. }
  95. </script>
  96. <style scoped>
  97. .yingbing-battery {
  98. /* #ifndef APP-NVUE */
  99. display: flex;
  100. /* #endif */
  101. flex-direction: row;
  102. align-items: center;
  103. opacity: 0.5;
  104. }
  105. .yingbing-battery-wrapper {
  106. width: 20px;
  107. height: 9px;
  108. border-width: 1px;
  109. border-style: solid;
  110. padding: 1px;
  111. /* #ifndef APP-NVUE */
  112. display: flex;
  113. flex-direction: column;
  114. box-sizing: border-box;
  115. overflow: hidden;
  116. /* #endif */
  117. }
  118. .yingbing-battery-content {
  119. flex: 1;
  120. /* #ifndef APP-NVUE */
  121. display: flex;
  122. flex-direction: column;
  123. box-sizing: border-box;
  124. overflow: hidden;
  125. /* #endif */
  126. }
  127. .yingbing-battery-content-value {
  128. height: 5px;
  129. }
  130. .yingbing-battery-content-value-text {
  131. font-size: 8px;
  132. }
  133. .yingbing-battery-top {
  134. width: 2px;
  135. height: 5px;
  136. }
  137. </style>