location_point.html 9.0 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. #getLocationBtn {
  11. transform: scale(3);
  12. width: 100vw;
  13. }
  14. .pagination a.current {
  15. font-weight: bold;
  16. color: #534d4d;
  17. }
  18. </style>
  19. <script>
  20. window.addEventListener('load', function () {
  21. var app = document.body;
  22. var startY;
  23. var dist;
  24. var threshold = 100; // 下拉刷新的阈值
  25. app.addEventListener('touchstart', function (e) {
  26. startY = e.touches[0].clientY;
  27. });
  28. app.addEventListener('touchmove', function (e) {
  29. dist = e.touches[0].clientY - startY;
  30. if (dist > threshold) {
  31. // 执行刷新操作
  32. location.reload();
  33. }
  34. });
  35. });
  36. </script>
  37. </head>
  38. <body>
  39. <div id="app" class="container">
  40. <div class="row justify-content-center mt-5" id="my_li">
  41. <ul>
  42. <li v-for="(item, index) in createdList">{{ convertToBeijingTime(item.created_at) }} &nbsp;&nbsp;&nbsp;&nbsp;
  43. <button class="deleteBtn" v-on:click="confirmDelete(item.id)">删除</button>&nbsp;
  44. <button class="eventBtn" v-on:click="addEvent(index)">事件</button>
  45. </li>
  46. <br/>
  47. <div class="pagination">
  48. <a href="#" @click.prevent="changePage(page-1)" :disabled="page === 1">上一页</a>&nbsp;
  49. <a
  50. v-for="p in visiblePages"
  51. @click="changePage(p)"
  52. :class="{ current: p === page }"
  53. >
  54. &nbsp;{{ p }}&nbsp;
  55. </a>&nbsp;
  56. <a href="#" @click.prevent="changePage(page+1)" :disabled="page === totalPages">下一页</a>&nbsp;&nbsp;
  57. <select v-model="size" v-on:change="changeSize">
  58. <option value="10">10条/页</option>
  59. <option value="20">20条/页</option>
  60. <option value="50">50条/页</option>
  61. </select>
  62. </div>
  63. <br/>
  64. <a href="https://web.tianyunperfect.cn/simple/location_event.html">转到事件页面</a>
  65. </ul>
  66. </div>
  67. <div class="row justify-content-center mt-5">
  68. <button id="getLocationBtn" class="btn btn-primary btn-lg" v-on:click="sendLocationToBackend">记录</button>
  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. computed: {
  107. visiblePages() {
  108. const left = Math.max(1, this.page - 2);
  109. const right = Math.min(this.totalPages, this.page + 2);
  110. const pages = [];
  111. for (let i = left; i <= right; i++) {
  112. pages.push(i);
  113. }
  114. return pages;
  115. }
  116. },
  117. methods: {
  118. changePage(newPage) {
  119. if (newPage > this.totalPages || newPage < 1) {
  120. alert("page 不存在");
  121. return;
  122. }
  123. this.page = newPage;
  124. this.getLatestData();
  125. },
  126. changeSize() {
  127. this.page = 1; // 重置页码
  128. this.getLatestData();
  129. },
  130. async getLatestData() {
  131. const response = await axios.get('https://api.tianyunperfect.cn/location/page', {
  132. params: {
  133. page: this.page,
  134. size: this.size
  135. }
  136. });
  137. this.createdList = response.data.data.list;
  138. this.totalPages = Math.ceil(response.data.data.total / app.size);
  139. },
  140. addEvent(index) {
  141. const currentItem = this.createdList[index];
  142. const nextItem = this.createdList[index + 1];
  143. const endTime = currentItem.created_at;
  144. const startTime = nextItem ? nextItem.created_at : currentItem.created_at;
  145. const eventName = prompt("请输入事件名称:");
  146. if (eventName) {
  147. const eventData = {
  148. start_time: convertToBeijingTime(startTime),
  149. end_time: convertToBeijingTime(endTime),
  150. event_name: eventName,
  151. created_by: "b90c0cf6997d4cfa987f2bc59583fcaa"
  152. };
  153. // 使用异步请求将 eventData 发送到后台
  154. axios.post("https://api.tianyunperfect.cn/location/event", eventData)
  155. .then(response => {
  156. // 处理请求成功的响应
  157. showMsg("成功", 2);
  158. })
  159. .catch(error => {
  160. // 处理请求失败的错误
  161. console.error(error);
  162. });
  163. }
  164. },
  165. confirmDelete: function (id) {
  166. if (confirm("确认要删除吗?")) {
  167. // 发送删除请求
  168. const deleteUrl = `https://api.tianyunperfect.cn/location/point_delete?id=${id}`;
  169. fetch(deleteUrl, {method: 'DELETE'}).then(response => {
  170. if (response.ok) {
  171. app.getLatestData();
  172. }
  173. })
  174. }
  175. },
  176. // 获取地址位置的名字
  177. getLocationStr: function (longitude, latitude) {
  178. // 创建地理编码实例, 并配置参数获取乡镇级数据
  179. const myGeo = new BMap.Geocoder({extensions_town: true});
  180. // 根据坐标得到地址描述
  181. myGeo.getLocation(new BMap.Point(longitude, latitude), function (result) {
  182. if (result) {
  183. app.locationStr = result.address;
  184. }
  185. });
  186. },
  187. // 获取经纬度
  188. getLocation: function () {
  189. // 创建百度地理位置实例,代替 navigator.geolocation
  190. let _this = this;
  191. const geolocation = new BMap.Geolocation();
  192. geolocation.getCurrentPosition(function (e) {
  193. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  194. // 百度 geolocation 的经纬度属性不同,此处是 point.lat 而不是 coords.latitude
  195. _this.latitude = e.point.lat;
  196. _this.longitude = e.point.lng;
  197. _this.getLocationStr(_this.longitude, _this.latitude);
  198. }
  199. });
  200. },
  201. sendLocationToBackend: async function () {
  202. const data = {
  203. location: app.locationStr,
  204. longitude: app.longitude,
  205. latitude: app.latitude,
  206. created_by: "b90c0cf6997d4cfa987f2bc59583fcaa"
  207. };
  208. axios.post('https://api.tianyunperfect.cn/location/point', data)
  209. .then(async function (response) {
  210. console.log(response.data);
  211. let res = response.data;
  212. showMsg(res.message, 2);
  213. await app.getLatestData();
  214. })
  215. .catch(function (error) {
  216. console.log(error);
  217. });
  218. },
  219. }
  220. });
  221. </script>
  222. </body>
  223. </html>