location_point.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. this.page = newPage;
  120. this.getLatestData();
  121. },
  122. changeSize() {
  123. this.page = 1; // 重置页码
  124. this.getLatestData();
  125. },
  126. async getLatestData() {
  127. const response = await axios.get('https://api.tianyunperfect.cn/location/page', {
  128. params: {
  129. page: this.page,
  130. size: this.size
  131. }
  132. });
  133. this.createdList = response.data.data.list;
  134. this.totalPages = Math.ceil(response.data.data.total / app.size);
  135. },
  136. addEvent(index) {
  137. const currentItem = this.createdList[index];
  138. const nextItem = this.createdList[index + 1];
  139. const endTime = currentItem.created_at;
  140. const startTime = nextItem ? nextItem.created_at : currentItem.created_at;
  141. const eventName = prompt("请输入事件名称:");
  142. if (eventName) {
  143. const eventData = {
  144. start_time: convertToBeijingTime(startTime),
  145. end_time: convertToBeijingTime(endTime),
  146. event_name: eventName,
  147. created_by: "b90c0cf6997d4cfa987f2bc59583fcaa"
  148. };
  149. // 使用异步请求将 eventData 发送到后台
  150. axios.post("https://api.tianyunperfect.cn/location/event", eventData)
  151. .then(response => {
  152. // 处理请求成功的响应
  153. showMsg("成功", 2);
  154. })
  155. .catch(error => {
  156. // 处理请求失败的错误
  157. console.error(error);
  158. });
  159. }
  160. },
  161. confirmDelete: function (id) {
  162. if (confirm("确认要删除吗?")) {
  163. // 发送删除请求
  164. const deleteUrl = `https://api.tianyunperfect.cn/location/point_delete?id=${id}`;
  165. fetch(deleteUrl, {method: 'DELETE'}).then(response => {
  166. if (response.ok) {
  167. app.getLatestData();
  168. }
  169. })
  170. }
  171. },
  172. // 获取地址位置的名字
  173. getLocationStr: function (longitude, latitude) {
  174. // 创建地理编码实例, 并配置参数获取乡镇级数据
  175. const myGeo = new BMap.Geocoder({extensions_town: true});
  176. // 根据坐标得到地址描述
  177. myGeo.getLocation(new BMap.Point(longitude, latitude), function (result) {
  178. if (result) {
  179. app.locationStr = result.address;
  180. }
  181. });
  182. },
  183. // 获取经纬度
  184. getLocation: function () {
  185. // 创建百度地理位置实例,代替 navigator.geolocation
  186. let _this = this;
  187. const geolocation = new BMap.Geolocation();
  188. geolocation.getCurrentPosition(function (e) {
  189. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  190. // 百度 geolocation 的经纬度属性不同,此处是 point.lat 而不是 coords.latitude
  191. _this.latitude = e.point.lat;
  192. _this.longitude = e.point.lng;
  193. _this.getLocationStr(_this.longitude, _this.latitude);
  194. }
  195. });
  196. },
  197. sendLocationToBackend: async function () {
  198. const data = {
  199. location: app.locationStr,
  200. longitude: app.longitude,
  201. latitude: app.latitude,
  202. created_by: "b90c0cf6997d4cfa987f2bc59583fcaa"
  203. };
  204. axios.post('https://api.tianyunperfect.cn/location/point', data)
  205. .then(async function (response) {
  206. console.log(response.data);
  207. let res = response.data;
  208. showMsg(res.message, 2);
  209. await app.getLatestData();
  210. })
  211. .catch(function (error) {
  212. console.log(error);
  213. });
  214. },
  215. }
  216. });
  217. </script>
  218. </body>
  219. </html>