全新原创研发新作Electron-ViteChat桌面端仿微信聊天室,正式完结了。
技术栈
- 编辑器:vscode
- 框架技术:electron31+vite5.3.1+vue3.4.29+vue-router4.4.0
- 跨端框架:electron31.1.0
- 状态管理:pinia^2.1.7
- UI组件库:element-plus^2.7.6 (饿了么桌面端vue3组件库)
- 本地存储:pinia-plugin-persistedstate^3.2.1
- 构建打包:electron-builder^24.13.3
- electron整合vite插件:vite-plugin-electron^0.28.7
electron-vitechat采用vite5.x搭建项目模板,vue3 setup语法糖编码。整合electron跨平台技术框架,支持新开多个窗口并存。
项目结构目录
vite5.x整合electron搭建聊天项目
- vite5创建vue3项目模板
yarn create vite electron-vitechat
cd electron-vitechat
yarn install
yarn dev
接下来,安装一些electron依赖包。如果安装失败或卡住,建议多试几次或切换镜像源。
// 安装electron
yarn add -D electron
// 安装electron-builder 用于打包可安装exe程序和绿色版免安装exe程序
yarn add -D electron-builder
// 安装vite-plugin-electron 用于将vite与electron无缝结合
yarn add -D vite-plugin-electron
electron主进程配置
/**
* electron主进程入口配置
* @author andy
*/
import { app, BrowserWindow } from 'electron'
import { WindowManager } from '../src/windows/index.js'
// 忽略安全警告提示 Electron Security Warning (Insecure Content-Security-Policy)
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = true
const createWindow = () => {
let win = new WindowManager()
win.create({isMajor: true})
// 系统托盘管理
win.trayManager()
// 监听ipcMain事件
win.ipcManager()
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if(BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', () => {
if(process.platform !== 'darwin') app.quit()
})
在vite.config.js中配置主进程入口。
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'node:path'
import electron from 'vite-plugin-electron'
// import electron from 'vite-plugin-electron/simple'
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd())
return {
define: {
'process.env': env, // 将.env环境变量注入到process.env
},
plugins: [
vue(),
electron({
entry: 'electron/main.js',
})
/* electron({
main: {
entry: 'electron/main.js'
},
preload: {
input: 'electron/preload.js'
},
// renderer: {}
}) */
],
// 构建配置
build: {
// ...
},
esbuild: {
// 打包去除 console.log 和 debugger
drop: ['console', 'debugger']
},
// 服务器配置
server: {
// port: 3000,
// open: true,
// https: false,
// proxy: {}
},
resolve: {
// 设置别名
alias: {
'@': resolve(__dirname, 'src'),
'@assets': resolve(__dirname, 'src/assets'),
'@components': resolve(__dirname, 'src/components'),
'@views': resolve(__dirname, 'src/views'),
}
}
}
})
渲染进程主入口main.js
import { createApp } from 'vue'
import './style.scss'
import App from './App.vue'
// 引入组件库
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
// 引入路由/状态管理
import Router from './router'
import Pinia from './pinia'
import { launchApp } from '@/windows/actions'
launchApp().then(config => {
if(config) {
console.log('窗口参数:', config)
console.log('窗口id:', config?.id)
// 全局存储窗口配置
window.config = config
}
// 创建app应用实例
createApp(App)
.use(ElementPlus)
.use(Router)
.use(Pinia)
.mount('#app')
})
项目布局模板
整个项目分为最左侧菜单条+侧边栏+右侧主体区(自定义系统导航条)
<template>
<template v-if="!route?.meta?.isNewWin">
<div
class="vu__container flexbox flex-alignc flex-justifyc"
:style="{'--themeSkin': appstate.config.skin}"
>
<div class="vu__layout flexbox flex-col">
<div class="vu__layout-body flex1 flexbox" @contextmenu.prevent>
<!-- 菜单栏 -->
<slot v-if="!route?.meta?.hideMenuBar" name="menubar">
<MenuBar />
</slot>
<!-- 侧边栏 -->
<div v-if="route?.meta?.showSideBar" class="vu__layout-sidebar flexbox">
<aside class="vu__layout-sidebar__body flexbox flex-col">
<slot name="sidebar">
<SideBar />
</slot>
</aside>
</div>
<!-- 主内容区 -->
<div class="vu__layout-main flex1 flexbox flex-col">
<ToolBar v-if="!route?.meta?.hideToolBar" />
<router-view v-slot="{ Component, route }">
<keep-alive>
<component :is="Component" :key="route.path" />
</keep-alive>
</router-view>
</div>
</div>
</div>
</div>
</template>
<template v-else>
<WinLayout />
</template>
</template>
vue3+electron31自定义拖拽导航窗口
<script setup>
import { ref } from 'vue'
import { isTrue } from '@/utils'
import { winSet } from '@/windows/actions'
import Winbtns from './btns.vue'
const props = defineProps({
// 标题
title: {type: String, default: ''},
// 标题颜色
color: String,
// 背景色
background: String,
// 标题是否居中
center: {type: [Boolean, String], default: false},
// 是否固定
fixed: {type: [Boolean, String], default: false},
// 背景是否镂空
transparent: {type: [Boolean, String], default: false},
// 层级
zIndex: {type: [Number, String], default: 2024},
/* 控制Winbtn参数 */
// 窗口是否可最小化
minimizable: {type: [Boolean, String], default: true},
// 窗口是否可最大化
maximizable: {type: [Boolean, String], default: true},
// 窗口是否可关闭
closable: {type: [Boolean, String], default: true},
})
</script>
<template>
<div class="ev__winbar" :class="{'fixed': fixed || transparent, 'transparent': transparent}">
<div class="ev__winbar-wrap flexbox flex-alignc vu__drag">
<div class="ev__winbar-body flex1 flexbox flex-alignc">
<!-- 左侧区域 -->
<div class="ev__winbar-left"><slot name="left" /></div>
<!-- 标题 -->
<div class="ev__winbar-title" :class="{'center': center}">
<slot name="title">{{title}}</slot>
</div>
<!-- 右侧附加区域 -->
<div class="ev__winbar-extra vu__undrag"><slot name="extra" /></div>
</div>
<Winbtns :color="color" :minimizable="minimizable" :maximizable="maximizable" :closable="closable" :zIndex="zIndex" />
</div>
</div>
</template>
btns.vue模板控制窗口最大化/最小化/关闭功能。
<script setup>
import { ref } from 'vue'
import { isTrue } from '@/utils'
import { winSet } from '@/windows/actions'
const props = defineProps({
color: String,
// 窗口是否可最小化
minimizable: {type: [Boolean, String], default: true},
// 窗口是否可最大化
maximizable: {type: [Boolean, String], default: true},
// 窗口是否可关闭
closable: {type: [Boolean, String], default: true},
// 层级
zIndex: {type: [Number, String], default: 2024},
})
const hasMaximized = ref(false)
const isResizable = ref(true)
const isMaximizable = ref(true)
// 用户是否可以手动调整窗口大小
window.electron.invoke('win-isResizable').then(res => {
isResizable.value = res
})
// 窗口是否可以最大化
window.electron.invoke('win-isMaximizable').then(res => {
isMaximizable.value = res
})
// 初始监听窗口是否最大化
window.electron.invoke('win-isMaximized').then(res => {
hasMaximized.value = res
})
// 实时监听窗口是否最大化
window.electron.on('win-maximized', (e, data) => {
hasMaximized.value = data
})
// 最小化
const handleWinMin = () => {
// winSet('minimize', window.config.id)
window.electron.invoke('win-min')
}
// 最大化/还原
const handleWinToggle = () => {
// winSet('max2min', window.config.id)
window.electron.invoke('win-toggle').then(res => {
hasMaximized.value = res
})
}
// 关闭
const handleWinClose = () => {
if(window.config.isMajor) {
let el = layer({
type: 'android',
content: '是否最小化托盘,不退出程序?',
layerStyle: 'background: #f9f9f9; border-radius: 8px;',
closable: false,
resize: false,
btns: [
{
text: '最小化托盘',
style: 'color: #646cff',
click: () => {
layer.close(el)
setTimeout(() => {
winSet('hide', window.config.id)
}, 300)
}
},
{
text: '退出',
style: 'color: #fa5151',
click: () => {
winSet('close')
}
}
]
})
}else {
winSet('close', window.config.id)
}
}
</script>
<template>
<div class="ev__winbtns flexbox flex-alignc vu__drag" :style="{'z-index': zIndex}">
<div class="ev__winbtns-actions flexbox flex-alignc vu__undrag" :style="{'color': color}">
<a v-if="isTrue(minimizable)" class="wbtn min" title="最小化" @click="handleWinMin"><i class="wicon elec-icon elec-icon-min"></i></a>
<a v-if="isTrue(maximizable) && isResizable && isMaximizable" class="wbtn toggle" :title="hasMaximized ? '向下还原' : '最大化'" @click="handleWinToggle">
<i class="wicon elec-icon" :class="hasMaximized ? 'elec-icon-restore' : 'elec-icon-max'"></i>
</a>
<a v-if="isTrue(closable)" class="wbtn close" title="关闭" @click="handleWinClose"><i class="wicon elec-icon elec-icon-quit"></i></a>
</div>
</div>
</template>
vite5+electron31实现多窗口实例
/**
* 创建新窗口
* @param {object} args 窗口配置参数 {url: '/chat', width: 850, height: 600, ...}
*/
export function winCreate(args) {
window.electron.send('win-create', args)
}
通过给主进程发送创建窗口指令,便可快速开启一个独立窗体。
// 登录窗口
export function loginWindow() {
winCreate({
url: '/login',
title: '登录',
width: 320,
height: 380,
isMajor: true,
resizable: false,
maximizable: false,
alwaysOnTop: true
})
}
// 关于窗口
export function aboutWindow() {
winCreate({
url: '/win/about',
title: '关于',
width: 375,
height: 300,
minWidth: 375,
minHeight: 300,
maximizable: false,
alwaysOnTop: true,
})
}
// 设置窗口
export function settingWindow() {
winCreate({
url: '/win/setting',
title: '设置',
width: 550,
height: 470,
resizable: false,
maximizable: false,
})
}
在主进程中监听创建窗口事件。
/**
* 主进程监听管理器
* 监听从渲染器进程(网页)发送出来的异步和同步信息。 从渲染器进程发送的消息将被发送到该模块。
*/
ipcManager() {
console.log('watching ipc event...')
/* ipcMain模块on监听 */
// 创建新窗口
ipcMain.on('win-create', (event, args) => this.create(args))
//...
}
通过new BrowserWindow()创建新窗口实例。
// 创建新窗口
create(options) {
console.log('create window started...')
// 窗口自定义配置参数
const windowConfig = mergeObjects(windowOptions, options)
// 窗口系统配置参数
const windowBaseConfig = mergeObjects(windowBaseOptions, options)
// 判断窗口是否存在
for(let i in this.winDict) {
let win = this.getWinById(i)
if(win && this.winDict[i].url === windowConfig.url && !this.winDict[i].isMultiple && !this.winDict[i].isMajor) {
win.restore()
win.focus()
return
}
}
// 设置父窗口
if(windowBaseConfig.parent) {
console.log('window parent: ', windowBaseConfig.parent)
windowBaseConfig.parent = this.getWinById(windowBaseConfig.parent)
}
// 创建窗口对象
let winObj = new BrowserWindow(windowBaseConfig)
// 是否主窗口
if(windowConfig.isMajor) {
// ...
}
// 加载页面
let url
if(!windowConfig.url) {
if(process.env.VITE_DEV_SERVER_URL) {
url = process.env.VITE_DEV_SERVER_URL
}else {
url = winURL
}
}else {
url = `${winURL}#${windowConfig.url}`
}
winObj.loadURL(url)
windowConfig.id = winObj.id
this.winDict[winObj.id] = windowConfig
console.log('winDict:', this.winDict)
// 开启开发者调试工具
if(isDev) {
// console.log('open devtools...')
// winObj.webContents.openDevTools({mode: 'bottom'})
}
winObj.once('ready-to-show', () => {
winObj.show()
})
// 初始化渲染进程
winObj.webContents.on('did-finish-load', () => {
this.sendById(winObj.id, 'win-loaded', windowConfig)
})
}
electron打包配置
在项目根目录新建一个electron-builder.json配置文件。
{
"productName": "Electron-ViteChat",
"appId": "com.andy.electron-vite-wechat",
"copyright": "Copyright © 2024-present Andy Q:282310962",
"compression": "maximum",
"asar": true,
"directories": {
"output": "release/${version}"
},
"nsis": {
"oneClick": false,
"allowToChangeInstallationDirectory": true,
"perMachine": true,
"deleteAppDataOnUninstall": true,
"createDesktopShortcut": true,
"createStartMenuShortcut": true,
"shortcutName": "ElectronViteChat"
},
"win": {
"icon": "./resources/shortcut.ico",
"artifactName": "${productName}-v${version}-${platform}-${arch}-setup.${ext}",
"target": [
{
"target": "nsis",
"arch": ["ia32"]
}
]
},
"mac": {
"icon": "./resources/shortcut.icns",
"artifactName": "${productName}-v${version}-${platform}-${arch}-setup.${ext}"
},
"linux": {
"icon": "./resources",
"artifactName": "${productName}-v${version}-${platform}-${arch}-setup.${ext}"
}
}
Okay,以上就是electron31+vite5开发桌面跨端聊天项目的一些分享,由于涉及到的知识点还是蛮多,限于篇幅就先分享到这里,感谢大家的阅读!