tianyunperfect 2 years ago
parent
commit
40dcdce234
1 changed files with 41 additions and 10 deletions
  1. 41 10
      simple-demo/location.html

+ 41 - 10
simple-demo/location.html

@@ -6,7 +6,8 @@
     <script src="js/axios.min.js"></script>
 </head>
 <body>
-<h1 id="msg"></h1>
+<textarea id="msg" style="width: 100%;" rows="3"></textarea>
+<h1 id="tip">2秒内发送</h1>
 <script>
     Date.prototype.format = function (fmt) {
         var o = {
@@ -28,10 +29,39 @@
         }
         return fmt;
     }
+
+    let longitude;
+    let latitude;
+    let status = false;
+
     navigator.geolocation.getCurrentPosition(function (position) {
-        let longitude = position.coords.longitude;
-        let latitude = position.coords.latitude;
+        longitude = position.coords.longitude;
+        latitude = position.coords.latitude;
+    }, function (error) {
+        alert(error.message);
+    }, {
+        timeout: 90000
+    });
+
+    setTimeout(() => {
+        if (status === false) {
+            sendMsg();
+        }
+    }, 2000)
+
+    function setTip(str) {
+        document.querySelector('#tip').innerHTML = str;
+    }
+
+    document.querySelector("#msg").onfocus = function () {
+        status = true;
+        setTip('点击发送');
+        document.querySelector("#tip").onclick = function () {
+            sendMsg();
+        };
+    };
 
+    function sendMsg() {
         axios.post("https://api.tianyunperfect.cn/web_history/location_history", {
             "location_str": "",
             "location_xy": longitude + "-" + latitude,
@@ -40,15 +70,16 @@
             "update_time": new Date().format('yyyy-MM-dd hh:mm:ss')
         }).then(res => {
             if (res.data.code === 1) {
-                window.close();
-                document.querySelector("#msg").innerHTML = "OK";
+                // window.close();
+                if (longitude === undefined) {
+                    setTip("发送成功,但是没有经纬度");
+                } else {
+                    setTip("发送成功");
+                }
             }
         });
-    }, function (error) {
-        alert(error.message);
-    }, {
-        timeout: 90000
-    });
+    }
+
 </script>
 </body>
 </html>