瀏覽代碼

update ipc demo

gaoshuaixing 4 年之前
父節點
當前提交
9ea58a9294

+ 1 - 40
electron/apis/base.js

@@ -1,5 +1,5 @@
 'use strict'
-const { app, dialog } = require('electron')
+
 const AutoLaunchManager = require('../lib/AutoLaunch')
 
 exports.autoLaunchEnable = function () {
@@ -19,42 +19,3 @@ exports.autoLaunchIsEnabled = function () {
   const isEnable = autoLaunchManager.isEnabled()
   return isEnable
 }
-
-
-exports.appExit = function () {
-  app.exit()
-}
-
-exports.appRelaunch = function () {
-  app.relaunch()
-  app.exit()
-}
-
-
-/**
- * 选择本地文件夹
- * @param title 弹出框的标题
- * @return {Promise<*>}
- */
-exports.choiceFolder = async function (title = '') {
-  return await dialog.showOpenDialog({
-    properties: ['openDirectory'],
-    title: title
-  })
-}
-
-/**
- * 选择本地文件
- * @param title 弹出框的标题
- * @param extensions 后缀名集合 e.g: ['exe','txt','png']
- * @return {Promise<*>}
- */
-exports.choiceFile = async function (title = '', extensions = []) {
-  return await dialog.showOpenDialog({
-    properties: ['openFile'],
-    filters: [{
-      extensions: extensions
-    }],
-    title: title
-  })
-}

+ 5 - 3
electron/ipc/example.js

@@ -1,6 +1,8 @@
 const { answerRenderer } = require('./index')
 
-answerRenderer('example.test', async (name) => {
-  const luckNum = (Math.random()*1000).toFixed()
-  return `${name}, 你的幸运数字是:${luckNum}`
+answerRenderer('example.hello', async (msg) => {
+  let newMsg = msg + " +1"
+  let reply = ''
+  reply = '收到:' + msg + ',返回:' + newMsg
+  return reply
 })

+ 1 - 1
electron/ipc/index.js

@@ -28,7 +28,7 @@ module.exports.answerRenderer = (channel, callback) => {
 /**
  * 加载所有的主程序
  */
-module.exports.loadIPC = () => {
+module.exports.setup = () => {
   const ipcDir = path.normalize(__dirname + '/')
 
   fs.readdirSync(ipcDir).forEach(function (filename) {

+ 0 - 22
electron/ipc/system.js

@@ -1,22 +0,0 @@
-const { appExit, appRelaunch, choiceFile, choiceFolder } = require('../apis/base')
-const { answerRenderer } = require('./index')
-
-/**
- * 退出app
- */
-answerRenderer('system.exit', appExit)
-
-/**
- * 重启app
- */
-answerRenderer('system.relaunch', appRelaunch)
-
-/**
- * 选择系统文件夹
- */
-answerRenderer('system.choiceFolder', choiceFolder)
-
-/**
- * 选择文件文件
- */
-answerRenderer('system.choiceFile', choiceFile)

+ 5 - 1
electron/setup.js

@@ -8,17 +8,21 @@ const api = require('./api');
 const ipc = require('./ipc');
 
 module.exports = () => {
+  // 存储模块
   storage.setup();
+  // 日志
   logger();
+  // 自动更新
   const updateConfig = config.get('autoUpdate');
   if ((is.windows() && updateConfig.windows) || (is.macOS() && updateConfig.macOS)
     || (is.linux() && updateConfig.linux)) {
     const autoUpdater = require('./autoUpdater');
     autoUpdater.setup();
   }
+  // electron业务模块
   api.setup();
   // 加载所有的主进程函数
-  ipc.loadIPC();
+  ipc.setup();
 }
 
 function logger () {

+ 5 - 5
frontend/src/config/router.config.js

@@ -10,17 +10,17 @@ export const constantRouterMap = [
       {
         path: 'fileOpenDir',
         name: 'FileOpenDir',
-        component: () => import('@/views/file/OpenDir')
+        component: () => import('@/views/example/OpenDir')
       },
       {
         path: 'uploadFile',
         name: 'UploadFile',
-        component: () => import('@/views/file/UploadFile')
+        component: () => import('@/views/example/UploadFile')
       },
       {
-        path: 'ipcExample',
-        name: 'IpcExample',
-        component: () => import('@/views/file/IpcExample')
+        path: 'ipc',
+        name: 'Ipc',
+        component: () => import('@/views/example/Ipc')
       },
       {
         path: 'setting',

+ 2 - 2
frontend/src/views/Layout.vue

@@ -65,8 +65,8 @@ export default {
             params: {},
           },
           'subMenu_3' : {
-            title: 'IpcExample',
-            pageName: 'IpcExample',
+            title: '通信',
+            pageName: 'Ipc',
             params: {},
           }
         },

+ 34 - 0
frontend/src/views/example/Ipc.vue

@@ -0,0 +1,34 @@
+<template>
+  <div>
+    <h3 :style="{ marginBottom: '16px' }">
+      demo3 渲染进程与主进程IPC通信
+    </h3>
+    <a-list bordered>
+      <!-- <a-button @click="helloHandle">打招呼</a-button> -->
+      <a-input-search v-model="content" @search="helloHandle">
+        <a-button slot="enterButton">
+          send
+        </a-button>
+      </a-input-search>
+    </a-list>
+  </div>
+</template>
+<script>
+export default {
+  data() {
+    return {
+      content: 'hello',
+      reply: ''
+    }
+  },
+  methods: {
+    helloHandle(value) {
+      const self = this;
+      this.$callMain('example.hello', value).then(r => {
+        self.$message.info(r);
+      })
+    }
+  }
+}
+</script>
+<style></style>

+ 0 - 0
frontend/src/views/file/OpenDir.vue → frontend/src/views/example/OpenDir.vue


+ 0 - 0
frontend/src/views/file/UploadFile.vue → frontend/src/views/example/UploadFile.vue


+ 0 - 45
frontend/src/views/file/IpcExample.vue

@@ -1,45 +0,0 @@
-<template>
-  <div>
-    <h3 :style="{ marginBottom: '16px' }">
-      渲染进程与主进程IPC通信Demo
-    </h3>
-    <a-list bordered>
-      <a-button @click="choiceFolder">选择本地文件夹</a-button>
-      <div>{{ choiceFolderInfo }}</div>
-      <a-button @click="choiceFile">选择本地文件</a-button>
-      <div>{{ choiceFileInfo }}</div>
-
-      <a-button @click="getMyLuckNum">摇一个幸运数字</a-button>
-      <div>{{ luckNum }}</div>
-    </a-list>
-  </div>
-</template>
-<script>
-const getMyLuckNum = require('./luckNum')
-export default {
-  data() {
-    return {
-      choiceFolderInfo: '',
-      choiceFileInfo: '',
-      luckNum: ''
-    }
-  },
-  methods: {
-    choiceFolder() {
-      this.$callMain('system.choiceFolder', '我要选择系统的文件夹').then(r => {
-        this.choiceFolderInfo = JSON.stringify(r)
-      })
-    },
-    choiceFile() {
-      this.$callMain('system.choiceFile', '我只选择excel文件', ['xlsx', 'xls']).then(r => {
-        this.choiceFileInfo = JSON.stringify(r)
-      })
-    },
-    getMyLuckNum() {
-      // 在外部js中调用主进程函数
-      getMyLuckNum(this, 'CNNN').then(r => this.luckNum = r)
-    }
-  }
-}
-</script>
-<style></style>

+ 0 - 5
frontend/src/views/file/luckNum.js

@@ -1,5 +0,0 @@
-function getMyLuckNum(vue, name) {
-  return vue.$callMain('example.test', name)
-}
-
-module.exports = getMyLuckNum