location.html 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>location</title>
  6. <script src="js/axios.min.js"></script>
  7. </head>
  8. <body>
  9. <textarea id="msg" style="width: 100%;" rows="10"></textarea>
  10. <h1 id="tip">2秒内发送</h1>
  11. <script>
  12. Date.prototype.format = function (fmt) {
  13. var o = {
  14. "M+": this.getMonth() + 1, //月份
  15. "d+": this.getDate(), //日
  16. "h+": this.getHours(), //小时
  17. "m+": this.getMinutes(), //分
  18. "s+": this.getSeconds(), //秒
  19. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  20. "S": this.getMilliseconds() //毫秒
  21. };
  22. if (/(y+)/.test(fmt)) {
  23. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  24. }
  25. for (var k in o) {
  26. if (new RegExp("(" + k + ")").test(fmt)) {
  27. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  28. }
  29. }
  30. return fmt;
  31. }
  32. </script>
  33. <script>
  34. let longitude;
  35. let latitude;
  36. let status = false;
  37. navigator.geolocation.getCurrentPosition(function (position) {
  38. longitude = position.coords.longitude;
  39. latitude = position.coords.latitude;
  40. }, function (error) {
  41. alert(error.message);
  42. }, {
  43. timeout: 90000
  44. });
  45. setTimeout(() => {
  46. if (status === false) {
  47. sendMsg();
  48. }
  49. }, 2000)
  50. function setTip(str) {
  51. document.querySelector('#tip').innerHTML = str;
  52. }
  53. document.querySelector("#msg").onfocus = function () {
  54. status = true;
  55. setTip('点击发送');
  56. document.querySelector("#tip").onclick = function () {
  57. sendMsg();
  58. };
  59. };
  60. function sendMsg() {
  61. axios.post("https://api.tianyunperfect.cn/web_history/location_history", {
  62. "location_str": "",
  63. "location_xy": longitude + "-" + latitude,
  64. "msg": document.querySelector("#msg").innerHTML,
  65. "create_time": new Date().format('yyyy-MM-dd hh:mm:ss'),
  66. "update_time": new Date().format('yyyy-MM-dd hh:mm:ss')
  67. }).then(res => {
  68. if (res.data.code === 1) {
  69. // window.close();
  70. if (longitude === undefined) {
  71. setTip("发送成功,但是没有经纬度");
  72. } else {
  73. setTip("发送成功!!!!!!");
  74. }
  75. }
  76. });
  77. }
  78. </script>
  79. </body>
  80. </html>