uniapp打包桌面应用exe

uniapp具有打包app等功能,最近接手的项目中也实现了uniapp打包桌面应用exe

文章目录

一、插件Electron

简单介绍下Electron:

Electron 是一个使用 JavaScript、HTML 和 CSS 构建跨平台的桌面应用程序。它基于 Node.js 和 Chromium,被 Atom 编辑器和许多其他应用程序使用。

Electron 兼容 Mac、Windows 和 Linux,可以构建出三个平台的应用程序。

二、使用步骤

1.全局安装electron插件

代码如下(示例):

npm install electron -g // 安装electron(主程序)
npm install electron-packager -g // 安装electron-packager(打包用)

会出现两种问题:

  • 问题1:解决npm下载过慢问题
安装淘宝的cnpm:
#在任意目录执行都可以
--global意思为安装到全局
npm install --global cnpm

如果不想安装cnpm又想使用淘宝的服务器来下载包
npm install jquery --registry=https://registry.npm.taobao.org

但是每一次手动这样加参数很麻烦,所以我们可以把这个选项加入配置文件中:
npm config set registry https://registry.npm.taobao.org

  • 问题2:在安装electron时出现以下报错:

connect ETIMEDOUT 20.205.243.166:443

解决办法:

ping GitHub.com

如果能ping通,继续以下操作

更改electron安装源。
npm config set ELECTRON_MIRROR https://npmmirror.com/mirrors/electron/

安装成功

2.修改manifest.json

代码如下(示例):

h5配置

运行的基础路径修改为./  不然打包出来会出现白屏,读取不到,因为打包出来的h5默认加载地址为/static/

去掉启用https协议不然会出现网络无法加载,去掉https不影响你请求后端的https协议。

图示如下:

3.H5打包

4.在打包后生成的H5文件夹下新建package.json和main.js

代码示例如下:

  • package.json
{  "name"    : "exam考试系统",  "version" : "0.1.0",  "main"    : "main.js"}

如果报错:
error couldn't find a package.json file in

解决办法:

这个错误提示是说在当前目录下找不到packagejson文件。在使用npm安装依赖包时,需要在项目根目录下执行命令,确保当前目录下
有 package.json 文件。如果没有,可以通过执行 npm init命令来生成一个新的package.ison文件。

执行 npm init命令来生成一个新的package.ison文件如下:

完整代码:
{
"name": "你的名字(注意不能是中文不能大写)",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"win": {
"icon": "icon.ico"(注意,此处为打包exe文件后生成桌面应用的图标,注意将图标位置放置正确,可直接放在生成的H5中)
}
}
  • 新建main.js

注意点

1.主要是index.html和icon图标放置(示例中index.html是在根目录下)

2.解决点击窗口最大化样式整体过大问题(默认是打开全屏,,项目需要打开应用不全屏,直接设置宽高会出现此问题)

监听 will-resize 事件

Electron 中的 BrowserWindow 在每次重新缩放大小前会触发will-resize事件,我们只要拦截这个事件,并手动设置窗口大小为想要的大小。

//解决点击窗口最大化样式整体过大问题
const WIDTH = 1860
const HEIGHT = 900
const aspectRatio = WIDTH / HEIGHT // 窗口宽高比

win = new BrowserWindow({
// fullscreen: true,
// width: 800,
// height: 600,
width: WIDTH,
height: HEIGHT,
// frame: false, // 无边框窗口
webPreferences: {
nodeIntegration: true, // 使渲染进程拥有node环境

}

})


win.once('ready-to-show', () => {
// 限制窗口最小尺寸(int整形), 无边框模式下,不考虑标题栏高度
win.setMinimumSize(WIDTH / 2, HEIGHT / 2)
win.show()
})

// 控制等比缩放
win.on('will-resize', resizeWindow)

function resizeWindow(event, newBounds) {
const wins = event.sender
event.preventDefault() // 拦截,使窗口先不变
const currentSize = wins.getSize()
const widthChanged = currentSize[0] !== newBounds.width // 判断是宽变了还是高变了,两者都变优先按宽适配
// ! 虽然搞不懂为何有1px偏差,但是可以解决问题(Windows 10)
if (widthChanged) {
wins.setContentSize(newBounds.width - 1, parseInt(newBounds.width / aspectRatio + 0.5) - 1)
} else {
wins.setContentSize(parseInt(aspectRatio * newBounds.height + 0.5) - 1, newBounds.height - 1)
}
}

3.win.webContents.openDevTools()  打开调试框 --------(shift + ctrl + i 快捷键

4.Menu.setApplicationMenu(null)关闭顶部导航菜单栏

完整main.js代码如下:

const {
app,
BrowserWindow,
ipcMain
} = require('electron')
const fs = require('fs')
const electron = require('electron')
const path = require('path')
const url = require('url')

// ipcMain.on('asynchronous-message', function(event, arg) {
//    // arg是从渲染进程返回来的数据
//   console.log(arg,1);

//   // 这里是传给渲染进程的数据C:/Users/Administrator/Desktop/test.txt
//   fs.readFile(path.join(__dirname,"./test.txt"),"utf8",(err,data)=>{
//    if(err){
// event.sender.send('asynchronous-reply', "读取失败");
// }else{
// event.sender.send('asynchronous-reply', data);
// }

//   })
// });

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win
const Menu = electron.Menu

function createWindow() {
// Create the browser window.
// Menu.setApplicationMenu(null)

//解决点击窗口最大化样式整体过大问题
const WIDTH = 1860
const HEIGHT = 900
const aspectRatio = WIDTH / HEIGHT // 窗口宽高比


win = new BrowserWindow({
// fullscreen: true,(设置默认打开满屏)
// width: 800,
// height: 600,
width: WIDTH,
height: HEIGHT,
// frame: false, // 无边框窗口
webPreferences: {
nodeIntegration: true, // 使渲染进程拥有node环境

},
icon: path.join(__dirname, 'icon.ico') // 设置ico

})


win.once('ready-to-show', () => {
// 限制窗口最小尺寸(int整形), 无边框模式下,不考虑标题栏高度
win.setMinimumSize(WIDTH / 2, HEIGHT / 2)
win.show()
})

// 控制等比缩放
win.on('will-resize', resizeWindow)

function resizeWindow(event, newBounds) {
const wins = event.sender
event.preventDefault() // 拦截,使窗口先不变
const currentSize = wins.getSize()
const widthChanged = currentSize[0] !== newBounds.width // 判断是宽变了还是高变了,两者都变优先按宽适配
// ! 虽然搞不懂为何有1px偏差,但是可以解决问题(Windows 10)
if (widthChanged) {
wins.setContentSize(newBounds.width - 1, parseInt(newBounds.width / aspectRatio + 0.5) - 1)
} else {
wins.setContentSize(parseInt(aspectRatio * newBounds.height + 0.5) - 1, newBounds.height - 1)
}
}

// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
//cpu


// Open the DevTools.
// win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null
})

// ipcMain.on('asynchronous-message', function(event, arg) {
//    // arg是从渲染进程返回来的数据
//   console.log(arg,2);

//   // 这里是传给渲染进程的数据C:/Users/Administrator/Desktop/test.txt
//   fs.readFile(path.join(__dirname,"./test.txt"),"utf8",(err,data)=>{
//    if(err){
// event.sender.send('asynchronous-reply', "读取失败");
// }else{
// event.sender.send('asynchronous-reply', data);
// }

//   })
// });
}

// 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', createWindow)

// 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 (win === null) {
createWindow()
}
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.



注:解决窗口样式过大问题时会出现一个问题

点击右侧滑动会出现以下报错

Error
A JavaScript error occurred in the main process
Uncaught Exception:
TypeError: Cannot read properties of undefined (reading 'getSize')
at BrowserWindow.resizeWindow
(E:\desktop\shb-win32-x64\resources\app.asar\main.js:71:28)
at BrowserWindow.emit(node:events:513:28)

目前此问题还未解决,还请各位大佬不吝赐教

5.最终打包

1.electron-packager . 可执行文件的文件名 --win --out 打包成的文件夹名 --arch=x64位还是32位 --electron-version版本号(不是你的h5版本号,是electron版本号) --overwrite --ignore=node_modules
例如:electron-packager . myAPP --win --out appDist --arch=x64 --electron-version 23.1.0 --overwrite --ignore=node_modules

如果先打包速度慢,可采取镜像打包方法

2.electron-packager . appName --platform=win32 --electron-version=10.0.0 --arch=x64 --download.mirrorOptions.mirror=https://npm.taobao.org/mirrors/electron/ --asar --app-version=0.0.0 --build-version=0.0.0 --out=outName --overwrite --no-package-manager --ignore="(.git)" --icon=logo.ico

运行命令打包,然后项目中会出现一个 App-win32-x64 的文件夹,这个文件就是打包好的桌面应用,文件夹里有一个 App.exe 文件,App.exe就是这个项目的启动文件


总结

以上就是今天要讲的内容,本文仅简单介绍了uniapp打包桌面应用exe,而electron提供了大量能使我们快速便捷地处理数据的函数和方法,具体可参考electron官网

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值