Vite5+Electron聊天室|electron31跨平臺仿微信EXE客戶端|vue3聊天程序
基于electron31+vite5+pinia2跨端仿微信Exe聊天應(yīng)用ViteElectronChat。
electron31-vite5-chat原創(chuàng)研發(fā)vite5+electron31+pinia2+element-plus跨平臺實戰(zhàn)仿微信客戶端聊天應(yīng)用。實現(xiàn)了聊天、聯(lián)系人、收藏、朋友圈/短視頻等模塊。支持electron多開窗口管理、壁紙皮膚、自定義最大化/最小化/關(guān)閉等功能。

Electron38-Wechat電腦端聊天|vite7+electron38仿微信桌面端聊天系統(tǒng)
使用技術(shù)
- 編輯器:vscode
- 技術(shù)框架:electron31.1.0+vite5.3.1+vue3.4.29+vue-router4.4.0
- 組件庫:element-plus^2.7.6 (餓了么桌面端vue3組件庫)
- 狀態(tài)管理:pinia^2.1.7
- 存儲服務(wù):pinia-plugin-persistedstate^3.2.1
- 打包構(gòu)建:electron-builder^24.13.3
- electron整合vite插件:vite-plugin-electron^0.28.7


項目結(jié)構(gòu)目錄
electron-vitechat跨平臺聊天項目采用 electron31 整合 vite5.x 構(gòu)建工具開發(fā)項目。


electron31-vue3chat聊天項目已經(jīng)同步到我的原創(chuàng)作品集,感興趣的可以去瞅瞅。
vite.js整合electron創(chuàng)建項目模板
- vite5構(gòu)建工具創(chuàng)建vue3項目模板
yarn create vite electron-vitechat cd electron-vitechat yarn install yarn dev

至此一個簡單的vite5+vue3項目模板就搭建好了。
接下來,需要在項目中安裝一些electron依賴包。如果在安裝過程中卡住或失敗,建議多試幾次或切換淘寶鏡像源。
// 安裝electron yarn add -D electron // 安裝electron-builder 用于打包可安裝exe程序和綠色版免安裝exe程序 yarn add -D electron-builder // 安裝vite-plugin-electron 用于將vite與electron無縫結(jié)合 yarn add -D vite-plugin-electron
至于具體如何整合vite和electron開發(fā)環(huán)境,大家可以去看看介紹文檔。
https://github.com/electron-vite/vite-plugin-electron























electron主線程/預(yù)加載文件

/** * 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}) // 系統(tǒng)托盤管理 win.trayManager() // 監(jiān)聽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() })
/** * electron預(yù)加載文件封裝 * @author andy */ import { contextBridge, ipcRenderer } from 'electron' contextBridge.exposeInMainWorld( 'electron', { // 通過 channel 向主進程發(fā)送異步消息。主進程使用 ipcMain.on() 監(jiān)聽 channel send: (channel, args) => { ipcRenderer.send(channel, args) }, // 通過 channel 向主進程發(fā)送消息,并異步等待結(jié)果。主進程應(yīng)該使用 ipcMain.handle() 監(jiān)聽 channel invoke: (channel, args) => { return new Promise(resolve => ipcRenderer.invoke(channel, args).then(data => resolve(data)).catch(e => console.log(e))) }, // 監(jiān)聽 channel 事件 on: (channel, func) => { console.log('receive event') ipcRenderer.on(channel, (event, ...args) => func(event, ...args)) }, // 一次性監(jiān)聽事件 once: (channel, func) => { ipcRenderer.once(channel, (event, ...args) => func(event, ...args)) }, setTitle: (title) => ipcRenderer.send('win-setTitle', title) } )
項目公共模板
整個項目分為左側(cè)菜單欄+側(cè)邊欄+右側(cè)內(nèi)容區(qū)(自定義導(dǎo)航條)幾個大模塊。


<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> <!-- 側(cè)邊欄 --> <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> <!-- 主內(nèi)容區(qū) --> <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>
electron+vite自定義無邊框拖拽窗口





整個項目采用無邊框 frame: false 模式,采用自定義拖拽導(dǎo)航條。

<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參數(shù) */ // 窗口是否可最小化 minimizable: {type: [Boolean, String], default: true}, // 窗口是否可最大化 maximizable: {type: [Boolean, String], default: true}, // 窗口是否可關(guān)閉 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"> <!-- 左側(cè)區(qū)域 --> <div class="ev__winbar-left"><slot name="left" /></div> <!-- 標題 --> <div class="ev__winbar-title" :class="{'center': center}"> <slot name="title">{{title}}</slot> </div> <!-- 右側(cè)附加區(qū)域 --> <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>
electron+vue3新開多窗口

項目支持同時打開多個窗口,調(diào)用公共封裝函數(shù) winCreate 即可快速創(chuàng)建一個新窗口。
/** * 創(chuàng)建新窗口 * @param {object} args 窗口配置參數(shù) {url: '/about', width: 500, height: 300, ...} */ 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 }) } // 關(guān)于窗口 export function aboutWindow() { winCreate({ url: '/win/about', title: '關(guān)于', width: 375, height: 300, minWidth: 375, minHeight: 300, maximizable: false, alwaysOnTop: true, }) } // 設(shè)置窗口 export function settingWindow() { winCreate({ url: '/win/setting', title: '設(shè)置', width: 550, height: 470, resizable: false, maximizable: false, }) }
支持如下窗口參數(shù)配置
// 自定義窗口參數(shù) const windowOptions = { // 窗口唯一標識id id: null, // 窗口標題 title: 'Electron-ViteChat', // 窗口路由地址 url: '', // 窗口數(shù)據(jù)傳參 data: null, // 是否是主窗口(為true則會關(guān)閉所有窗口并創(chuàng)建一個新窗口) isMajor: false, // 是否支持多開窗口(為true則支持創(chuàng)建多個窗口) isMultiple: false, // 窗口是否最大化 maximize: false, } // 系統(tǒng)窗口參數(shù)(與electron的new BrowserWindow()參數(shù)一致) const windowBaseOptions = { // 窗口圖標 icon: join(__root, 'resources/shortcut.ico'), // 是否自動隱藏菜單欄(按下Alt鍵顯示) autoHideMenuBar: true, // 窗口標題欄樣式 titleBarStyle: 'hidden', // 窗口背景色 backgroundColor: '#fff', // 寬度 width: 840, // 高度 height: 610, // 最小寬度 minWidth: '', // 最小高度 minHeight: '', // 窗口x坐標 x: '', // 窗口y坐標 y: '', // 是否可縮放 resizable: true, // 是否可最小化 minimizable: true, // 是否可最大化 maximizable: true, // 是否可關(guān)閉 closable: true, // 父窗口 parent: null, // 是否模態(tài)窗口 modal: false, // 窗口是否置頂 alwaysOnTop: false, // 是否顯示窗口邊框(要創(chuàng)建無邊框窗口,將frame參數(shù)設(shè)置為false) frame: false, // 是否透明窗口(僅frame: false有效) transparent: false, // 創(chuàng)建時是否顯示窗口 show: false, }
electron創(chuàng)建系統(tǒng)托盤圖標

/** * 系統(tǒng)托盤圖標管理 */ trayManager() { console.log('create tray started...') if(this.tray) return const trayMenu = Menu.buildFromTemplate([ { label: '打開主界面', icon: join(__root, 'resources/tray-win.png'), click: () => { for(let i in this.winDict) { let win = this.getWinById(i) if(!win) return win.restore() win.show() } } }, { label: '設(shè)置', icon: join(__root, 'resources/tray-setting.png'), click: () => this.sendByMainWin('win-ipcdata', {type: 'WINIPC_SETTINGWIN', value: null}) }, { label: '鎖定系統(tǒng)', click: () => null, }, { label: '關(guān)閉托盤閃爍', click: () => this.trayFlash(false) }, { label: '關(guān)于', click: () => this.sendByMainWin('win-ipcdata', {type: 'WINIPC_ABOUTWIN', value: null}) }, { label: '退出聊天室', icon: join(__root, 'resources/tray-exit.png'), click: () => { dialog.showMessageBox(this.winMain, { title: '提示', message: '確定要退出聊天程序嗎?', buttons: ['取消', '最小化托盤', '確認退出'], type: 'error', noLink: false, cancelId: 0, }).then(res => { // console.log(res) const index = res.response if(index == 0) { console.log('用戶取消操作') }else if(index == 1) { console.log('最小化到托盤') this.winMain.hide() }else if(index == 2) { console.log('退出程序') click: () => this.sendByMainWin('win-ipcdata', {type: 'WINIPC_LOGOUT', value: null}) app.quit() } }) } } ]) this.tray = new Tray(this.trayIcon) this.tray.setContextMenu(trayMenu) this.tray.setToolTip(app.name) this.tray.on('double-click', () => { console.log('tray double clicked!') }) }

注意:如果路徑配置不正確,則無法顯示托盤圖標。
// 當前目錄路徑 const __dirname = import.meta.dirname // 根目錄 const __root = join(__dirname, '../../')
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}" } }
OK,綜上就是electron31+vite5開發(fā)桌面端聊天系統(tǒng)的一些知識分享,希望對大家有所幫助!
最新款Electron38+Vite7+Vue3+ElementPlus仿微信桌面端聊天軟件
最后附上幾個最新研發(fā)的實戰(zhàn)項目實例
Uniapp-DeepSeek跨三端AI助手|uniapp+vue3+deepseek-v3流式ai聊天模板
vue3-webseek網(wǎng)頁版AI問答|Vite6+DeepSeek+Arco流式ai聊天打字效果
Electron32-ViteOS桌面版os系統(tǒng)|vue3+electron+arco客戶端OS管理模板
Electron35-DeepSeek桌面端AI系統(tǒng)|vue3.5+electron+arco客戶端ai模板
Tauri2.0+Vite5聊天室|vue3+tauri2+element-plus仿微信|tauri聊天應(yīng)用
Tauri2.0-Vue3OS桌面端os平臺|tauri2+vite6+arco電腦版OS管理系統(tǒng)
uniapp+vue3酒店預(yù)訂|vite5+uniapp預(yù)約訂房系統(tǒng)模板(h5+小程序+App端)
uniapp+vue3聊天室|uni-app+vite4+uv-ui跨端仿微信app聊天語音/朋友圈
tauri2.0-admin桌面端后臺系統(tǒng)|Tauri2+Vite5+ElementPlus管理后臺EXE程序


浙公網(wǎng)安備 33010602011771號