在avue框架中使用Node.js OS 模块中的os.networkInterfaces()方法,将该方法在vue.config.js引入
const os = require("os");
let OS_CPUS = os.networkInterfaces()
module.exports = {
chainWebpack: config => {
const entry = config.entry('app')
entry
.add('babel-polyfill')
.end()
entry
.add('classlist-polyfill')
.end()
config.plugin("define").tap((args) => {
args[0]["process.env"].osCups = JSON.stringify(OS_CPUS);
return args;
});
},
}
在其他页面使用console.log(process.env.osCups)
从得到的数据中获取本机的物理地址: 30-9C-23-D8-30-WE
将项目更新服务器后,在其他的客户端电脑打开获取的本机物理地址是我自己的,而不是这台客户端电脑
【以下回答由 GPT 生成】
解决方案:
问题的原因是你在vue.config.js
中将os.networkInterfaces()
的结果赋值给process.env.osCups
,而process.env
是指向当前Node.js进程的环境变量对象,并不具有在不同电脑之间共享的能力。因此,在其他电脑上访问process.env.osCups
仍然会返回你自己电脑的物理地址。
要解决这个问题,你需要在客户端电脑上获取物理地址,并将其提交到服务器。服务器再将这个地址发送给其他客户端电脑。
下面是一个可能的解决方案:
navigator.getBattery()
来获取设备的电量信息。然后,根据电量信息生成一个唯一标识符作为物理地址。以下是示例代码:function generatePhysicalAddress(batteryLevel) {
// 使用设备电量和其他信息生成一个唯一标识符
let uniqueIdentifier = batteryLevel + navigator.userAgent + navigator.platform;
let hash = 0;
for (let i = 0; i < uniqueIdentifier.length; i++) {
let char = uniqueIdentifier.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
}
let physicalAddress = hash.toString(16).toUpperCase().substring(0, 12); // 获取前12个字符作为物理地址
return physicalAddress;
}
// 使用Battery API获取电量信息
navigator.getBattery().then(function(battery) {
let batteryLevel = battery.level;
let physicalAddress = generatePhysicalAddress(batteryLevel);
// 在这里将physicalAddress提交到服务器
// 你可以使用Vue.js内置的Ajax库(例如axios)或原生的XMLHttpRequest来发送请求
});
服务器接收到物理地址后,将其保存到数据库或其他持久化存储中。
其他客户端电脑从服务器获取物理地址。你可以使用Vue.js内置的Ajax库(例如axios)或原生的XMLHttpRequest来发送请求。以下是示例代码:
// 使用Vue.js的axios发送请求
import axios from 'axios';
axios.get('/api/physicalAddress').then(function(response) {
let physicalAddress = response.data;
// 在这里使用物理地址进行相关操作
});
这样,你就可以在其他电脑上获取到正确的物理地址了。
需要注意的是,由于浏览器的限制和安全性考虑,使用Browser API获取的物理地址可能不是完全可靠的,因此仅供参考。