Browse Source

电源状态

gaoshuaixing 4 years ago
parent
commit
0f9187d009

+ 41 - 1
electron/ipc/example.js

@@ -9,7 +9,7 @@
  * @param arg 接收到的消息
  */
 
-const {app, dialog, BrowserWindow, BrowserView, Notification} = require('electron');
+const {app, dialog, BrowserWindow, BrowserView, Notification, powerMonitor} = require('electron');
 const path = require('path');
 const _ = require('lodash');
 
@@ -174,4 +174,44 @@ exports.removeViewContent = function () {
   notificationObj.show();
 
   return true
+}
+
+/**
+ * 电源监控
+ */
+ exports.initPowerMonitor = function (event, channel, arg) {
+
+  powerMonitor.on('on-ac', (e) => {
+    let data = {
+      type: 'on-ac',
+      msg: '接入了电源'
+    }
+    event.reply(`${channel}`, data)
+  });
+
+  powerMonitor.on('on-battery', (e) => {
+    let data = {
+      type: 'on-battery',
+      msg: '使用电池中'
+    }
+    event.reply(`${channel}`, data)
+  });
+
+  powerMonitor.on('lock-screen', (e) => {
+    let data = {
+      type: 'lock-screen',
+      msg: '锁屏了'
+    }
+    event.reply(`${channel}`, data)
+  });
+
+  powerMonitor.on('on-ac', (e) => {
+    let data = {
+      type: 'unlock-screen',
+      msg: '解锁了'
+    }
+    event.reply(`${channel}`, data)
+  });
+
+  return true
 }

+ 6 - 1
frontend/src/config/router.config.js

@@ -42,7 +42,12 @@ export const constantRouterMap = [
             path: '/demo/notification/index',
             name: 'DemoNotificationIndex',
             component: () => import('@/views/demo/notification/Index')
-          },                  
+          },
+          {
+            path: '/demo/powermonitor/index',
+            name: 'DemoPowerMonitorIndex',
+            component: () => import('@/views/demo/powermonitor/Index')
+          },                 
           {
             path: '/demo/shortcut/index',
             name: 'DemoShortcutIndex',

+ 8 - 2
frontend/src/layouts/DemoMenu.vue

@@ -50,13 +50,19 @@ export default {
         },
         'menu_402' : {
           icon: 'profile',
-          title: '通知',
+          title: '桌面通知',
           pageName: 'DemoNotificationIndex',
           params: {}
         },
+        'menu_403' : {
+          icon: 'profile',
+          title: '电源监控',
+          pageName: 'DemoPowerMonitorIndex',
+          params: {}
+        },        
         'menu_500' : {
           icon: 'profile',
-          title: '软件',
+          title: '软件调用',
           pageName: 'DemoSoftwareIndex',
           params: {}
         },

+ 56 - 0
frontend/src/views/demo/powermonitor/Index.vue

@@ -0,0 +1,56 @@
+<template>
+  <div id="app-demo-window-view">
+    <div class="one-block-1">
+      <span>
+        1. 监控电源状态
+      </span>
+    </div>  
+    <div class="one-block-2">
+      <a-space>
+        <p>当前状态:{{ currentStatus }}</p>
+      </a-space>
+      <p>* 拔掉电源,使用电池供电</p>
+      <p>* 接入电源</p>
+      <p>* 锁屏</p>
+      <p>* 解锁</p>
+    </div>
+  </div>
+</template>
+<script>
+
+export default {
+  data() {
+    return {
+      currentStatus: '无',
+    };
+  },
+  mounted () {
+    this.init();
+  },
+  methods: {
+    init () {
+      const self = this;
+      self.$ipc.on('example.initPowerMonitor', (event, result) => {
+        if (Object.prototype.toString.call(result) == '[object Object]') {
+          self.currentStatus = result.msg;
+          self.$message.info(result.msg);
+        }
+      })
+    }
+  }
+};
+</script>
+<style lang="less" scoped>
+#app-demo-window-view {
+  padding: 0px 10px;
+  text-align: left;
+  width: 100%;
+  .one-block-1 {
+    font-size: 16px;
+    padding-top: 10px;
+  }
+  .one-block-2 {
+    padding-top: 10px;
+  }
+}
+</style>