diff --git a/electron/electron.js b/electron/electron.js index e60cecb1b..484ce1a30 100644 --- a/electron/electron.js +++ b/electron/electron.js @@ -1,7 +1,7 @@ const fs = require('fs') const os = require("os"); const path = require('path') -const {app, BrowserWindow, ipcMain, dialog, clipboard, nativeImage, shell, Tray, Menu, globalShortcut} = require('electron') +const {app, BrowserWindow, ipcMain, dialog, clipboard, nativeImage, shell, Tray, Menu, globalShortcut, Notification} = require('electron') const {autoUpdater} = require("electron-updater") const log = require("electron-log"); const fsProm = require('fs/promises'); @@ -503,6 +503,21 @@ ipcMain.on('closeScreenshot', (event) => { event.returnValue = "ok" }) +/** + * 通知 + */ +ipcMain.on('openNotification', (event, args) => { + const notifiy = new Notification(args); + notifiy.addListener('click', _ => { + mainWindow.webContents.send("clickNotification", args) + }) + notifiy.addListener('reply', (event, reply) => { + mainWindow.webContents.send("replyNotification", Object.assign(args, {reply})) + }) + notifiy.show() + event.returnValue = "ok" +}) + //================================================================ // Update //================================================================ @@ -557,7 +572,10 @@ ipcMain.on('mainWindowTop', (event) => { /** * 将主窗口激活 */ -ipcMain.on('mainWindowFocus', (event) => { +ipcMain.on('mainWindowActive', (event) => { + if (!mainWindow.isVisible()) { + mainWindow.show() + } mainWindow.focus() event.returnValue = "ok" }) diff --git a/resources/assets/js/pages/manage.vue b/resources/assets/js/pages/manage.vue index 4c30d4656..b928c753b 100644 --- a/resources/assets/js/pages/manage.vue +++ b/resources/assets/js/pages/manage.vue @@ -1018,6 +1018,16 @@ export default { this.$store.dispatch('openDialog', dialog_id) } }) + } else if (this.$Electron) { + this.$Electron.sendMessage('openNotification', { + icon: $A.originUrl('images/logo.png'), + title, + body, + data, + tag: "dialog", + hasReply: true, + replyPlaceholder: this.$L('回复消息') + }) } else { this.notificationManage.replaceOptions({ icon: $A.originUrl('images/logo.png'), @@ -1124,38 +1134,39 @@ export default { } }, + /** + * 初始化通知 + */ notificationInit() { this.notificationManage = new notificationKoro(this.$L("打开通知成功")); if (this.notificationManage.support) { this.notificationManage.notificationEvent({ onclick: ({target}) => { - console.log("[Notification] Click", target); + console.log("[Notification] A Click", target); this.notificationManage.close(); - try { - window.focus() - if (this.$Electron) { - this.$Electron.sendMessage('mainWindowFocus') - } - }catch (e) {} - // - const {tag, data} = target; - if (tag == 'dialog') { - if (!$A.isJson(data)) { - return; - } - this.goForward({name: 'manage-messenger'}); - this.$store.dispatch('openDialog', data.dialog_id) - // - if (this.$Electron) { - this.$Electron.sendMessage('mainWindowTop') - } - } + this.notificationClick(target) + window.focus() }, }); this.notificationPermission(); } + // + if (this.$Electron) { + this.$Electron.registerMsgListener('clickNotification', target => { + console.log("[Notification] B Click", target); + this.$Electron.sendMessage('mainWindowActive') + this.notificationClick(target) + }) + this.$Electron.registerMsgListener('replyNotification', target => { + console.log("[Notification] B Reply", target); + this.notificationReply(target) + }) + } }, + /** + * 通知权限 + */ notificationPermission() { const userSelectFn = msg => { switch (msg) { @@ -1181,6 +1192,48 @@ export default { }; this.notificationManage.initNotification(userSelectFn); }, + + /** + * 点击通知(客户端) + * @param target + */ + notificationClick(target) { + const {tag, data} = target; + if (tag == 'dialog') { + if (!$A.isJson(data)) { + return; + } + this.goForward({name: 'manage-messenger'}); + this.$nextTick(_ => { + this.$store.dispatch('openDialog', data.dialog_id) + }) + } + }, + + /** + * 回复通知(客户端) + * @param target + */ + notificationReply(target) { + const {tag, data, reply} = target; + if (tag == 'dialog' && reply) { + this.$store.dispatch("call", { + url: 'dialog/msg/sendtext', + data: { + dialog_id: data.dialog_id, + text: reply, + }, + method: 'post', + }).then(({data}) => { + this.$store.dispatch("saveDialogMsg", data); + this.$store.dispatch("increaseTaskMsgNum", data); + this.$store.dispatch("increaseMsgReplyNum", data); + this.$store.dispatch("updateDialogLastMsg", data); + }).catch(({msg}) => { + $A.modalError(msg) + }); + } + }, } }