location.html 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. let longitude;
  33. let latitude;
  34. let status = false;
  35. navigator.geolocation.getCurrentPosition(function (position) {
  36. longitude = position.coords.longitude;
  37. latitude = position.coords.latitude;
  38. }, function (error) {
  39. alert(error.message);
  40. }, {
  41. timeout: 90000
  42. });
  43. setTimeout(() => {
  44. if (status === false) {
  45. sendMsg();
  46. }
  47. }, 2000)
  48. function setTip(str) {
  49. document.querySelector('#tip').innerHTML = str;
  50. }
  51. document.querySelector("#msg").onfocus = function () {
  52. status = true;
  53. setTip('点击发送');
  54. document.querySelector("#tip").onclick = function () {
  55. sendMsg();
  56. };
  57. };
  58. function sendMsg() {
  59. axios.post("https://api.tianyunperfect.cn/web_history/location_history", {
  60. "location_str": "",
  61. "location_xy": longitude + "-" + latitude,
  62. "msg": document.querySelector("#msg").innerHTML,
  63. "create_time": new Date().format('yyyy-MM-dd hh:mm:ss'),
  64. "update_time": new Date().format('yyyy-MM-dd hh:mm:ss')
  65. }).then(res => {
  66. if (res.data.code === 1) {
  67. // window.close();
  68. if (longitude === undefined) {
  69. setTip("发送成功,但是没有经纬度");
  70. } else {
  71. setTip("发送成功");
  72. }
  73. }
  74. });
  75. }
  76. </script>
  77. </body>
  78. </html>