location.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>location</title>
  7. <script src="js/axios.min.js"></script>
  8. <script src="https://web.tianyunperfect.cn/util.js"></script>
  9. <script src="https://api.map.baidu.com/api?v=2.0&ak=WMYeXwq7z0VcaiUXTuYTGPZsm8Selmfd"></script>
  10. </head>
  11. <body>
  12. <textarea id="msg" style="width: 100%; font-size: 25px" rows="10"></textarea>
  13. <h1 id="tip">2秒内发送</h1>
  14. <script>
  15. Date.prototype.format = function (fmt) {
  16. var o = {
  17. "M+": this.getMonth() + 1, //月份
  18. "d+": this.getDate(), //日
  19. "h+": this.getHours(), //小时
  20. "m+": this.getMinutes(), //分
  21. "s+": this.getSeconds(), //秒
  22. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  23. "S": this.getMilliseconds() //毫秒
  24. };
  25. if (/(y+)/.test(fmt)) {
  26. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  27. }
  28. for (var k in o) {
  29. if (new RegExp("(" + k + ")").test(fmt)) {
  30. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  31. }
  32. }
  33. return fmt;
  34. }
  35. </script>
  36. <script>
  37. let longitude;
  38. let latitude;
  39. let locationStr = '';
  40. let status = false;
  41. // 获取地址位置的名字
  42. function getLocationStr() {
  43. var map = new BMap.Map("l-map");
  44. // 创建地理编码实例, 并配置参数获取乡镇级数据
  45. var myGeo = new BMap.Geocoder({extensions_town: true});
  46. // 根据坐标得到地址描述
  47. myGeo.getLocation(new BMap.Point(longitude, latitude), function (result) {
  48. if (result) {
  49. locationStr = result.address;
  50. } else {
  51. return "未知";
  52. }
  53. });
  54. }
  55. // 获取经纬度
  56. function getLocation() {
  57. // 创建百度地理位置实例,代替 navigator.geolocation
  58. var geolocation = new BMap.Geolocation();
  59. geolocation.getCurrentPosition(function (e) {
  60. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  61. // 百度 geolocation 的经纬度属性不同,此处是 point.lat 而不是 coords.latitude
  62. latitude = e.point.lat;
  63. longitude = e.point.lng;
  64. getLocationStr();
  65. } else {
  66. }
  67. });
  68. }
  69. getLocation();
  70. setTimeout(() => {
  71. if (status === false) {
  72. sendMsg();
  73. }
  74. }, 2000)
  75. function setTip(str) {
  76. document.querySelector('#tip').innerHTML = str;
  77. }
  78. document.querySelector("#msg").onfocus = function () {
  79. status = true;
  80. setTip('点击发送');
  81. document.querySelector("#tip").onclick = function () {
  82. sendMsg();
  83. };
  84. };
  85. function sendMsg() {
  86. axios.post("https://api.tianyunperfect.cn/web_history/location_history", {
  87. "location_str": locationStr,
  88. "location_xy": latitude + "," + longitude,
  89. "msg": document.querySelector("#msg").value,
  90. "create_time": new Date().format('yyyy-MM-dd hh:mm:ss'),
  91. "update_time": new Date().format('yyyy-MM-dd hh:mm:ss'),
  92. "user_id": getQueryString("user_id")
  93. }).then(res => {
  94. if (res.data.code === 1) {
  95. // window.close();
  96. if (longitude === undefined) {
  97. setTip("发送成功,但是没有经纬度");
  98. } else {
  99. setTip("发送成功!!!!!!");
  100. }
  101. }
  102. });
  103. }
  104. </script>
  105. </body>
  106. </html>