92 lines
1.9 KiB
JavaScript
92 lines
1.9 KiB
JavaScript
const { app, BrowserWindow, ipcMain } = require('electron')
|
|
const path = require('path')
|
|
const fs = require('fs')
|
|
|
|
const AGREEMENT_FILE = 'user-agreement-accepted.json'
|
|
|
|
function agreementPath() {
|
|
return path.join(app.getPath('userData'), AGREEMENT_FILE)
|
|
}
|
|
|
|
function hasAcceptedAgreement() {
|
|
try {
|
|
return fs.existsSync(agreementPath())
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
function markAgreementAccepted() {
|
|
fs.writeFileSync(
|
|
agreementPath(),
|
|
JSON.stringify({ v: 1, acceptedAt: Date.now() }, null, 2),
|
|
'utf8'
|
|
)
|
|
}
|
|
|
|
/** 占位主窗口;合并到实际桌面端时请替换为真实 loadURL / loadFile */
|
|
function createMainWindow() {
|
|
const win = new BrowserWindow({
|
|
width: 960,
|
|
height: 640,
|
|
webPreferences: {
|
|
contextIsolation: true,
|
|
nodeIntegration: false,
|
|
},
|
|
})
|
|
win.loadURL('about:blank')
|
|
return win
|
|
}
|
|
|
|
function showAgreementGate(onAccepted) {
|
|
const win = new BrowserWindow({
|
|
width: 520,
|
|
height: 680,
|
|
show: false,
|
|
resizable: true,
|
|
minimizable: false,
|
|
maximizable: false,
|
|
fullscreenable: false,
|
|
webPreferences: {
|
|
preload: path.join(__dirname, 'preload.js'),
|
|
contextIsolation: true,
|
|
nodeIntegration: false,
|
|
},
|
|
})
|
|
|
|
win.loadFile(path.join(__dirname, 'agreement.html'))
|
|
win.once('ready-to-show', () => win.show())
|
|
|
|
ipcMain.once('agreement-accept', () => {
|
|
markAgreementAccepted()
|
|
ipcMain.removeAllListeners('agreement-refuse')
|
|
win.close()
|
|
onAccepted()
|
|
})
|
|
|
|
ipcMain.once('agreement-refuse', () => {
|
|
ipcMain.removeAllListeners('agreement-accept')
|
|
app.quit()
|
|
})
|
|
|
|
win.on('closed', () => {
|
|
if (!hasAcceptedAgreement()) {
|
|
app.quit()
|
|
}
|
|
})
|
|
}
|
|
|
|
app.whenReady().then(() => {
|
|
if (!hasAcceptedAgreement()) {
|
|
showAgreementGate(() => createMainWindow())
|
|
} else {
|
|
createMainWindow()
|
|
}
|
|
})
|
|
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit()
|
|
}
|
|
})
|