location_point.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>记录</title>
  7. <script src="https://web.tianyunperfect.cn/simple/js/util.js"></script>
  8. <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.2/css/bootstrap.min.css">
  9. <style>
  10. #getLocationBtn {
  11. transform: scale(3);
  12. }
  13. #app {
  14. position: relative;
  15. }
  16. #getLocationBtn {
  17. position: absolute;
  18. top: 20%;
  19. left: 50%;
  20. transform: translateX(-50%);
  21. }
  22. #my_li {
  23. position: absolute;
  24. top: calc(20% + 50px); /* 调整按钮下方的间距,根据需要修改数值 */
  25. }
  26. </style>
  27. <script>
  28. window.addEventListener('load', function () {
  29. var app = document.body;
  30. var startY;
  31. var dist;
  32. var threshold = 100; // 下拉刷新的阈值
  33. app.addEventListener('touchstart', function (e) {
  34. startY = e.touches[0].clientY;
  35. });
  36. app.addEventListener('touchmove', function (e) {
  37. dist = e.touches[0].clientY - startY;
  38. if (dist > threshold) {
  39. // 执行刷新操作
  40. location.reload();
  41. }
  42. });
  43. });
  44. </script>
  45. </head>
  46. <body>
  47. <div id="app" class="container">
  48. <div class="row justify-content-center mt-5">
  49. <button id="getLocationBtn" class="btn btn-primary btn-lg" v-on:click="sendLocationToBackend">记录</button>
  50. </div>
  51. <div class="row justify-content-center mt-5" id="my_li">
  52. <ul>
  53. <li v-for="item in createdList">{{ convertToBeijingTime(item.created_at) }} &nbsp;&nbsp;&nbsp;&nbsp;
  54. <button class="deleteBtn" v-on:click="confirmDelete(item.id)">删除</button>
  55. </li>
  56. </ul>
  57. </div>
  58. </div>
  59. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.5.16/vue.min.js"></script>
  60. <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
  61. <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.min.js"></script>
  62. <script src="https://api.map.baidu.com/api?v=2.0&ak=WMYeXwq7z0VcaiUXTuYTGPZsm8Selmfd"></script>
  63. <script>
  64. function convertToBeijingTime(dateString) {
  65. // 创建 Date 对象,将字符串解析为日期
  66. const date = new Date(dateString);
  67. // 转换为北京时间
  68. const beijingTime = date.toLocaleString('zh-CN', {
  69. timeZone: 'Asia/Shanghai',
  70. year: 'numeric',
  71. month: '2-digit',
  72. day: '2-digit',
  73. hour: '2-digit',
  74. minute: '2-digit',
  75. second: '2-digit'
  76. });
  77. return beijingTime;
  78. }
  79. const app = new Vue({
  80. el: '#app',
  81. data: {
  82. longitude: "0",
  83. latitude: "0",
  84. locationStr: "未知",
  85. createdList: []
  86. },
  87. mounted: function () {
  88. this.getLocation();
  89. this.getLatestData(); // 获取最新的数据
  90. },
  91. methods: {
  92. confirmDelete: function (id) {
  93. if (confirm("确认要删除吗?")) {
  94. // 发送删除请求
  95. const deleteUrl = `https://api.tianyunperfect.cn/location/point_delete?id=${id}`;
  96. fetch(deleteUrl, {method: 'DELETE'}).then(response => {
  97. if (response.ok) {
  98. app.getLatestData();
  99. }
  100. })
  101. }
  102. },
  103. // 获取地址位置的名字
  104. getLocationStr: function (longitude, latitude) {
  105. // 创建地理编码实例, 并配置参数获取乡镇级数据
  106. const myGeo = new BMap.Geocoder({extensions_town: true});
  107. // 根据坐标得到地址描述
  108. myGeo.getLocation(new BMap.Point(longitude, latitude), function (result) {
  109. if (result) {
  110. app.locationStr = result.address;
  111. }
  112. });
  113. },
  114. // 获取经纬度
  115. getLocation: function () {
  116. // 创建百度地理位置实例,代替 navigator.geolocation
  117. let _this = this;
  118. const geolocation = new BMap.Geolocation();
  119. geolocation.getCurrentPosition(function (e) {
  120. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  121. // 百度 geolocation 的经纬度属性不同,此处是 point.lat 而不是 coords.latitude
  122. _this.latitude = e.point.lat;
  123. _this.longitude = e.point.lng;
  124. _this.getLocationStr(_this.longitude, _this.latitude);
  125. }
  126. });
  127. },
  128. getLatestData: async function () {
  129. try {
  130. const response = await axios.get('https://api.tianyunperfect.cn/location/page?page=1&size=10');
  131. this.createdList = response.data.data.list;
  132. } catch (error) {
  133. console.log(error);
  134. }
  135. },
  136. sendLocationToBackend: async function () {
  137. const data = {
  138. location: app.locationStr,
  139. longitude: app.longitude,
  140. latitude: app.latitude,
  141. created_by: "b90c0cf6997d4cfa987f2bc59583fcaa"
  142. };
  143. axios.post('https://api.tianyunperfect.cn/location/point', data)
  144. .then(async function (response) {
  145. console.log(response.data);
  146. let res = response.data;
  147. showMsg(res.message, 2);
  148. await app.getLatestData();
  149. })
  150. .catch(function (error) {
  151. console.log(error);
  152. });
  153. },
  154. }
  155. });
  156. </script>
  157. </body>
  158. </html>