From 1e7e5e4ed981f24dc39f2422518b6878a439b5aa Mon Sep 17 00:00:00 2001 From: gsx <530353222@qq.com> Date: Sun, 14 May 2023 21:17:59 +0800 Subject: [PATCH] demo ipc --- electron/controller/framework.js | 2 +- electron/controller/os.js | 26 ++++++++++----------- frontend/src/views/framework/socket/Ipc.vue | 16 ++++++------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/electron/controller/framework.js b/electron/controller/framework.js index 03adcf5..d89105d 100644 --- a/electron/controller/framework.js +++ b/electron/controller/framework.js @@ -217,7 +217,7 @@ class FrameworkController extends Controller { /** * 异步消息类型 */ - async ipcInvokeMsg(args, event) { + async ipcInvokeMsg(args, event) { let timeNow = dayjs().format('YYYY-MM-DD HH:mm:ss'); const data = args + ' - ' + timeNow; diff --git a/electron/controller/os.js b/electron/controller/os.js index 5009bf5..c50349e 100644 --- a/electron/controller/os.js +++ b/electron/controller/os.js @@ -29,7 +29,7 @@ class OsController extends Controller { /** * 消息提示对话框 */ - messageShow () { + messageShow() { dialog.showMessageBoxSync({ type: 'info', // "none", "info", "error", "question" 或者 "warning" title: '自定义标题-message', @@ -43,7 +43,7 @@ class OsController extends Controller { /** * 消息提示与确认对话框 */ - messageShowConfirm () { + messageShowConfirm() { const res = dialog.showMessageBoxSync({ type: 'info', title: '自定义标题-message', @@ -61,7 +61,7 @@ class OsController extends Controller { /** * 选择目录 */ - selectFolder () { + selectFolder() { const filePaths = dialog.showOpenDialogSync({ properties: ['openDirectory', 'createDirectory'] }); @@ -76,7 +76,7 @@ class OsController extends Controller { /** * 打开目录 */ - openDirectory (args) { + openDirectory(args) { if (!args.id) { return false; } @@ -94,7 +94,7 @@ class OsController extends Controller { /** * 加载视图内容 */ - loadViewContent (args) { + loadViewContent(args) { const { type, content } = args; let contentUrl = content; if (type == 'html') { @@ -109,7 +109,7 @@ class OsController extends Controller { /** * 移除视图内容 */ - removeViewContent () { + removeViewContent() { this.service.os.removeBrowserView(); @@ -119,7 +119,7 @@ class OsController extends Controller { /** * 打开新窗口 */ - createWindow (args) { + createWindow(args) { const { type, content, windowName, windowTitle } = args; let contentUrl = null; if (type == 'html') { @@ -155,7 +155,7 @@ class OsController extends Controller { /** * 获取窗口contents id */ - getWCid (args) { + getWCid(args) { const addonWindow = this.app.addon.window; // 主窗口的name默认是main,其它窗口name开发者自己定义 @@ -189,7 +189,7 @@ class OsController extends Controller { /** * 创建系统通知 */ - sendNotification (args, event) { + sendNotification(args, event) { const { title, subtitle, body, silent} = args; if (!Notification.isSupported()) { @@ -218,7 +218,7 @@ class OsController extends Controller { /** * 电源监控 */ - initPowerMonitor (args, event) { + initPowerMonitor(args, event) { const channel = 'controller.os.initPowerMonitor'; powerMonitor.on('on-ac', (e) => { let data = { @@ -258,7 +258,7 @@ class OsController extends Controller { /** * 获取屏幕信息 */ - getScreen (args) { + getScreen(args) { let data = []; let res = {}; if (args == 0) { @@ -322,7 +322,7 @@ class OsController extends Controller { /** * 获取系统主题 */ - getTheme () { + getTheme() { let theme = 'system'; if (nativeTheme.shouldUseHighContrastColors) { theme = 'light'; @@ -336,7 +336,7 @@ class OsController extends Controller { /** * 设置系统主题 */ - setTheme (args) { + setTheme(args) { // TODO 好像没有什么明显效果 nativeTheme.themeSource = args; diff --git a/frontend/src/views/framework/socket/Ipc.vue b/frontend/src/views/framework/socket/Ipc.vue index 9b56db0..f2f4041 100644 --- a/frontend/src/views/framework/socket/Ipc.vue +++ b/frontend/src/views/framework/socket/Ipc.vue @@ -79,13 +79,12 @@ export default { }, methods: { init () { - const self = this; // 避免重复监听,或者将 on 功能写到一个统一的地方,只加载一次 this.$ipc.removeAllListeners(ipcApiRoute.ipcSendMsg); this.$ipc.on(ipcApiRoute.ipcSendMsg, (event, result) => { console.log('[ipcRenderer] [socketMsgStart] result:', result); - self.messageString = result; + this.messageString = result; // 调用后端的另一个接口 event.sender.send(ipcApiRoute.hello, 'electron-egg'); }) @@ -110,28 +109,27 @@ export default { } this.$ipc.send(ipcApiRoute.ipcSendMsg, params) }, - handleInvoke () { - const self = this; + handleInvoke() { this.$ipc.invoke(ipcApiRoute.ipcInvokeMsg, '异步-回调').then(r => { console.log('r:', r); - self.message1 = r; + this.message1 = r; }); }, - async handleInvoke2 () { + async handleInvoke2() { const msg = await this.$ipc.invoke(ipcApiRoute.ipcInvokeMsg, '异步'); console.log('msg:', msg); this.message2 = msg; }, - handleSendSync () { + handleSendSync() { const msg = this.$ipc.sendSync(ipcApiRoute.ipcSendSyncMsg, '同步'); this.message3 = msg; }, - createWindow (index) { + createWindow(index) { this.$ipc.invoke(ipcApiRoute.createWindow, this.views[index]).then(id => { console.log('[createWindow] id:', id); }) }, - async sendTosubWindow () { + async sendTosubWindow() { // 新窗口id this.newWcId = await this.$ipc.invoke(ipcApiRoute.getWCid, this.windowName); this.$ipc.sendTo(this.newWcId, specialIpcRoute.window1ToWindow2, '窗口1通过 sendTo 给窗口2发送消息');