vue获取IP地址 | 您所在的位置:网站首页 › vue获取客户端mac地址 › vue获取IP地址 |
vue–用户登录需要使用用户Ip
1、创建一个Ip.js export function getUserIP(onNewIP) { let MyPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; let pc = new MyPeerConnection({ iceServers: [] }); let noop = () => { }; let localIPs = {}; let ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g; let iterateIP = (ip) => { if (!localIPs[ip]) onNewIP(ip); localIPs[ip] = true; }; pc.createDataChannel(''); pc.createOffer().then((sdp) => { sdp.sdp.split('\n').forEach(function (line) { if (line.indexOf('candidate') { }); pc.onicecandidate = (ice) => { if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return; ice.candidate.candidate.match(ipRegex).forEach(iterateIP); }; }2、在vue页面引用 import {getUserIP} from '@/utils/ip'3、created中调用 created(){ getUserIP((ip) => { this.ip = ip; }) },几天后,呜呜呜~~~ 这个方法使用ios系统登录网址是获取不到的IP地址的~~~
vue项目: 在 public 文件夹中 index.html 加如下代码:(注:使用的搜狐接口) sessionStorage.setItem('ip', returnCitySN["cip"])在需要传ip地址的地方获取就可以了 this.lastIp = sessionStorage.getItem('ip')原文链接:https://www.cnblogs.com/l-coil/p/12790668.html |
CopyRight 2018-2019 实验室设备网 版权所有 |