From 0ec1a7327a385ad17c7466ff7f40fae85505f294 Mon Sep 17 00:00:00 2001 From: lyc Date: Thu, 25 Jul 2024 16:45:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dev-app-update.yml | 2 +- electron-builder-test.yml | 46 +++++++++++++++++++++++++++++ electron-builder.yml | 2 +- package.json | 4 ++- src/main/index.js | 7 +++-- src/main/update.js | 61 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 electron-builder-test.yml create mode 100644 src/main/update.js diff --git a/dev-app-update.yml b/dev-app-update.yml index 0a21494..0083a29 100644 --- a/dev-app-update.yml +++ b/dev-app-update.yml @@ -1,3 +1,3 @@ provider: generic -url: https://example.com/auto-updates +url: http://localhost:3000/ updaterCacheDirName: electron-app-updater diff --git a/electron-builder-test.yml b/electron-builder-test.yml new file mode 100644 index 0000000..86eda95 --- /dev/null +++ b/electron-builder-test.yml @@ -0,0 +1,46 @@ +appId: com.electron.app +productName: AIx +directories: + buildResources: build +files: + - '!**/.vscode/*' + - '!src/*' + - '!electron.vite.config.{js,ts,mjs,cjs}' + - '!{.eslintignore,.eslintrc.cjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}' + - '!{.env,.env.*,.npmrc,pnpm-lock.yaml}' +asarUnpack: + - resources/** +win: + executableName: AIx +nsis: + oneClick: false + allowToChangeInstallationDirectory: true + artifactName: ${name}-${version}-setup.${ext} + shortcutName: ${productName} + uninstallDisplayName: ${productName} + createDesktopShortcut: always +mac: + entitlementsInherit: build/entitlements.mac.plist + extendInfo: + - NSCameraUsageDescription: Application requests access to the device's camera. + - NSMicrophoneUsageDescription: Application requests access to the device's microphone. + - NSDocumentsFolderUsageDescription: Application requests access to the user's Documents folder. + - NSDownloadsFolderUsageDescription: Application requests access to the user's Downloads folder. + notarize: false +dmg: + artifactName: ${name}-${version}.${ext} +linux: + target: + - AppImage + - snap + - deb + maintainer: electronjs.org + category: Utility +appImage: + artifactName: ${name}-${version}.${ext} +npmRebuild: false +publish: + provider: generic + url: http://localhost:3000 +electronDownload: + mirror: https://npmmirror.com/mirrors/electron/ diff --git a/electron-builder.yml b/electron-builder.yml index 52741c5..2834cf9 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -41,6 +41,6 @@ appImage: npmRebuild: false publish: provider: generic - url: https://example.com/auto-updates + url: https://file.ysaix.com:7868/src/assets/smarttalk/ electronDownload: mirror: https://npmmirror.com/mirrors/electron/ diff --git a/package.json b/package.json index c92e305..31fd129 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "build": "electron-vite build", "postinstall": "electron-builder install-app-deps", "build:unpack": "npm run build && electron-builder --dir", - "build:win": "npm run build && electron-builder --win", + "build:test": "npm run build && electron-builder --win --config ./electron-builder-test.yml", + "build:prod": "npm run build && electron-builder --win --config ./electron-builder.yml", "build:mac": "npm run build && electron-builder --mac", "build:linux": "npm run build && electron-builder --linux" }, @@ -24,6 +25,7 @@ "@vueuse/core": "^10.11.0", "crypto-js": "^4.2.0", "electron-dl-manager": "^3.0.0", + "electron-log": "^5.1.7", "electron-updater": "^6.1.7", "element-plus": "^2.7.6", "js-cookie": "^3.0.5", diff --git a/src/main/index.js b/src/main/index.js index 12395b8..7acd4ca 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -3,6 +3,7 @@ import { join } from 'path' import { electronApp, optimizer, is } from '@electron-toolkit/utils' import icon from '../../resources/icon.png?asset' import File from './file' +import updateInit from './update' File({ app, shell, BrowserWindow, ipcMain }) process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true' @@ -31,13 +32,14 @@ function createLoginWindow() { loginWindow.loadURL('http://localhost:5173/#/login') } else { loginWindow.loadFile(join(__dirname, '../renderer/index.html'), {hash: 'login'}) + updateInit(loginWindow) } - loginWindow.webContents.openDevTools() + // loginWindow.webContents.openDevTools() loginWindow.once('ready-to-show', () => { loginWindow.show() }) - + loginWindow.on('closed', () => { loginWindow = null }) @@ -114,7 +116,6 @@ async function createLinkWin(data) { // 初始化完成 app.on('ready', () => { - // 设置应用程序用户模型标识符 electronApp.setAppUserModelId('com.electron') diff --git a/src/main/update.js b/src/main/update.js new file mode 100644 index 0000000..d59d155 --- /dev/null +++ b/src/main/update.js @@ -0,0 +1,61 @@ +import { dialog } from 'electron' +import logger from 'electron-log' +const updateURL = 'http://27.128.240.72:3000/zhuhao/AIx_Smarttalk/releases/tag/V1.0.0%28%E6%B5%8B%E8%AF%95%E7%89%88%29/' + +// 主进程中的更新检查 +const { autoUpdater } = require('electron-updater') + +const updateInit = (win) => { + logger.info('进来了') + // 检查更新 + autoUpdater.checkForUpdates() + // 自动下载 + autoUpdater.autoDownload = false + // 设置版本更新服务器地址 + // autoUpdater.setFeedURL(updateURL) + + //监听更新事件 + autoUpdater.on('update-available', (info) => { + logger.info('发现新版本') + dialog + .showMessageBox(win,{ + type: 'info', + title: '新版本可用', + message: '有一个可用的新版本,要更新吗', + buttons: ['是', '否'] + }) + .then((result) => { + if (result.response === 0) { + // 用户选择更新,触发下载和安装 + autoUpdater.downloadUpdate() + } + }) + }) + + // 没有新版本 + autoUpdater.on('update-not-available', () => { + logger.info('没有新版本') + }) + + // 更新发生错误 + autoUpdater.on('error', () => { + logger.error('检查更新失败') + }) + + // 跟新下载完毕 + autoUpdater.on('update-downloaded', () => { + dialog + .showMessageBox({ + type: 'info', + title: '更新下载完成', + message: '点击确定重启获取最新内容', + buttons: ['确定'] + }) + .then(() => { + // 调用 quitAndInstall 来安装更新 + autoUpdater.quitAndInstall() + }) + }) +} + +export default updateInit \ No newline at end of file