Browse Source

获取本机ip

刘洋 10 months ago
parent
commit
d8b50c6426
1 changed files with 16 additions and 23 deletions
  1. 16 23
      src/modules/examwork/view/ipConfig.vue

+ 16 - 23
src/modules/examwork/view/ipConfig.vue

@@ -385,29 +385,22 @@ export default {
         });
     },
     getIpAddress() {
-      let year = new Date().getFullYear();
-      fetch(`https://${year}.ip138.com/`)
-        .then((x) => x.text())
-        .then((h) => {
-          let domParser = new DOMParser();
-          let doc = domParser.parseFromString(h, "text/html");
-          let text = doc?.querySelector("p")?.innerText?.trim();
-          // document.getElementById('content1').innerText = text;
-          // let ipAddress = {ip: "", location: "", type: ""}
-          // try {
-          //     let ip = text.substring(text.indexOf("[")+1,text.indexOf("]")).trim();
-          //     ipAddress.ip = ip;
-          //     let other = text.substring(text.indexOf("来自:")+3).trim();
-          //     let arr = other.split(" ");
-          //     ipAddress.location = arr[0];
-          //     ipAddress.type = arr[1];
-          // }catch (e) {
-          //     console.error(e)
-          // }
-          let ip = text
-            .substring(text.indexOf("[") + 1, text.indexOf("]"))
-            .trim();
-          this.editForm.ip = ip;
+      // let year = new Date().getFullYear();
+      // fetch(`https://${year}.ip138.com/`)
+      //   .then((x) => x.text())
+      //   .then((h) => {
+      //     let domParser = new DOMParser();
+      //     let doc = domParser.parseFromString(h, "text/html");
+      //     let text = doc?.querySelector("p")?.innerText?.trim();
+      //     let ip = text
+      //       .substring(text.indexOf("[") + 1, text.indexOf("]"))
+      //       .trim();
+      //     this.editForm.ip = ip;
+      //   });
+      fetch("https://qifu-api.baidubce.com/ip/local/geo/v1/district")
+        .then((res) => res.json())
+        .then((res) => {
+          this.editForm.ip = res?.ip || "";
         });
     },
     editSubmit() {