From 74f02f1ce263e339d2bf6dd4afb39c71e11a5fba Mon Sep 17 00:00:00 2001 From: xiaozhiyong Date: Thu, 6 Apr 2023 13:13:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/permission.js | 2 +- src/store/piniaIndex.js | 29 ++++++++++------- src/views/HomeView.vue | 70 +++++++++++++++++++++++------------------ 3 files changed, 57 insertions(+), 44 deletions(-) diff --git a/src/permission.js b/src/permission.js index 0ec0146..fd3b908 100644 --- a/src/permission.js +++ b/src/permission.js @@ -12,7 +12,7 @@ router.beforeEach(async (to, from, next) => { let store = useStore() //获取状态 let token = getToken() //获取cookie // removeToken() - store.getPosition() + // store.getPosition() if (!token) { let paramsTarget = obtainUrlPathParameterTarget(location.href) diff --git a/src/store/piniaIndex.js b/src/store/piniaIndex.js index 9d476c4..da1de4e 100644 --- a/src/store/piniaIndex.js +++ b/src/store/piniaIndex.js @@ -27,22 +27,27 @@ const instance = defineStore('user', { actions: { getPosition() { - // initAMap().then(() => { - // console.log(3) - // let AMap = Vue.prototype.$AMap - // let geolocation = new AMap.Geolocation({ - // // enableHighAccuracy: true // 是否使用高精度定位,默认:true - // }) - // geolocation.getCurrentPosition(function (status, result) { - // console.log('status', status) - // console.log('result', result) - // }) - // }) - // return + initAMap().then(() => { + // console.log(3) + let AMap = Vue.prototype.$AMap + let geolocation = new AMap.Geolocation({ + // enableHighAccuracy: true // 是否使用高精度定位,默认:true + }) + geolocation.getCurrentPosition(function (status, result) { + console.log(2) + if (status === 'complete') { + let { lat: latitude, lng: longitude } = result.position + this.position = { latitude, longitude } + console.log('this.position', this.position) + } + }) + }) + return if (navigator?.geolocation?.getCurrentPosition) { navigator.geolocation.getCurrentPosition( position => { + console.log('position.coords', position.coords) let { latitude, longitude } = position.coords this.position = { latitude, longitude } }, diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index 95bf3ce..0be748e 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -19,6 +19,9 @@ import homeNavBar from '@/components/homeNavBar.vue' import listItem from '@/components/listItem.vue' import oilSiteApi from '@/api/oil-site.js' + +import initAMap from '@/utils/amap.js' + export default { components: { homeNavBar, @@ -122,39 +125,44 @@ export default { return !position?.latitude && !position?.longitude ? true : false }, getlist() { - let position = this.$pinia.state.value.user.position - // console.log('position', position) - // console.log(Object.assign(this.page.params, position), position, '地理位置数据') - if (this.positionChenk(position)) { - position = JSON.parse(localStorage.getItem('position')) - if (this.positionChenk(position)) { - // console.log(position, this.$pinia.state.value.user.position, '地理位置错误') - this.finished = true - return - } - } - this.page.params = Object.assign(this.page.params, position) - this.loading = true - oilSiteApi - .getSiteList(this.page) - .then(res => { - if (res.code == 20000) { - if (res.data.list.length < 10) { - this.finished = true - } - if (this.page.currentPage == 1) { - this.list = res.data.list - } else { - this.list = this.list.concat(res.data.list) - } - this.loading = false - this.refreshing = false - } - // console.log(res) + initAMap().then(() => { + let AMap = this.$AMap + let geolocation = new AMap.Geolocation({ + // enableHighAccuracy: true // 是否使用高精度定位,默认:true }) - .catch(error => { - this.finished = true + geolocation.getCurrentPosition((status, result) => { + if (status === 'complete') { + let { lat: latitude, lng: longitude } = result.position + + // this.position = { latitude, longitude } + // console.log('this.position', this.position) + + let position = { latitude, longitude } + + this.page.params = Object.assign(this.page.params, position) + this.loading = true + oilSiteApi + .getSiteList(this.page) + .then(res => { + if (res.code == 20000) { + if (res.data.list.length < 10) { + this.finished = true + } + if (this.page.currentPage == 1) { + this.list = res.data.list + } else { + this.list = this.list.concat(res.data.list) + } + this.loading = false + this.refreshing = false + } + }) + .catch(error => { + this.finished = true + }) + } }) + }) } } }