zdg #82

Merged
zhengdegang merged 5 commits from zdg into main 2024-07-30 10:07:30 +08:00
4 changed files with 54 additions and 14 deletions
Showing only changes of commit 45770d15c1 - Show all commits

View File

@ -34,7 +34,7 @@
"js-cookie": "^3.0.5", "js-cookie": "^3.0.5",
"jsencrypt": "^3.3.2", "jsencrypt": "^3.3.2",
"jsondiffpatch": "0.6.0", "jsondiffpatch": "0.6.0",
"pdfjs-dist": "^4.4.168", "pdfjs-dist": "4.4.168",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"pinia": "^2.1.7", "pinia": "^2.1.7",
"pinia-plugin-persistedstate": "^3.2.1", "pinia-plugin-persistedstate": "^3.2.1",

View File

@ -75,8 +75,10 @@ function createMainWindow() {
mainWindow.show() mainWindow.show()
}) })
mainWindow.on('closed', () => { mainWindow.on('closed', () => {
setTimeout(() => { // 延迟销毁
mainWindow = null mainWindow = null
app.quit() // 主窗口关闭-结束所有进程 }, 1000)
// app.quit() // 主窗口关闭-结束所有进程
}) })
mainWindow.webContents.setWindowOpenHandler((details) => { mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url) shell.openExternal(details.url)
@ -163,7 +165,10 @@ app.on('ready', () => {
loginWindow.destroy() loginWindow.destroy()
} }
if (mainWindow) { if (mainWindow) {
mainWindow.close() // 先发出这个关闭指令
setTimeout(() => { //
mainWindow.destroy() mainWindow.destroy()
}, 200);
} }
}) })
@ -190,6 +195,14 @@ app.on('ready', () => {
ipcMain.on('openWindow', (e, data) => { ipcMain.on('openWindow', (e, data) => {
createLinkWin(data) createLinkWin(data)
}) })
// 新窗口创建-监听
ipcMain.on('new-window', (e,data) => {
const { id, type } = data
const win = BrowserWindow.fromId(id)
win.type = type // 绑定独立标识
remote.enable(win.webContents) // 开启远程服务
})
// 打开-登录窗口 // 打开-登录窗口
createLoginWindow() createLoginWindow()
@ -197,6 +210,7 @@ app.on('ready', () => {
app.on('activate', function () { app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createLoginWindow() if (BrowserWindow.getAllWindows().length === 0) createLoginWindow()
}) })
}) })
// Quit when all windows are closed, except on macOS. There, it's common // Quit when all windows are closed, except on macOS. There, it's common

View File

@ -3,7 +3,8 @@
*/ */
// const { ipcRenderer } = window.electron || {} // const { ipcRenderer } = window.electron || {}
// import { ipcRenderer } from 'electron' // 渲染器里面可以使用ipcRenderer // import { ipcRenderer } from 'electron' // 渲染器里面可以使用ipcRenderer
// Remote.app.getAppPath() E:\njys-work\AIx_Smarttalk\dist\win-unpacked\resources\app.asar
// path.join(__dirname) 根目录 E:\njys-work\AIx_Smarttalk\dist\win-unpacked\resources\app.asar\out\renderer
const path = require('path') const path = require('path')
const Remote = require('@electron/remote') const Remote = require('@electron/remote')
@ -13,7 +14,22 @@ const { ipcRenderer } = require('electron')
const BaseUrl = process.env['ELECTRON_RENDERER_URL']+'/#' const BaseUrl = process.env['ELECTRON_RENDERER_URL']+'/#'
const isDev = process.env.NODE_ENV !== 'production' const isDev = process.env.NODE_ENV !== 'production'
/**
* 获取静态资源开发和生产环境
* @param {*} url
* @param {*} type 类型 app 应用内 user (用户目录 selfFile 自定义文件路径)
* @returns
*/
export const getStaticUrl = (url = '', type = 'app', exitPath = '', isFile = false) => {
if (isDev) return url
else { // 生产环境获取-url
switch(type) {
case 'app': return path.join(__dirname, url) // 应用目录
case 'user': return (isFile?'file://':'')+path.join(Remote.app.getPath('userData'),exitPath, url) // 用户目录
default: return ''
}
}
}
/** /**
* @description 消息发送-nodejs 消息发送 * @description 消息发送-nodejs 消息发送
* @form src/main/tool.js 来源 * @form src/main/tool.js 来源
@ -55,6 +71,7 @@ export const createWindow = async (type, data) => {
} }
data.option = {...defOption, ...option} data.option = {...defOption, ...option}
const win = await toolWindow(data) const win = await toolWindow(data)
win.setTitle('窗口标题: 我的自定义参数')
win.type = type // 唯一标识 win.type = type // 唯一标识
win.show() win.show()
win.setFullScreen(true) // 设置窗口为全屏 win.setFullScreen(true) // 设置窗口为全屏
@ -105,8 +122,11 @@ export function toolWindow({url, isFile, isConsole, option={}}) {
const config = { const config = {
width, height, width, height,
type: 'toolbar', // 创建的窗口类型为工具栏窗口 type: 'toolbar', // 创建的窗口类型为工具栏窗口
icon: path.join(__dirname, '../../resources/logo2.ico'),
webPreferences: { webPreferences: {
// preload: path.join(__dirname, '../preload/index.js'),
preload: '@root/src/preload/index.js', preload: '@root/src/preload/index.js',
sandbox: false,
nodeIntegration: true, // nodeApi调用 nodeIntegration: true, // nodeApi调用
contextIsolation: false, // 沙箱取消 contextIsolation: false, // 沙箱取消
// webSecurity: false // 跨域关闭 // webSecurity: false // 跨域关闭
@ -136,17 +156,18 @@ export function toolWindow({url, isFile, isConsole, option={}}) {
* @param {*} win 窗口对象 * @param {*} win 窗口对象
*/ */
const eventHandles = (type, win) => { const eventHandles = (type, win) => {
// const winAll = Remote.BrowserWindow.getAllWindows() const winAll = Remote.BrowserWindow.getAllWindows()
// const mainWin = winAll.find(o => o.type == 'main') // 主窗口对象 const mainWin = winAll.find(o => o.type == 'main') // 主窗口对象
// 公共方法 // 公共方法
const publicMethods = ({onClosed}={}) => { const publicMethods = ({onClosed}={}) => {
// 监听主窗口-关闭事件 // 监听主窗口-关闭事件
// Remote.ipcMain.on('close-window', () => {console.log('关闭窗口');win.destroy()}) mainWin.once('close', () => {win.destroy()})
// mainWin.once('closed', () => {console.log('关闭窗口');win.destroy()})
win.on('closed', () => { win.on('closed', () => {
if(onClosed) onClosed() if(onClosed) onClosed() // 自定义关闭事件
win = null win = null
}) })
// 新窗口-创建事件(如:主进程加载远程服务)
ipcRenderer.send('new-window', {id:win.id, type})
} }
switch(type) { switch(type) {
case 'tool-sphere': { // 创建-悬浮球 case 'tool-sphere': { // 创建-悬浮球
@ -165,6 +186,7 @@ const eventHandles = (type, win) => {
case 'open-PDF': { case 'open-PDF': {
// 最小化窗口 minimize() // 最小化窗口 minimize()
Remote.ipcMain.once('open-PDF:minimize', () => {win.destroy()}) Remote.ipcMain.once('open-PDF:minimize', () => {win.destroy()})
win.webContents.openDevTools()
publicMethods() // 加载公共方法 publicMethods() // 加载公共方法
break} break}
default: default:
@ -180,3 +202,4 @@ const taskHandles = () => {
// click: () => {Remote.app.quit()} // click: () => {Remote.app.quit()}
// })) // }))
} }

View File

@ -34,15 +34,18 @@
<script setup> <script setup>
import { ref, onMounted, watch, reactive } from 'vue' import { ref, onMounted, watch, reactive } from 'vue'
import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf' import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf'
pdfjsLib.GlobalWorkerOptions.workerSrc = '/lib/build/pdf.worker.mjs'
import pdfCanvas from '@/components/pdf/index.vue' import pdfCanvas from '@/components/pdf/index.vue'
import { getStaticUrl } from '@/utils/tool'
const { ipcRenderer } = require('electron') const { ipcRenderer } = require('electron')
pdfjsLib.GlobalWorkerOptions.workerSrc = getStaticUrl('/lib/build/pdf.worker.mjs')
// //
const pdfObj = reactive({ const pdfObj = reactive({
numberOfPdf: 2, // numberOfPdf: 2, //
pdfUrl: 'aaa.pdf', //url pdfUrl: getStaticUrl('aaa.pdf', 'user', 'selfFile', true),
numPages: 1 // numPages: 1 //
}) })
// //
const numPagesTotal = ref(0) const numPagesTotal = ref(0)
const pdfCanvaslist = ref(null) const pdfCanvaslist = ref(null)
@ -87,7 +90,7 @@ const switchPageMode = () => {
pdfCanvaslist.value.initPdf('restone') pdfCanvaslist.value.initPdf('restone')
} }
} }
onMounted(async () => {console.log('测试: ', pdfObj)}) onMounted(async () => {})
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>