分析
mac地址使用 getmac 可获取。版本信息是在 package.json 配置文件里面,在所需页面 import 导入直接获取使用
没什么好说的, 直接上代码!
完整代码
background.js
const { app, protocol, BrowserWindow, ipcMain } = require('electron')
const { createProtocol } = require('vue-cli-plugin-electron-builder/lib')
const path = require('path');
import getMAC from 'getmac';
ipcMain.handle('get-mac', async (event) => {
return getMAC(); // 返回mac地址
});
preload.js
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('electronApi', {
/**
* 获取设备mac地址
* @returns
*/
getMacInfo: async () => {
return ipcRenderer.invoke('get-mac');
},
});
mac.vue
import config from '../../package.json';//导入electron配置文件,用于获取版本信息
data() {
return {
config:{
version: '',
mac: ''
}
}
},
activated(){
this.config.version = 'V' + config.version;//获取package.json页面的版本信息
window.electronApi.getMacInfo().then(data => {
this.config.mac = data;
}).catch(err => {
console.error('Error fetching mac:', err);
});
},