location_point.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, index) in createdList">{{ convertToBeijingTime(item.created_at) }} &nbsp;&nbsp;&nbsp;&nbsp;
  54. <button class="deleteBtn" v-on:click="confirmDelete(item.id)">删除</button>&nbsp;
  55. <button class="eventBtn" v-on:click="addEvent(index)">事件</button>
  56. </li>
  57. <a href="https://web.tianyunperfect.cn/simple/location_event.html">转到事件</a>
  58. </ul>
  59. </div>
  60. </div>
  61. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.5.16/vue.min.js"></script>
  62. <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
  63. <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.min.js"></script>
  64. <script src="https://api.map.baidu.com/api?v=2.0&ak=WMYeXwq7z0VcaiUXTuYTGPZsm8Selmfd"></script>
  65. <script>
  66. function convertToBeijingTime(dateString) {
  67. // 创建 Date 对象,将字符串解析为日期
  68. const date = new Date(dateString);
  69. // 转换为北京时间
  70. const beijingTime = date.toLocaleString('zh-CN', {
  71. timeZone: 'Asia/Shanghai',
  72. year: 'numeric',
  73. month: '2-digit',
  74. day: '2-digit',
  75. hour: '2-digit',
  76. minute: '2-digit',
  77. second: '2-digit'
  78. });
  79. return beijingTime;
  80. }
  81. const app = new Vue({
  82. el: '#app',
  83. data: {
  84. longitude: "0",
  85. latitude: "0",
  86. locationStr: "未知",
  87. createdList: []
  88. },
  89. mounted: function () {
  90. this.getLocation();
  91. this.getLatestData(); // 获取最新的数据
  92. },
  93. methods: {
  94. addEvent(index) {
  95. const currentItem = this.createdList[index];
  96. const nextItem = this.createdList[index + 1];
  97. const endTime = currentItem.created_at;
  98. const startTime = nextItem ? nextItem.created_at : currentItem.created_at;
  99. const eventName = prompt("请输入事件名称:");
  100. if (eventName) {
  101. const eventData = {
  102. start_time: convertToBeijingTime(startTime),
  103. end_time: convertToBeijingTime(endTime),
  104. event_name: eventName,
  105. created_by: "b90c0cf6997d4cfa987f2bc59583fcaa"
  106. };
  107. // 使用异步请求将 eventData 发送到后台
  108. axios.post("https://api.tianyunperfect.cn/location/event", eventData)
  109. .then(response => {
  110. // 处理请求成功的响应
  111. showMsg("成功", 2);
  112. })
  113. .catch(error => {
  114. // 处理请求失败的错误
  115. console.error(error);
  116. });
  117. }
  118. },
  119. confirmDelete: function (id) {
  120. if (confirm("确认要删除吗?")) {
  121. // 发送删除请求
  122. const deleteUrl = `https://api.tianyunperfect.cn/location/point_delete?id=${id}`;
  123. fetch(deleteUrl, {method: 'DELETE'}).then(response => {
  124. if (response.ok) {
  125. app.getLatestData();
  126. }
  127. })
  128. }
  129. },
  130. // 获取地址位置的名字
  131. getLocationStr: function (longitude, latitude) {
  132. // 创建地理编码实例, 并配置参数获取乡镇级数据
  133. const myGeo = new BMap.Geocoder({extensions_town: true});
  134. // 根据坐标得到地址描述
  135. myGeo.getLocation(new BMap.Point(longitude, latitude), function (result) {
  136. if (result) {
  137. app.locationStr = result.address;
  138. }
  139. });
  140. },
  141. // 获取经纬度
  142. getLocation: function () {
  143. // 创建百度地理位置实例,代替 navigator.geolocation
  144. let _this = this;
  145. const geolocation = new BMap.Geolocation();
  146. geolocation.getCurrentPosition(function (e) {
  147. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  148. // 百度 geolocation 的经纬度属性不同,此处是 point.lat 而不是 coords.latitude
  149. _this.latitude = e.point.lat;
  150. _this.longitude = e.point.lng;
  151. _this.getLocationStr(_this.longitude, _this.latitude);
  152. }
  153. });
  154. },
  155. getLatestData: async function () {
  156. try {
  157. const response = await axios.get('https://api.tianyunperfect.cn/location/page?page=1&size=20');
  158. this.createdList = response.data.data.list;
  159. } catch (error) {
  160. console.log(error);
  161. }
  162. },
  163. sendLocationToBackend: async function () {
  164. const data = {
  165. location: app.locationStr,
  166. longitude: app.longitude,
  167. latitude: app.latitude,
  168. created_by: "b90c0cf6997d4cfa987f2bc59583fcaa"
  169. };
  170. axios.post('https://api.tianyunperfect.cn/location/point', data)
  171. .then(async function (response) {
  172. console.log(response.data);
  173. let res = response.data;
  174. showMsg(res.message, 2);
  175. await app.getLatestData();
  176. })
  177. .catch(function (error) {
  178. console.log(error);
  179. });
  180. },
  181. }
  182. });
  183. </script>
  184. </body>
  185. </html>