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') < 0) return;line.match(ipRegex).forEach(iterateIP);});pc.setLocalDescription(sdp, noop, noop);}).catch((reason) => {});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地址的~~~
那只能获取外网ip了
vue项目:
在 public 文件夹中 index.html 加如下代码:(注:使用的搜狐接口)
<script src="/cityjson?ie=utf-8"></script><script type="text/javascript">sessionStorage.setItem('ip', returnCitySN["cip"])</script>
在需要传ip地址的地方获取就可以了
this.lastIp = sessionStorage.getItem('ip')
原文链接:/l-coil/p/12790668.html