前言
2021年了,vue3开发也需要加紧提上日程,最近一直在捣鼓Electron+Vue3结合的项目。前段时间有给大家分享一个vue3仿抖音短视频实例项目。今天带来的是最新研发的Vue3+Electron跨平台电脑端聊天exe应用程序。
采用无边框模式,自定义导航栏菜单。
基本实现了图文消息、图片/视频预览、截图、拖拽发送图片、朋友圈等功能。
搭建技术
- 编码+技术:vscode | vue3.0+electron11.2.3+vuex4+vue-router@4
- 组件库:ant-design-vue (阿里桌面端vue3组件库)
- 弹窗组件:v3layer(vue3自定义弹窗组件)
- 滚动条组件:v3scroll(vue3自定义滚动条组件)
- 打包工具:vue-cli-plugin-electron-builder
- 按需引入:babel-plugin-import^1.13.3
目录结构
electron+vue3自定义无边框导航条
为了让项目UI更加漂亮,采用了frame:false
无边框窗体,所以需要自定义顶部导航菜单。通过-webkit-app-region:drag
快速实现一个可拖拽区域。
拖拽区域里面的元素无法响应点击事件,可以通过设置-webkit-app-region:no-drag
来解除。 另外最大化/最小化/关闭按钮也需要自定义实现。
不过有个问题,如上图:拖拽区域右键会有系统菜单,大家如果对项目要求高的话,可以通过下面的方法快速屏蔽掉。
// 屏蔽系统右键菜单
win.hookWindowMessage(278, () => {
win.setEnabled(false)
setTimeout(() => {
win.setEnabled(true)
}, 100)
return true
})
由于之前有过相关分享,大家感兴趣可以去看看下面这篇文章。
electron多开器|父子modal窗口
项目支持多个窗口同时存在,父子模态窗口。
// 关于窗口
const handleAboutWin = () => {
createWin({
title: '关于',
route: '/about',
width: 380,
height: 280,
resize: false,
parent: winCfg.window.id,
modal: true,
})
}
// 换肤窗口
const handleSkinWin = () => {
createWin({
title: '换肤',
route: '/skin',
width: 720,
height: 475,
resize: false,
})
}
// 朋友圈窗口
const handleFZoneWin = () => {
createWin({
title: '朋友圈',
route: '/fzone',
width: 550,
height: 700,
resize: false,
})
}
// 界面管理器窗口
const handleUIManager = () => {
createWin({
title: '界面管理器',
route: '/uimanager',
width: 400,
height: 475,
resize: false,
parent: winCfg.window.id,
modal: true,
})
}
通过如上方法即可快速生成一个新窗口,只需传入配置参数就行。
之前也有过相关分享文章,这里就不详细介绍了,大家可以去看下。
electron实现系统托盘
窗体关闭的时候会提示,直接退出程序,还是最小化到托盘。
const handleWinClose = () => {
if(winCfg.window.isMainWin) {
let $el = v3layer({
type: 'android',
content: '是否最小化至托盘,不退出程序?',
btns: [
{
text: '残忍退出',
style: 'color:#ff5438',
click: () => {
$el.close()
store.commit('LOGOUT')
setWin('close')
}
},
{
text: '最小化至托盘',
style: 'color:#00d2ff',
click: () => {
$el.close()
win.hide()
}
}
]
})
}else {
setWin('close', winCfg.window.id)
}
}
大家需要提前准备两个大小一致的ico图标,一个透明即可。通过定时器控制切换显示。
// 创建系统托盘图标
let tray = null
let flashTimer = null
let trayIco1 = path.join(__dirname, '../static/tray.ico')
let trayIco2 = path.join(__dirname, '../static/tray-empty.ico')
createTray() {
const trayMenu = Menu.buildFromTemplate([
{
label: '我在线上', icon: path.join(__dirname, '../static/icon-online.png'),
click: () => {...}
},
{
label: '忙碌', icon: path.join(__dirname, '../static/icon-busy.png'),
click: () => {...}
},
{
label: '隐身', icon: path.join(__dirname, '../static/icon-invisible.png'),
click: () => {...}
},
{
label: '离开', icon: path.join(__dirname, '../static/icon-offline.png'),
click: () => {...}
},
{type: 'separator'},
{
label: '关闭所有声音', click: () => {...},
},
{
label: '关闭头像闪动', click: () => {
this.flashTray(false)
}
},
{type: 'separator'},
{
label: '打开主窗口', click: () => {
try {
for(let i in this.winLs) {
let win = this.getWin(i)
if(!win) return
if(win.isMinimized()) win.restore()
win.show()
}
} catch (error) {
console.log(error)
}
}
},
{
label: '退出', click: () => {
try {
for(let i in this.winLs) {
let win = this.getWin(i)
if(win) win.webContents.send('win-logout')
}
app.quit()
} catch (error) {
console.log(error)
}
}
},
])
this.tray = new Tray(this.trayIco1)
this.tray.setContextMenu(trayMenu)
this.tray.setToolTip(app.name)
this.tray.on('double-click', () => {
// ...
})
}
// 托盘图标闪烁
flashTray(flash) {
let hasIco = false
if(flash) {
if(this.flashTimer) return
this.flashTimer = setInterval(() => {
this.tray.setImage(hasIco ? this.trayIco1 : this.trayIco2)
hasIco = !hasIco
}, 500)
}else {
if(this.flashTimer) {
clearInterval(this.flashTimer)
this.flashTimer = null
}
this.tray.setImage(this.trayIco1)
}
}
// 销毁托盘图标
destoryTray() {
this.flashTray(false)
this.tray.destroy()
this.tray = null
}
electron-builder打包配置
项目根目录下的vue.config.js
文件,可以进行简单的项目配置及electron打包配置。
/**
* @Desc vue3项目配置文件
* @Time andy by 2021-02
* @About Q:282310962 wx:xy190310
*/
const path = require('path')
module.exports = {
// 基本路径
// publicPath: '/',
// 输出文件目录
// outputDir: 'dist',
// assetsDir: '',
// 环境配置
devServer: {
// host: 'localhost',
// port: 8080,
// 是否开启https
https: false,
// 编译完是否打开网页
open: false,
// 代理配置
// proxy: {
// '^/api': {
// target: '<url>',
// ws: true,
// changeOrigin: true
// },
// '^/foo': {
// target: '<other_url>'
// }
// }
},
// webpack配置
chainWebpack: config => {
// 配置路径别名
config.resolve.alias
.set('@', path.join(__dirname, 'src'))
.set('@assets', path.join(__dirname, 'src/assets'))
.set('@components', path.join(__dirname, 'src/components'))
.set('@module', path.join(__dirname, 'src/module'))
.set('@plugins', path.join(__dirname, 'src/plugins'))
.set('@layouts', path.join(__dirname, 'src/layouts'))
.set('@views', path.join(__dirname, 'src/views'))
},
// 插件配置
pluginOptions: {
electronBuilder: {
// 配置后可以在渲染进程使用ipcRenderer
nodeIntegration: true,
// 项目打包参数配置
builderOptions: {
"productName": "electron-qchat", //项目名称 打包生成exe的前缀名
"appId": "com.example.electronqchat", //包名
"compression": "maximum", //store|normal|maximum 打包压缩情况(store速度较快)
"artifactName": "${productName}-${version}-${platform}-${arch}.${ext}", //打包后安装包名称
// "directories": {
// "output": "build", //输出文件夹(默认dist_electron)
// },
"asar": false, //asar打包
// 拷贝静态资源目录到指定位置
"extraResources": [
{
"from": "./static",
"to": "static"
},
],
"nsis": {
"oneClick": false, //一键安装
"allowToChangeInstallationDirectory": true, //允许修改安装目录
"perMachine": true, //是否开启安装时权限设置(此电脑或当前用户)
"artifactName": "${productName}-${version}-${platform}-${arch}-setup.${ext}", //打包后安装包名称
"deleteAppDataOnUninstall": true, //卸载时删除数据
"createDesktopShortcut": true, //创建桌面图标
"createStartMenuShortcut": true, //创建开始菜单图标
"shortcutName": "ElectronQChat", //桌面快捷键图标名称
},
"win": {
"icon": "./static/shortcut.ico", //图标路径
}
}
}
}
}
ok,以上就是electron+vue3开发跨平台聊天应用实例。希望大家喜欢哈~~✏️