From 76078bb92d19610cd117e32c29941548abfec7c3 Mon Sep 17 00:00:00 2001
From: Administrator <admin>
Date: Tue, 23 Nov 2021 13:54:47 +0800
Subject: [PATCH] 新增获取本地ip 接口

---
 src/views/securityGuard/sfzJS.js |  116 ++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 69 insertions(+), 47 deletions(-)

diff --git a/src/views/securityGuard/sfzJS.js b/src/views/securityGuard/sfzJS.js
index 5a006d3..4bd044e 100644
--- a/src/views/securityGuard/sfzJS.js
+++ b/src/views/securityGuard/sfzJS.js
@@ -43,27 +43,69 @@
     var xmlHttp = null;
     //根据window.XMLHttpRequest对象是否存在使用不同的创建方式
     if (window.XMLHttpRequest) {
-        xmlHttp = new XMLHttpRequest();                  //FireFox、Opera等浏览器支持的创建方式
-    }
-    else if (window.ActiveXObject) {
-        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器支持的创建方式
+        xmlHttp = new XMLHttpRequest(); //FireFox、Opera等浏览器支持的创建方式
+    } else if (window.ActiveXObject) {
+        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE浏览器支持的创建方式
     }
     xmlHttp.withCredentials = false;
     return xmlHttp;
 }
 
+function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
+    //compatibility for firefox and chrome
+    var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
+    var pc = new myPeerConnection({
+            iceServers: []
+        }),
+        noop = function() {},
+        localIPs = {},
+        ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
+        key;
+
+    function iterateIP(ip) {
+        if (!localIPs[ip]) onNewIP(ip);
+        localIPs[ip] = true;
+    }
+
+    //create a bogus data channel
+    pc.createDataChannel("");
+
+    // create offer and set local description
+    pc.createOffer().then(function(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(function(reason) {
+        // An error occurred, so handle the failure to connect
+    });
+
+    //sten for candidate events
+    pc.onicecandidate = function(ice) {
+        if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
+        ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
+    };
+}
+
 //连接方法
 function CertCtl_connect() {
+    var baseURL = "";
+    getUserIP(function(ip) {
+        // alert("Got IP! :" + ip);
+        baseURL = ip;
+    });
+    console.log(baseURL, 666666);
     var result = "";
     var iParam1 = "1";
     //创建请求 第一个参数是代表以post方式发送;第二个是请求端口和地址;第三个表示是否异步
-    CertCtl.xhr.open("POST", "http://127.0.0.1:18889/api/connect", false);
+    CertCtl.xhr.open("POST", "http://" + baseURL + ":18889/api/connect", false);
     // CertCtl.xhr.open("POST", urls, false);
     //发送请求
     try {
         CertCtl.xhr.send();
-    } catch (e) {
-    }
+    } catch (e) {}
     //返回值readyState   0: 请求未初始化
     //				    1: 服务器连接已建立
     //				    2:请求已接收
@@ -87,8 +129,7 @@
     //发送请求
     try {
         CertCtl.xhr.send();
-    } catch (e) {
-    }
+    } catch (e) {}
     if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
         result = CertCtl.xhr.responseText;
         CertCtl.xhr.readyState = 1;
@@ -103,8 +144,7 @@
     //发送请求
     try {
         CertCtl.xhr.send();
-    } catch (e) {
-    }
+    } catch (e) {}
     if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
         result = CertCtl.xhr.responseText;
         CertCtl.xhr.readyState = 1;
@@ -139,8 +179,7 @@
     //发送请求
     try {
         CertCtl.xhr.send();
-    } catch (e) {
-    }
+    } catch (e) {}
     if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
         result = CertCtl.xhr.responseText;
         CertCtl.xhr.readyState = 1;
@@ -156,8 +195,7 @@
     //发送请求
     try {
         CertCtl.xhr.send();
-    } catch (e) {
-    }
+    } catch (e) {}
     if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
         result = CertCtl.xhr.responseText;
         CertCtl.xhr.readyState = 1;
@@ -175,8 +213,7 @@
     //发送请求
     try {
         CertCtl.xhr.send();
-    } catch (e) {
-    }
+    } catch (e) {}
     if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
         result = CertCtl.xhr.responseText;
         CertCtl.xhr.readyState = 1;
@@ -214,8 +251,7 @@
     //发送请求
     try {
         CertCtl.xhr.send();
-    } catch (e) {
-    }
+    } catch (e) {}
     if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
         result = CertCtl.xhr.responseText;
         CertCtl.xhr.readyState = 1;
@@ -256,8 +292,7 @@
     //发送请求
     try {
         CertCtl.xhr.send();
-    } catch (e) {
-    }
+    } catch (e) {}
     if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
         result = CertCtl.xhr.responseText;
         CertCtl.xhr.readyState = 1;
@@ -277,8 +312,7 @@
     //发送请求
     try {
         CertCtl.xhr.send();
-    } catch (e) {
-    }
+    } catch (e) {}
     if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
         result = CertCtl.xhr.responseText;
         CertCtl.xhr.readyState = 1;
@@ -321,8 +355,7 @@
     //发送请求
     try {
         CertCtl.xhr.send();
-    } catch (e) {
-    }
+    } catch (e) {}
     if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
         result = CertCtl.xhr.responseText;
         CertCtl.xhr.readyState = 1;
@@ -339,8 +372,7 @@
     //发送请求
     try {
         CertCtl.xhr.send();
-    } catch (e) {
-    }
+    } catch (e) {}
     if (CertCtl.xhr.readyState == 4 && CertCtl.xhr.status == 200) {
         result = CertCtl.xhr.responseText;
         CertCtl.xhr.readyState = 1;
@@ -401,8 +433,7 @@
             //result页面回显
             document.getElementById("result").value = result;
         }
-    } catch (e) {
-    }
+    } catch (e) {}
 }
 
 //断开连接方法
@@ -420,8 +451,7 @@
             //result页面回显
             document.getElementById("result").value = result;
         }
-    } catch (e) {
-    }
+    } catch (e) {}
 }
 
 //获取状态方法
@@ -439,8 +469,7 @@
             //result页面回显
             document.getElementById("result").value = result;
         }
-    } catch (e) {
-    }
+    } catch (e) {}
 }
 
 //读卡方法
@@ -509,8 +538,7 @@
             //result页面回显
             document.getElementById("result").value = result;
         }
-    } catch (e) {
-    }
+    } catch (e) {}
 }
 
 function readIDCardSN() {
@@ -527,8 +555,7 @@
             //result页面回显
             document.getElementById("result").value = result;
         }
-    } catch (e) {
-    }
+    } catch (e) {}
 }
 
 function getSAMID() {
@@ -545,8 +572,7 @@
             //result页面回显
             document.getElementById("result").value = result;
         }
-    } catch (e) {
-    }
+    } catch (e) {}
 }
 
 function readM1Card() {
@@ -563,8 +589,7 @@
             //result页面回显
             document.getElementById("ACardResult").value = result;
         }
-    } catch (e) {
-    }
+    } catch (e) {}
 }
 
 function writeM1Card() {
@@ -581,8 +606,7 @@
             //result页面回显
             document.getElementById("ACardResult").value = result;
         }
-    } catch (e) {
-    }
+    } catch (e) {}
 }
 
 function handleAPDUCMD() {
@@ -599,8 +623,7 @@
             //result页面回显
             document.getElementById("ACardResult").value = result;
         }
-    } catch (e) {
-    }
+    } catch (e) {}
 }
 
 /*
@@ -669,8 +692,7 @@
             //result页面回显
             document.getElementById("ACardResult").value = result;
         }
-    } catch (e) {
-    }
+    } catch (e) {}
 }
 
 export default CertCtl;
\ No newline at end of file

--
Gitblit v1.9.3