优化生产环境静态文件加载

This commit is contained in:
zdg 2024-07-30 10:04:49 +08:00
parent 604a20b9b0
commit 45770d15c1
4 changed files with 54 additions and 14 deletions

View File

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

View File

@ -75,8 +75,10 @@ function createMainWindow() {
mainWindow.show()
})
mainWindow.on('closed', () => {
mainWindow = null
app.quit() // 主窗口关闭-结束所有进程
setTimeout(() => { // 延迟销毁
mainWindow = null
}, 1000)
// app.quit() // 主窗口关闭-结束所有进程
})
mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
@ -163,7 +165,10 @@ app.on('ready', () => {
loginWindow.destroy()
}
if (mainWindow) {
mainWindow.destroy()
mainWindow.close() // 先发出这个关闭指令
setTimeout(() => { //
mainWindow.destroy()
}, 200);
}
})
@ -190,6 +195,14 @@ app.on('ready', () => {
ipcMain.on('openWindow', (e, 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()
@ -197,6 +210,7 @@ app.on('ready', () => {
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createLoginWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common

View File

@ -3,7 +3,8 @@
*/
// const { ipcRenderer } = window.electron || {}
// 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 Remote = require('@electron/remote')
@ -13,7 +14,22 @@ const { ipcRenderer } = require('electron')
const BaseUrl = process.env['ELECTRON_RENDERER_URL']+'/#'
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 消息发送
* @form src/main/tool.js 来源
@ -55,6 +71,7 @@ export const createWindow = async (type, data) => {
}
data.option = {...defOption, ...option}
const win = await toolWindow(data)
win.setTitle('窗口标题: 我的自定义参数')
win.type = type // 唯一标识
win.show()
win.setFullScreen(true) // 设置窗口为全屏
@ -105,8 +122,11 @@ export function toolWindow({url, isFile, isConsole, option={}}) {
const config = {
width, height,
type: 'toolbar', // 创建的窗口类型为工具栏窗口
icon: path.join(__dirname, '../../resources/logo2.ico'),
webPreferences: {
// preload: path.join(__dirname, '../preload/index.js'),
preload: '@root/src/preload/index.js',
sandbox: false,
nodeIntegration: true, // nodeApi调用
contextIsolation: false, // 沙箱取消
// webSecurity: false // 跨域关闭
@ -136,17 +156,18 @@ export function toolWindow({url, isFile, isConsole, option={}}) {
* @param {*} win 窗口对象
*/
const eventHandles = (type, win) => {
// const winAll = Remote.BrowserWindow.getAllWindows()
// const mainWin = winAll.find(o => o.type == 'main') // 主窗口对象
const winAll = Remote.BrowserWindow.getAllWindows()
const mainWin = winAll.find(o => o.type == 'main') // 主窗口对象
// 公共方法
const publicMethods = ({onClosed}={}) => {
// 监听主窗口-关闭事件
// Remote.ipcMain.on('close-window', () => {console.log('关闭窗口');win.destroy()})
// mainWin.once('closed', () => {console.log('关闭窗口');win.destroy()})
mainWin.once('close', () => {win.destroy()})
win.on('closed', () => {
if(onClosed) onClosed()
if(onClosed) onClosed() // 自定义关闭事件
win = null
})
// 新窗口-创建事件(如:主进程加载远程服务)
ipcRenderer.send('new-window', {id:win.id, type})
}
switch(type) {
case 'tool-sphere': { // 创建-悬浮球
@ -165,6 +186,7 @@ const eventHandles = (type, win) => {
case 'open-PDF': {
// 最小化窗口 minimize()
Remote.ipcMain.once('open-PDF:minimize', () => {win.destroy()})
win.webContents.openDevTools()
publicMethods() // 加载公共方法
break}
default:
@ -180,3 +202,4 @@ const taskHandles = () => {
// click: () => {Remote.app.quit()}
// }))
}

View File

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