add
吴健
9 天以前 f48c38125956578611832f6017b0cb2ffdbe3725
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict'
 
const { app, protocol, BrowserWindow, ipcMain } = require('electron')
const { createProtocol } = require('vue-cli-plugin-electron-builder/lib')
const path = require('path')
const isDevelopment = process.env.NODE_ENV !== 'production'
const additionalData = { myKey: 'myValue' }
let myWindow = null
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
  { scheme: 'app', privileges: { secure: true, standard: true } }
])
 
async function createWindow() {
  // Create the browser window.
  const win = new BrowserWindow({
    width: 1200,
    height: 800,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
      webSecurity: false,
      preload: path.join(__dirname, '../electron/preload.js')
    }
  })
 
  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    win.loadURL('app://./index.html')
  }
 
  // 处理打印请求
  ipcMain.on('silent-print', (event, data) => {
    const { content, printerName } = data
    const printWindow = new BrowserWindow({
      show: false,
      webPreferences: {
        nodeIntegration: true,
        contextIsolation: false
      }
    })
 
    printWindow.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(content)}`)
 
    printWindow.webContents.on('did-finish-load', () => {
      // 获取所有打印机
      const printers = printWindow.webContents.getPrinters()
      console.log('Available printers:', printers)
 
      // 检查指定的打印机是否存在
      const targetPrinter = printers.find(p => p.name === printerName)
      if (!targetPrinter) {
        console.error('Printer not found:', printerName)
        event.reply('print-complete', { 
          success: false, 
          error: `Printer "${printerName}" not found` 
        })
        printWindow.close()
        return
      }
 
      // 设置打印选项
      const options = {
        silent: true,
        printBackground: true,
        deviceName: printerName,
        margins: {
          marginType: 'none'
        },
        landscape: false,
        scaleFactor: 100
      }
 
      // 执行打印
      printWindow.webContents.print(options, (success, errorType) => {
        console.log('Print result:', success, errorType)
        if (success) {
          event.reply('print-complete', { success: true })
        } else {
          event.reply('print-complete', { 
            success: false, 
            error: errorType || 'Unknown error' 
          })
        }
        printWindow.close()
      })
    })
 
    // 处理加载错误
    printWindow.webContents.on('did-fail-load', (error) => {
      console.error('Failed to load content:', error)
      event.reply('print-complete', { 
        success: false, 
        error: 'Failed to load content' 
      })
      printWindow.close()
    })
  })
}
 
const gotTheLock = app.requestSingleInstanceLock(additionalData)
if (!gotTheLock) {
  app.quit()
} else {
  app.on('second-instance', (event, commandLine, workingDirectory, additionalData) => {
    if (myWindow) {
      if (myWindow.isMinimized()) myWindow.restore()
      myWindow.focus()
    }
  })
 
  // Quit when all windows are closed.
  app.on('window-all-closed', () => {
    // On macOS it is common for applications and their menu bar
    // to stay active until the user quits explicitly with Cmd + Q
    if (process.platform !== 'darwin') {
      app.quit()
    }
  })
 
  app.on('activate', () => {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
 
  // This method will be called when Electron has finished
  // initialization and is ready to create browser windows.
  // Some APIs can only be used after this event occurs.
  app.on('ready', async () => {
    createWindow()
  })
 
  // Exit cleanly on request from parent process in development mode.
  if (isDevelopment) {
    if (process.platform === 'win32') {
      process.on('message', (data) => {
        if (data === 'graceful-exit') {
          app.quit()
        }
      })
    } else {
      process.on('SIGTERM', () => {
        app.quit()
      })
    }
  }
}