在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中将本地电脑的物理地址存在了环境变量process.env.osCups中,然后其他客户端的电脑在访问时读取了这个环境变量的值,所以得到的还是本地电脑的物理地址而不是客户端电脑的物理地址。
解决这个问题的方法是,在服务器端获取客户端电脑的物理地址,并将其传递给客户端的vue实例。下面是一个可能的解决方案:
```javascript const os = require("os"); const express = require("express"); const app = express();
app.get("/getPhysicalAddress", (req, res) => { const networkInterfaces = os.networkInterfaces(); // 在这里选择一个合适的物理地址,例如第一个不是回环地址的地址 const physicalAddress = Object.values(networkInterfaces)[0].find( (interface) => !interface.internal ).mac; res.json({ physicalAddress }); });
app.listen(3000, () => { console.log("Server is running on port 3000"); }); ```
```javascript // 首先安装axios:npm install axios --save import axios from "axios";
mounted() { axios .get("/getPhysicalAddress") .then((response) => { this.physicalAddress = response.data.physicalAddress; }) .catch((error) => { console.error(error); }); }, ```
javascript data() { return { physicalAddress: "" }; },
{{ physicalAddress }}
输出客户端电脑的物理地址。这样修改后,客户端电脑在访问项目时会去服务器端获取自己的物理地址,无论在哪台电脑上打开,得到的都应该是该客户端电脑的物理地址了。