location_point.html 8.5 KB

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