From 1261d55c6ec19c31e9ee88f25c2294694608b54c Mon Sep 17 00:00:00 2001 From: huliyong <2783385703@qq.com> Date: Tue, 24 Sep 2024 20:41:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 6 ++++++ api/api.js | 1 + store/store.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- utils/position.js | 13 ++++++++++--- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/App.vue b/App.vue index c6e0043..bcacb59 100644 --- a/App.vue +++ b/App.vue @@ -5,6 +5,12 @@ onShow: function() { this.$store.commit('initConfig') this.$store.commit('getBanner') + this.$store.commit('getPosition') + this.$store.commit('getSpotList') + + setInterval(() => { + // this.$store.commit('getPosition') + }, 5000) }, onHide: function() { } diff --git a/api/api.js b/api/api.js index 5e9798c..9500245 100644 --- a/api/api.js +++ b/api/api.js @@ -94,6 +94,7 @@ const config = { method: 'GET', showLoading: true, }, + // 获取景点列表 // areaId: 0-瓷都镇区 1-湖田片区 2-高岭片区 3-瑶里片区 4-蛟潭片区; categoryTyep: 0-景点 1-美食店铺 2-民宿 3-厕所 querySpotList: { url: '/info/querySpotList', diff --git a/store/store.js b/store/store.js index 4985509..bf78e2e 100644 --- a/store/store.js +++ b/store/store.js @@ -34,6 +34,7 @@ const store = new Vuex.Store({ ], banner : {}, cartCheckboxValue : [],//选中的购物车 + spotList : [], }, getters: { // 角色 true为水洗店 false为酒店 @@ -74,6 +75,51 @@ const store = new Vuex.Store({ }) }, + getSpotList(state){ + api('querySpotList', { + pageNo : 1, + pageSize : 999999999, + }, res => { + if(res.code == 200){ + + let spot = [] + + res.result.records.forEach(n => { + if(n.spotLatitude && n.spotLongitude){ + spot.push(n) + } + }) + + Position.calculateDistance() + + state.spotList = spot + + this.commit('calculateDistance') + } + }) + }, + // 计算地点与自己的距离 + calculateDistance(state){ + if(state.spotList.length && state.position.latitude){ + + state.spotList.forEach(n => { + n.distance = parseFloat(Position.calculateDistance( + state.position.latitude, + state.position.longitude, + n.spotLatitude, + n.spotLongitude, + 3 + )) + }) + + + // 排序,最近的在前面 + state.spotList.sort((a, b) => a.distance - b.distance) + + console.log(state.spotList); + + } + }, login(state){ uni.showLoading({ title: '登录中...' @@ -120,10 +166,11 @@ const store = new Vuex.Store({ }, getPosition(state){ Position.getLocation(res => { - console.log(res); state.position = res + this.commit('calculateDistance') }) }, + }, actions: {}, }) diff --git a/utils/position.js b/utils/position.js index 744b95c..93c87db 100644 --- a/utils/position.js +++ b/utils/position.js @@ -1,7 +1,14 @@ import config from '../config.js' - -function calculateDistance(lat1, lon1, lat2, lon2) { //计算两点距离 +/** + * 计算两点之间的距离 + * @param {number} lat1 地点1精度 + * @param {number} lon1 地点1维度 + * @param {number} lat2 地点2精度 + * @param {number} lon2 地点2维度 + * @param {number} fixed 保留几位小数,默认0 + */ +function calculateDistance(lat1, lon1, lat2, lon2, fixed = 0) { //计算两点距离 let distance = 0 if (!lat2 || !lon2) return distance //先强制转换一下(后端给的字符串) @@ -21,7 +28,7 @@ function calculateDistance(lat1, lon1, lat2, lon2) { //计算两点距离 const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); // 计算距离 distance = R * c; - return distance.toFixed(0) + return distance.toFixed(fixed) } function getLocation(fn) { //获取用户经纬度