Browse Source

修改前端ipc

哆啦好梦 2 years ago
parent
commit
dea09f890b

+ 2 - 2
frontend/src/main.js

@@ -4,7 +4,7 @@ import 'ant-design-vue/dist/antd.less';
 import App from './App'
 import router from './router'
 import { VueAxios } from './utils/request'
-import IpcRenderer from '@/utils/ipcRenderer'
+import { ipc } from '@/utils/ipcRenderer'
 
 // 使用antd
 Vue.use(antd)
@@ -13,7 +13,7 @@ Vue.use(antd)
 Vue.use(VueAxios)
 
 // 全局注入IPC通信
-Vue.use(IpcRenderer)
+Vue.prototype.$ipc = ipc
 
 Vue.config.productionTip = false
 

+ 20 - 23
frontend/src/utils/ipcRenderer.js

@@ -1,31 +1,28 @@
-const { ipcRenderer: ipc } = (window.require && window.require('electron')) || window.electron || {}
+const { ipcRenderer: ipc } = (window.require && window.require('electron')) || window.electron || null;
 
 /**
- * 发送异步消息(invoke/handle 模型)
- * @param channel
- * @param param
- * @returns {Promise}
+ * ipc
+ * 官方api说明:https://www.electronjs.org/zh/docs/latest/api/ipc-renderer
+ * 
+ * 属性/方法
+ * ipc.invoke(channel, param) - 发送异步消息(invoke/handle 模型)
+ * ipc.sendSync(channel, param) - 发送同步消息(send/on 模型)
+ * ipc.on(channel, listener) - 监听 channel, 当新消息到达,调用 listener
+ * ipc.once(channel, listener) - 添加一次性 listener 函数
+ * ipc.removeListener(channel, listener) - 为特定的 channel 从监听队列中删除特定的 listener 监听者
+ * ipc.removeAllListeners(channel) - 移除所有的监听器,当指定 channel 时只移除与其相关的所有监听器
+ * ipc.send(channel, ...args) - 通过channel向主进程发送异步消息
+ * ipc.postMessage(channel, message, [transfer]) - 发送消息到主进程
+ * ipc.sendTo(webContentsId, channel, ...args) - 通过 channel 发送消息到带有 webContentsId 的窗口
+ * ipc.sendToHost(channel, ...args) - 消息会被发送到 host 页面上的 <webview> 元素
  */
-const invoke = (channel, param) => {
-  const message = ipc.invoke(channel, param);
-  return message;
-}
 
 /**
- * 发送同步消息(send/on 模型)
- * @param channel
- * @param param
- * @returns {Any}
+ * 是否为EE环境
  */
-const sendSync = (channel, param) => {
-  const message = ipc.sendSync(channel, param);
-  return message;
-}
+const isEE = ipc ? true : false;
 
-export default {
-  install(Vue) {
-    Vue.prototype.$ipc = ipc // 全局注入ipc
-    Vue.prototype.$ipcInvoke = invoke
-    Vue.prototype.$ipcSendSync = sendSync
-  }
+export {
+  ipc,
+  isEE
 }

+ 0 - 3
frontend/src/utils/message.js

@@ -1,3 +0,0 @@
-const { ipcRenderer: ipc } = (window.require && window.require('electron')) || window.electron || {}
-
-export default ipc

+ 2 - 2
frontend/src/views/base/db/Index.vue

@@ -156,7 +156,7 @@ export default {
       const params = {
         action: 'all',
       }
-      this.$ipcInvoke(ipcApiRoute.dbOperation, params).then(res => {
+      this.$ipc.invoke(ipcApiRoute.dbOperation, params).then(res => {
         console.log('res:', res);
         if (res.all_list.length == 0) {
           return false;
@@ -180,7 +180,7 @@ export default {
       if (ac == 'add' && this.name.length == 0) {
         self.$message.error(`请填写数据`);
       }
-      this.$ipcInvoke(ipcApiRoute.dbOperation, params).then(res => {
+      this.$ipc.invoke(ipcApiRoute.dbOperation, params).then(res => {
         console.log('res:', res);
         if (ac == 'get') {
           if (res.result.length == 0) {

+ 5 - 5
frontend/src/views/base/file/Index.vue

@@ -111,7 +111,7 @@ export default {
   methods: {
     getHost () {
       const self = this;
-      this.$ipcInvoke(ipcApiRoute.checkHttpServer, {}).then(r => {
+      this.$ipc.invoke(ipcApiRoute.checkHttpServer, {}).then(r => {
         if (r.enable) {
           self.servicAddress = r.server;
           storage.set('httpServiceConfig', r);
@@ -125,13 +125,13 @@ export default {
       })
     },
     openDirectry (id) {
-      this.$ipcInvoke(ipcApiRoute.openDirectory, {id: id}).then(res => {
+      this.$ipc.invoke(ipcApiRoute.openDirectory, {id: id}).then(res => {
         //console.log('res:', res)
       })      
     },
     selectDir() {
       const self = this;
-      self.$ipcInvoke(ipcApiRoute.selectFolder, '').then(r => {
+      this.$ipc.invoke(ipcApiRoute.selectFolder, '').then(r => {
         self.dir_path = r;
         self.$message.info(r);
       })      
@@ -139,14 +139,14 @@ export default {
 		messageShow(type) {
       const self = this;
       console.log('[messageShow] type:', type)
-      this.$ipcInvoke(ipcApiRoute.messageShow, '').then(r => {
+      this.$ipc.invoke(ipcApiRoute.messageShow, '').then(r => {
         self.$message.info(r);
       })
     },    
     messageShowConfirm(type) {
       const self = this;
       console.log('[messageShowConfirm] type:', type)
-      this.$ipcInvoke(ipcApiRoute.messageShowConfirm, '').then(r => {
+      this.$ipc.invoke(ipcApiRoute.messageShowConfirm, '').then(r => {
         self.$message.info(r);
       })
     },

+ 1 - 1
frontend/src/views/base/powermonitor/Index.vue

@@ -32,7 +32,7 @@ export default {
     init () {
       const self = this;
       this.$ipc.removeAllListeners(ipcApiRoute.initPowerMonitor);
-      self.$ipc.on(ipcApiRoute.initPowerMonitor, (event, result) => {
+      this.$ipc.on(ipcApiRoute.initPowerMonitor, (event, result) => {
         if (Object.prototype.toString.call(result) == '[object Object]') {
           self.currentStatus = result.msg;
           self.$message.info(result.msg);

+ 1 - 1
frontend/src/views/base/screen/Index.vue

@@ -40,7 +40,7 @@ export default {
   methods: {
     getScreen (index) {
       const self = this;
-      this.$ipcInvoke(ipcApiRoute.getScreen, index).then(result => {
+      this.$ipc.invoke(ipcApiRoute.getScreen, index).then(result => {
         self.data = result;
       })
     },

+ 1 - 1
frontend/src/views/base/socket/HttpServer.vue

@@ -40,7 +40,7 @@ export default {
   methods: {
     init () {
       const self = this;
-      this.$ipcInvoke(ipcApiRoute.checkHttpServer, {}).then(r => {
+      this.$ipc.invoke(ipcApiRoute.checkHttpServer, {}).then(r => {
         if (r.enable) {
           self.currentStatus = '开启';
           self.servicAddress = r.server;

+ 5 - 5
frontend/src/views/base/socket/Ipc.vue

@@ -112,28 +112,28 @@ export default {
     },
     handleInvoke () {
       const self = this;
-      this.$ipcInvoke(ipcApiRoute.ipcInvokeMsg, '异步-回调').then(r => {
+      this.$ipc.invoke(ipcApiRoute.ipcInvokeMsg, '异步-回调').then(r => {
         console.log('r:', r);
         self.message1 = r;
       });
     },
     async handleInvoke2 () {
-      const msg = await this.$ipcInvoke(ipcApiRoute.ipcInvokeMsg, '异步');
+      const msg = await this.$ipc.invoke(ipcApiRoute.ipcInvokeMsg, '异步');
       console.log('msg:', msg);
       this.message2 = msg;
     },
     handleSendSync () {
-      const msg = this.$ipcSendSync(ipcApiRoute.ipcSendSyncMsg, '同步');
+      const msg = this.$ipc.sendSync(ipcApiRoute.ipcSendSyncMsg, '同步');
       this.message3 = msg;
     },
     createWindow (index) {
-      this.$ipcInvoke(ipcApiRoute.createWindow, this.views[index]).then(id => {
+      this.$ipc.invoke(ipcApiRoute.createWindow, this.views[index]).then(id => {
         console.log('[createWindow] id:', id);
       })
     },
     async sendTosubWindow () {
       // 新窗口id
-      this.newWcId = await this.$ipcInvoke(ipcApiRoute.getWCid, this.windowName);
+      this.newWcId = await this.$ipc.invoke(ipcApiRoute.getWCid, this.windowName);
       this.$ipc.sendTo(this.newWcId, specialIpcRoute.window1ToWindow2, '窗口1通过 sendTo 给窗口2发送消息');
     },
   }

+ 1 - 1
frontend/src/views/base/software/Index.vue

@@ -40,7 +40,7 @@ export default {
   methods: {
     openSoft (id) {
       const self = this;   
-      this.$ipcInvoke(ipcApiRoute.openSoftware, id).then(result => {
+      this.$ipc.invoke(ipcApiRoute.openSoftware, id).then(result => {
         if (!result) {
           self.$message.error('程序不存在');
         }

+ 6 - 6
frontend/src/views/base/sqlitedb/Index.vue

@@ -181,7 +181,7 @@ export default {
       const params = {
         action: 'getDataDir',
       }
-      this.$ipcInvoke(ipcApiRoute.sqlitedbOperation, params).then(res => {
+      this.$ipc.invoke(ipcApiRoute.sqlitedbOperation, params).then(res => {
         this.data_dir = res.result;
       }) 
     },
@@ -190,7 +190,7 @@ export default {
       const params = {
         action: 'all',
       }
-      this.$ipcInvoke(ipcApiRoute.sqlitedbOperation, params).then(res => {
+      this.$ipc.invoke(ipcApiRoute.sqlitedbOperation, params).then(res => {
         if (res.all_list.length == 0) {
           return false;
         }
@@ -198,7 +198,7 @@ export default {
       }) 
     },
     selectDir() {
-      this.$ipcInvoke(ipcApiRoute.selectFolder, '').then(r => {
+      this.$ipc.invoke(ipcApiRoute.selectFolder, '').then(r => {
         this.data_dir = r;
         // 修改数据目录
         this.modifyDataDir(r);
@@ -206,7 +206,7 @@ export default {
     },
     openDir() {
       console.log('dd:', this.data_dir);
-      this.$ipcInvoke(ipcApiRoute.openDirectory, {id: this.data_dir}).then(res => {
+      this.$ipc.invoke(ipcApiRoute.openDirectory, {id: this.data_dir}).then(res => {
         //
       })
     },    
@@ -215,7 +215,7 @@ export default {
         action: 'setDataDir',
         data_dir: dir
       }
-      this.$ipcInvoke(ipcApiRoute.sqlitedbOperation, params).then(res => {
+      this.$ipc.invoke(ipcApiRoute.sqlitedbOperation, params).then(res => {
         this.all_list = res.all_list;
       }) 
     },
@@ -235,7 +235,7 @@ export default {
       if (ac == 'add' && this.name.length == 0) {
         self.$message.error(`请填写数据`);
       }
-      this.$ipcInvoke(ipcApiRoute.sqlitedbOperation, params).then(res => {
+      this.$ipc.invoke(ipcApiRoute.sqlitedbOperation, params).then(res => {
         console.log('res:', res);
         if (ac == 'get') {
           if (res.result.length == 0) {

+ 4 - 4
frontend/src/views/base/subwindow/Ipc.vue

@@ -102,23 +102,23 @@ export default {
     },
     handleInvoke () {
       const self = this;
-      this.$ipcInvoke(ipcApiRoute.ipcInvokeMsg, '异步-回调').then(r => {
+      this.$ipc.invoke(ipcApiRoute.ipcInvokeMsg, '异步-回调').then(r => {
         console.log('r:', r);
         self.message1 = r;
       });
     },
     async handleInvoke2 () {
-      const msg = await this.$ipcInvoke(ipcApiRoute.ipcInvokeMsg, '异步');
+      const msg = await this.$ipc.invoke(ipcApiRoute.ipcInvokeMsg, '异步');
       console.log('msg:', msg);
       this.message2 = msg;
     },
     handleSendSync () {
-      const msg = this.$ipcSendSync(ipcApiRoute.ipcSendSyncMsg, '同步');
+      const msg = this.$ipc.sendSync(ipcApiRoute.ipcSendSyncMsg, '同步');
       this.message3 = msg;
     },
     sendTosubWindow () {
       // 获取主窗口id
-      this.$ipcInvoke(ipcApiRoute.getWCid, 'main').then(id => {
+      this.$ipc.invoke(ipcApiRoute.getWCid, 'main').then(id => {
         this.mainWCid = id;
         this.$ipc.sendTo(this.mainWCid, specialIpcRoute.window2ToWindow1, '窗口2 通过 sendTo 给主窗口发送消息');
       });

+ 1 - 2
frontend/src/views/base/system/Index.vue

@@ -36,8 +36,7 @@ export default {
   methods: {
     init () {
       // todo .....
-      const self = this;
-      self.$ipcInvoke(ipcApiRoute.autoLaunch, 'check').then(result => {
+      this.$ipc.invoke(ipcApiRoute.autoLaunch, 'check').then(result => {
         console.log('[ipcRenderer] [autoLaunch] result:', result)
         this.autoLaunchChecked = result.status;
       })      

+ 1 - 1
frontend/src/views/base/testapi/Index.vue

@@ -27,7 +27,7 @@ export default {
       const params = {
         id: id
       }
-      this.$ipcInvoke(ipcApiRoute.test, params).then(res => {
+      this.$ipc.invoke(ipcApiRoute.test, params).then(res => {
         console.log('res:', res)
       }) 
     },

+ 2 - 2
frontend/src/views/base/theme/Index.vue

@@ -53,14 +53,14 @@ export default {
       this.currentThemeMode = e.target.value;
       console.log('setTheme currentThemeMode:', this.currentThemeMode)
 
-      this.$ipcInvoke(ipcApiRoute.setTheme, this.currentThemeMode).then(result => {
+      this.$ipc.invoke(ipcApiRoute.setTheme, this.currentThemeMode).then(result => {
         console.log('result:', result)
         self.currentThemeMode = result;
       })      
     },
     getTheme () {
       const self = this;
-      this.$ipcInvoke(ipcApiRoute.getTheme).then(result => {
+      this.$ipc.invoke(ipcApiRoute.getTheme).then(result => {
         console.log('result:', result)
         self.currentThemeMode = result;
       })  

+ 2 - 2
frontend/src/views/base/updater/Index.vue

@@ -54,7 +54,7 @@ export default {
       })
     },
     checkForUpdater () {
-      this.$ipcInvoke(ipcApiRoute.checkForUpdater).then(r => {
+      this.$ipc.invoke(ipcApiRoute.checkForUpdater).then(r => {
         console.log(r);
       })
     },
@@ -63,7 +63,7 @@ export default {
         this.$message.info('没有可用版本');
         return
       }
-      this.$ipcInvoke(ipcApiRoute.downloadApp).then(r => {
+      this.$ipc.invoke(ipcApiRoute.downloadApp).then(r => {
         console.log(r);
       })
     },

+ 1 - 1
frontend/src/views/base/window/Index.vue

@@ -56,7 +56,7 @@ export default {
   },
   methods: {
     createWindow (index) {
-      this.$ipcInvoke(ipcApiRoute.createWindow, this.views[index]).then(r => {
+      this.$ipc.invoke(ipcApiRoute.createWindow, this.views[index]).then(r => {
         console.log(r);
       })
     },

+ 2 - 4
frontend/src/views/base/windowview/Index.vue

@@ -44,14 +44,12 @@ export default {
   },
   methods: {
     loadViewContent (index) {
-      const self = this;
-      self.$ipcInvoke(ipcApiRoute.loadViewContent, this.views[index]).then(r => {
+      this.$ipc.invoke(ipcApiRoute.loadViewContent, this.views[index]).then(r => {
         console.log(r);
       })
     },
     removeViewContent (index) {
-      const self = this;
-      self.$ipcInvoke(ipcApiRoute.removeViewContent, self.views[index]).then(r => {
+      this.$ipc.invoke(ipcApiRoute.removeViewContent, self.views[index]).then(r => {
         console.log(r);
       })
     },

+ 2 - 2
frontend/src/views/other/java/Index.vue

@@ -29,7 +29,7 @@ export default {
   },  
   methods: {
     startServer () {
-      this.$ipcInvoke(ipcApiRoute.startJavaServer, {}).then(r => {
+      this.$ipc.invoke(ipcApiRoute.startJavaServer, {}).then(r => {
         if (r.code != 0) {
           this.$message.error(r.msg);
         }
@@ -39,7 +39,7 @@ export default {
     },
 
     closeServer () {
-      this.$ipcInvoke(ipcApiRoute.closeJavaServer, {}).then(r => {
+      this.$ipc.invoke(ipcApiRoute.closeJavaServer, {}).then(r => {
         if (r.code != 0) {
           this.$message.error(r.msg);
         }