location_point.html 9.0 KB

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