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