admin管理员组

文章数量:1602103

1、在控制台安装electron 

1

cnpm install electron -g

2、在控制台安装electron-packager

1

cnpm install electron-packager -g

3、uniapp的manifest.json修改

 

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

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

4、H5打包

 

5、H5文件夹下新建package.json和main.js

新建package.json

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


新建main.js

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

// 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

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({width: 400, height: 700})

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

  // 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
  })
}

// 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.

如果你的网页首页的文件名不是 “index.html”,那么请在 main.js 中将其中的 'index.html' 修改为你的网页首页名。

6、打包

建议使用cmd,本人使用powershell和git hash有踩坑

cmd命令行进入H5目录,输入打包命令

 命令公司如下:

electron-packager . helloWorld --platform=win32 --arch=x64 --icon=computer.ico --out=./out --asar --app-version=1.0.0 --overwrite --ignore=node_modules --electron-version 8.2.1

参数说明:

HelloWorld :你将要生成的exe文件的名称
–platform=win32:确定了你要构建哪个平台的应用,可取的值有 darwin, linux, mas, win32
–arch=x64:决定了使用 x86 还是 x64 还是两个架构都用
–icon=自定义.ico:自定义设置应用图标
–out=./out:指定打包文件输出的文件夹位置,当前指定的为项目目录下的out文件夹
–asar:该参数可以不加,如果加上,打包之后应用的源码会以.asar格式存在
–app-version=1.0.0:生成应用的版本号
–overwrite:覆盖原有的build,让新生成的包覆盖原来的包
–ignore=node_modules:如果加上该参数,项目里node_modules模块不会被打包进去
–electron-version 8.2.1:指定当前要构建的electron的电子版本(不带"v"),需要和当前的版本一致,具体可以在 package.json文件中查看,可以不加该参数,如果不一致,会自动下载。

eg:

electron-packager . exam考试系统 --win --icon=computer.ico --out exam考试系统 --arch=x64 --electron-version 1.0.0 --overwrite --ignore=node_modules

【打包慢,无反应】

(1)更换为阿里镜像源 命令行下输入

npm config set ELECTRON_MIRROR http://npm.taobao/mirrors/electron/

(2)修改环境变量

我的电脑->右键->属性->高级系统设置->高级->环境变量->新建

切换到淘宝源,然后打包就好了。

7、更新内容后再次使用HbuilderX生成h5前记得备份

package.json
main.js

8、打包成功后,根目录会生成一个新的文件夹,点进去,找到 exe 文件,双击就可以看到网页变成了一个桌面应用啦!

本文标签: 步骤桌面方法项目uniapp