location_point.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #app {
  11. position: relative;
  12. }
  13. #getLocationBtn {
  14. position: absolute;
  15. top: 20%;
  16. left: 50%;
  17. transform: scale(3) translateX(-50%);
  18. }
  19. #my_li {
  20. position: absolute;
  21. top: calc(20% + 50px); /* 调整按钮下方的间距,根据需要修改数值 */
  22. }
  23. </style>
  24. <script>
  25. window.addEventListener('load', function () {
  26. var app = document.body;
  27. var startY;
  28. var dist;
  29. var threshold = 100; // 下拉刷新的阈值
  30. app.addEventListener('touchstart', function (e) {
  31. startY = e.touches[0].clientY;
  32. });
  33. app.addEventListener('touchmove', function (e) {
  34. dist = e.touches[0].clientY - startY;
  35. if (dist > threshold) {
  36. // 执行刷新操作
  37. location.reload();
  38. }
  39. });
  40. });
  41. </script>
  42. </head>
  43. <body>
  44. <div id="app" class="container">
  45. <div class="row justify-content-center mt-5">
  46. <button id="getLocationBtn" class="btn btn-primary btn-lg" v-on:click="sendLocationToBackend">记录</button>
  47. </div>
  48. <div class="row justify-content-center mt-5" id="my_li">
  49. <ul>
  50. <li v-for="(item, index) in createdList">{{ convertToBeijingTime(item.created_at) }} &nbsp;&nbsp;&nbsp;&nbsp;
  51. <button class="deleteBtn" v-on:click="confirmDelete(item.id)">删除</button>&nbsp;
  52. <button class="eventBtn" v-on:click="addEvent(index)">事件</button>
  53. </li>
  54. <div class="pagination">
  55. <a href="#" @click.prevent="changePage(page-1)" :disabled="page === 1">上一页</a>
  56. <a
  57. v-for="p in visiblePages"
  58. @click="changePage(p)"
  59. :class="{ current: p === page }"
  60. >
  61. {{ p }}
  62. </a>
  63. <a href="#" @click.prevent="changePage(page+1)" :disabled="page === totalPages">下一页</a>
  64. <select v-model="size" v-on:change="changeSize">
  65. <option value="10">10条/页</option>
  66. <option value="20">20条/页</option>
  67. <option value="50">50条/页</option>
  68. </select>
  69. </div>
  70. <a href="https://web.tianyunperfect.cn/simple/location_event.html">转到事件</a>
  71. </ul>
  72. </div>
  73. </div>
  74. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.5.16/vue.min.js"></script>
  75. <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
  76. <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.min.js"></script>
  77. <script src="https://api.map.baidu.com/api?v=2.0&ak=WMYeXwq7z0VcaiUXTuYTGPZsm8Selmfd"></script>
  78. <script>
  79. function convertToBeijingTime(dateString) {
  80. // 创建 Date 对象,将字符串解析为日期
  81. const date = new Date(dateString);
  82. // 转换为北京时间
  83. const beijingTime = date.toLocaleString('zh-CN', {
  84. timeZone: 'Asia/Shanghai',
  85. year: 'numeric',
  86. month: '2-digit',
  87. day: '2-digit',
  88. hour: '2-digit',
  89. minute: '2-digit',
  90. second: '2-digit'
  91. });
  92. return beijingTime;
  93. }
  94. const app = new Vue({
  95. el: '#app',
  96. data: {
  97. page: 1,
  98. size: 10,
  99. totalPages: 1,
  100. longitude: "0",
  101. latitude: "0",
  102. locationStr: "未知",
  103. createdList: []
  104. },
  105. mounted: function () {
  106. this.getLocation();
  107. this.getLatestData(); // 获取最新的数据
  108. },
  109. computed: {
  110. visiblePages() {
  111. const left = Math.max(1, this.page - 2);
  112. const right = Math.min(this.totalPages, this.page + 2);
  113. const pages = [];
  114. for (let i = left; i <= right; i++) {
  115. pages.push(i);
  116. }
  117. return pages;
  118. }
  119. },
  120. methods: {
  121. changePage(newPage) {
  122. this.page = newPage;
  123. this.getLatestData();
  124. },
  125. changeSize() {
  126. this.page = 1; // 重置页码
  127. this.getLatestData();
  128. },
  129. async getLatestData() {
  130. const response = await axios.get('https://api.tianyunperfect.cn/location/page', {
  131. params: {
  132. page: this.page,
  133. size: this.size
  134. }
  135. });
  136. this.createdList = response.data.data.list;
  137. this.totalPages = Math.ceil(response.data.data.total / app.size);
  138. },
  139. addEvent(index) {
  140. const currentItem = this.createdList[index];
  141. const nextItem = this.createdList[index + 1];
  142. const endTime = currentItem.created_at;
  143. const startTime = nextItem ? nextItem.created_at : currentItem.created_at;
  144. const eventName = prompt("请输入事件名称:");
  145. if (eventName) {
  146. const eventData = {
  147. start_time: convertToBeijingTime(startTime),
  148. end_time: convertToBeijingTime(endTime),
  149. event_name: eventName,
  150. created_by: "b90c0cf6997d4cfa987f2bc59583fcaa"
  151. };
  152. // 使用异步请求将 eventData 发送到后台
  153. axios.post("https://api.tianyunperfect.cn/location/event", eventData)
  154. .then(response => {
  155. // 处理请求成功的响应
  156. showMsg("成功", 2);
  157. })
  158. .catch(error => {
  159. // 处理请求失败的错误
  160. console.error(error);
  161. });
  162. }
  163. },
  164. confirmDelete: function (id) {
  165. if (confirm("确认要删除吗?")) {
  166. // 发送删除请求
  167. const deleteUrl = `https://api.tianyunperfect.cn/location/point_delete?id=${id}`;
  168. fetch(deleteUrl, {method: 'DELETE'}).then(response => {
  169. if (response.ok) {
  170. app.getLatestData();
  171. }
  172. })
  173. }
  174. },
  175. // 获取地址位置的名字
  176. getLocationStr: function (longitude, latitude) {
  177. // 创建地理编码实例, 并配置参数获取乡镇级数据
  178. const myGeo = new BMap.Geocoder({extensions_town: true});
  179. // 根据坐标得到地址描述
  180. myGeo.getLocation(new BMap.Point(longitude, latitude), function (result) {
  181. if (result) {
  182. app.locationStr = result.address;
  183. }
  184. });
  185. },
  186. // 获取经纬度
  187. getLocation: function () {
  188. // 创建百度地理位置实例,代替 navigator.geolocation
  189. let _this = this;
  190. const geolocation = new BMap.Geolocation();
  191. geolocation.getCurrentPosition(function (e) {
  192. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  193. // 百度 geolocation 的经纬度属性不同,此处是 point.lat 而不是 coords.latitude
  194. _this.latitude = e.point.lat;
  195. _this.longitude = e.point.lng;
  196. _this.getLocationStr(_this.longitude, _this.latitude);
  197. }
  198. });
  199. },
  200. sendLocationToBackend: async function () {
  201. const data = {
  202. location: app.locationStr,
  203. longitude: app.longitude,
  204. latitude: app.latitude,
  205. created_by: "b90c0cf6997d4cfa987f2bc59583fcaa"
  206. };
  207. axios.post('https://api.tianyunperfect.cn/location/point', data)
  208. .then(async function (response) {
  209. console.log(response.data);
  210. let res = response.data;
  211. showMsg(res.message, 2);
  212. await app.getLatestData();
  213. })
  214. .catch(function (error) {
  215. console.log(error);
  216. });
  217. },
  218. }
  219. });
  220. </script>
  221. </body>
  222. </html>