当前位置: 首页 > wzjs >正文

中英文网站制作chrome浏览器官网入口

中英文网站制作,chrome浏览器官网入口,政府网站建设会主持词,传奇类游戏前言 electron应用打包后,希望可以实现自动嗅探新版本,下载新版本并覆盖安装。 准备工作 项目中使用的技术栈:electron react,打包工具为electron-builder,升级工具为electron-updater。项目中使用了框架electron-pro…

前言

electron应用打包后,希望可以实现自动嗅探新版本,下载新版本并覆盖安装。

准备工作

项目中使用的技术栈:electron + react,打包工具为electron-builder,升级工具为electron-updater。项目中使用了框架electron-prokit,其中封装了ipc的过程。
需要一个放置软件包的存储服务器,这边使用了Amazon S3,最好安装桌面工具S3 Brower,简化开发过程。
对于S3,公司内部会有自己的Endpoint,并配置AK和SK。配置好后创建Bucket,确认软件包的存放路径。
例如Endpoint是abc.com,Bucket名称为test,那么首先需要把test的Bucket Policy修改为

{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Principal": "*","Action": "s3:GetObject","Resource": "arn:aws:s3:::test/*"}]
}

并把Permissions尽量放开。(感觉这步非必须,还需要试验一下)

配置electron-builder

electron-builder打包时需要生成last.yml,将这个文件上传到存储服务器后,在本地安装的软件通过读取这个文件来获取是否有新的版本。

在electron-builder中需要配置publish的选项,配置之后才会生成last.yml。同时在根目录创建release-notes.md,每次更新时可以把更新内容写在里边,这样也可以在last.yml中读取到。

    "publish": {"provider": "generic","url": "https://'https://abc.com/test/path/'    //endpoint名称/bucket名称/存储路径},"releaseInfo": {"releaseNotesFile": "release-notes.md"}

主进程代码编写

在electron的主进程文件夹中创建一个update.js,用来注册更新过程。

import { Notification, BrowserWindow } from 'electron';
import { autoUpdater } from 'electron-updater';
import { sendMsgToRender } from 'electron-prokit';export async function initUpdate(mainWindow: BrowserWindow) {autoUpdater.forceDevUpdateConfig = true;autoUpdater.disableWebInstaller = false;autoUpdater.autoDownload = false;autoUpdater.setFeedURL('https://abc.com/test/path/') //endpoint名称/bucket名称/存储路径autoUpdater.checkForUpdates();autoUpdater.on('update-available', (info) => {sendMsgToRender('main', { key: 'update-available', info });});autoUpdater.on('update-not-available', () => {new Notification({ title: '无更新可用', body: '无更新可用' }).show();});autoUpdater.on('download-progress', (progressObj) => {sendMsgToRender('main', { key: 'download-progress', progressObj });});autoUpdater.on('update-downloaded', () => {sendMsgToRender('main', 'update-downloaded');});autoUpdater.on('error', (error) => {new Notification({ title: '更新错误', body: `更新失败: ${error}` }).show();sendMsgToRender('main', { key: 'update-error', error });});
}export function checkUpdate() {autoUpdater.checkForUpdates();
}export async function downloadUpdate() {await autoUpdater.downloadUpdate();
}export function installUpdate() {autoUpdater.quitAndInstall();
}

在创建窗口后调用initUpdate()

app.whenReady().then(() => {// 创建窗口createWindow();// 检查更新initUpdate()();}).catch(console.log);

创建窗口后的initUpdate中首先去check有无新版本,如果没有新版本,则会弹出Notification,如果有新版本,会给主窗口发ipc消息,key为update-available
并在ipc的注册函数中进行downloadUpdate和installUpdate的注册,以便接受渲染进程传进来的消息。

渲染进程

在渲染进程中需要创建一个更新的弹窗,这里使用react配合antd。

import React, { useState, useEffect, useImperativeHandle } from 'react';
import { onMsgFromMain, sendMsgToMain } from 'electron-prokit';
import { Modal, Flex, Space, Progress, Button } from 'antd';
import Markdown from 'react-markdown';
import { t } from 'i18next';const Index = React.forwardRef((props, ref) => {useImperativeHandle(ref, () => ({showModalHandle,}));const [openModal, setOpenModal] = useState(false);const [newVersion, setNewVersion] = useState('');const [releaseNote, setReleaseNote] = useState('');const [state, setState] = React.useState('');const [downloadPercent, setDownloadPercent] = React.useState(0);const showModalHandle = (info: any) => {setOpenModal(true);setState('update-available');const { version = '', releaseNotes = '' } = info;setNewVersion(version);setReleaseNote(releaseNotes);};const handleCancel = () => {setOpenModal(false);};const handleDownload = () => {sendMsgToMain({key: 'downloadUpdate'});};const handleInstall = () => {sendMsgToMain({key: 'newInstall'});};useEffect(() => {onMsgFromMain((event, args) => {if (args.key === 'download-progress') {setState('download-progress');setDownloadPercent(args.progressObj.percent.toFixed(2));}if (args === 'update-downloaded') {setState('update-downloaded');}if (args.key === 'update-error') {setState('update-error');console.error('update-error', args.error)setTimeout(() => {setOpenModal(false);}, 10000);}});});const footer = (<>{state === 'download-progress' && null}{state === 'update-unavailable' && null}{state === 'update-available' &&<><Buttontype="primary"onClick={handleDownload}>{t('update')}</Button><Button onClick={handleCancel}>{t('Update later')}</Button></>}{state === 'update-downloaded' &&<><Buttontype="primary"onClick={handleInstall}>{t('update')}</Button><Button onClick={handleCancel}>{t('close')}</Button></>}{state === 'update-error' &&<Button onClick={handleCancel}>{t('close')}</Button>}</>);return (<Modalwidth={400}open={openModal}title={`${t('New version is available')}: ${newVersion}`}onCancel={handleCancel}closable={false}footer={footer}maskClosable={false}><Flex style={{ height: 150 }}><Space direction="vertical" style={{ width: '100%', padding: '0px 20px', overflow: 'auto' }}>{state === 'update-available' && <Markdown>{releaseNote}</Markdown>}{state === 'download-progress' && <Flex justify={'center'} align={'center'}><Progress type="circle" percent={downloadPercent} /></Flex>}{state === 'update-unavailable' && <div>{t('No new version available')}</div>}{state === 'update-downloaded' && <div>{t('The latest version has been downloaded. Do you want to update it')}</div>}{state === 'update-error' && <div dangerouslySetInnerHTML={{ __html: t('Update error') }} />}</Space></Flex></Modal>);
});export default Index;

在根组件中引用此Modal,并接收key为update-available时showModal。

import UpdateModal from './UpdateModal'
...const updateRef = useRef(null);useEffect(() => {onMsgFromMain((_event: unknown, args: any) => {if (args.key === 'update-available') {updateRef.current.showModalHandle(args.info);}});}
...
<UpdateModal ref={updateRef}></UpdateModal>

升级功能基本实现。

其他

Notification

在electron自带的Notification中,想要设置展示头,可以在app注册时设置

app.setAppUserModelId('Test')

覆盖安装

有可能是起项目时候有什么问题(很老的项目),发现下载下来的安装包不能覆盖安装,报软件无法关闭的错误。
这里可以配置nsis

    "nsis": {"oneClick": false,"perMachine": false,"createDesktopShortcut": true,"createStartMenuShortcut": true,"guid": "Test","include": "build/install/test.nsh","shortcutName": "Test","artifactName": "Test Setup ${version}.${ext}","allowElevation": false,"allowToChangeInstallationDirectory": true,"deleteAppDataOnUninstall": true,"runAfterFinish": false},

其中test.nsh中添加:

!macro customInit
DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\Test"
!macroend

在每次安装之前把已安装版本卸载,即可解决这个问题。

http://www.dtcms.com/wzjs/199714.html

相关文章:

  • 西安网站优化公司今日新闻头条新闻
  • 最新上市新手机seo搜索如何优化
  • 梅州做网站怎么请专业拓客团队
  • 求职简历模板免费下载佛山seo培训机构
  • 东莞做汽车有没有买票的网站做网络营销推广
  • 手机网站和app有什么区别上海自媒体推广
  • 我的世界查询建筑网站注册推广赚钱一个40元
  • 网站产品说明书模板seo人员工作内容
  • wordpress 付费下载吉林百度seo公司
  • wordpress 微信公众平台seo免费课程
  • 怎么在网站做视频接口福州关键词搜索排名
  • 域名和空间都有了怎么做网站今天最新的新闻头条新闻
  • 关于开通网站建设的请示百度查询关键词排名工具
  • jsp做网站都可以做什么网络营销的含义特点
  • 久久建筑网论坛宁波专业seo服务
  • 做a 需要制作网站今日最新国际新闻
  • 做网站的平台建网站免费
  • 自建网站过程seo如何优化图片
  • 汕头网站排名优化图片外链在线生成网址
  • 苏州企业名录黄页全专业优化公司
  • 大丰做网站的公司西安seo推广
  • 要怎么做自己的网站视频教学网络营销成功案例ppt
  • 彩票网站模版企业策划咨询公司
  • 做网站推广要注意的事项写手接单平台
  • 寺院网站建设软文营销的概念
  • 网站域名怎么修改培训机构不退钱最怕什么举报
  • 苏州优秀网站设计sem是什么的缩写
  • 南京 网站开发成都网站seo收费标准
  • 卖网站赚钱吗优化精灵
  • 成都定制软件开发公司seo优化师就业前景