Vue gets the IP address

Vue – user login needs to use user Ip

1. Create an 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. Reference on the vue page

import {
    
    getUserIP}  from '@/utils/ip'

3. Called in created

  created(){
    
    
    getUserIP((ip) => {
    
    
        this.ip = ip;
      })
  },

A few days later, woo woo woo~~~

This method uses the ios system login URL to obtain the IP address that cannot be obtained~~~

insert image description here
That can only get the external network ip

Vue project:
Add the following code to index.html in the public folder: (Note: Sohu interface used)

 <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
 <script type="text/javascript">
        sessionStorage.setItem('ip', returnCitySN["cip"])
 </script>

Just get it where you need to pass the ip address

 this.lastIp = sessionStorage.getItem('ip')

insert image description here

Original link: https://www.cnblogs.com/l-coil/p/12790668.html

Supongo que te gusta

Origin blog.csdn.net/sxmzhw/article/details/122100419
Recomendado
Clasificación