马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
vue前端获取差别的客户端mac地点(最详细,制止踩坑)
应用场景:每个账号绑定唯一的电脑可以用网卡的mac地点来做这个唯一的字段。
作用:**可以获取到差别使用者电脑的mac地点,发送给后端**。
直接开始教程:
找了很多教程,很多都是不全的,要么就是实现不了的,以是整理了一份比力详细的制止踩坑的教程
阐明我的环境 使用的是vue-admin-template根本版开辟的
node:16.18.1 npm: 8.19.2 vue :2.6.10 electron : 13.0
一、对现有的项目打包成exe文件,安装之后获取mac地点
1、对如今的vue项目 安装 : vue add electron-builder
报错须要安装vue/cli 安装下令:npm install -g @vue/cli
下载完成会多几个文件,package 也会多一些下令
二、 在vue.config.js内里设置
- module.exports = {
- pluginOptions: {
- electronBuilder: {
- nodeIntegration: true
- }
- },
- }
复制代码 三、开始获取mac地点
在须要mac地点的页面获取,获取的mac地点分为以太网和WLAN- <template>
- <div class="dashboard-container">
- <h1>mac:{{ mac }}</h1>
- <h1>address:{{ address }}</h1>
- </div>
- </template>
- <script>
- import { mapGetters } from "vuex";
- export default {
- name: "Dashboard",
- data() {
- return {
- mac: "",
- address: "",
- };
- },
- computed: {
- ...mapGetters(["name"]),
- },
- async created() {
- this.getMAC();
- },
- methods: {
- getMAC() {
- // 判断是否在Electron中运行
- if (this.isElectron()) {
- const networkInterfaces = require("os").networkInterfaces();
- for (const iface of Object.values(networkInterfaces)) {
- for (const details of iface) {
- if (
- details.family === "IPv4" &&
- details.mac !== "00:00:00:00:00:00" &&
- details.address !== "127.0.0.1"
- ) {
- this.mac = details.mac;
- this.address = details.address;
- }
- }
- }
- } else {
- console.warn("不在 Electron 中运行,跳过获取 MAC 地址");
- }
- },
- isElectron() {
- // 通过判断process.versions.electron来检查是否在Electron环境中运行
- return !!(
- window &&
- window.process &&
- window.process.versions &&
- window.process.versions.electron
- );
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- .dashboard {
- &-container {
- margin: 30px;
- }
- &-text {
- font-size: 30px;
- line-height: 46px;
- }
- }
- </style>
复制代码 四、 之后运行npm run electron:serve大概打包 npm run electron:build即可
须要注意在欣赏器运行的项目是获取不到mac地点的,以是我加了判断,只在electron环境中
可以看到已经有了
|