import { defineStore } from 'pinia'; import { ref } from 'vue' import { msg } from '@/common/globalFunction.js' import config from '../config'; const useLocationStore = defineStore("location", () => { // 定义状态 const longitudeVal = ref(null) // 经度 const latitudeVal = ref(null) //纬度 function getLocation() { return new Promise((resole, reject) => { uni.getLocation({ type: 'wgs84', altitude: true, isHighAccuracy: true, enableHighAccuracy: true, // 关键参数:启用传感器辅助 timeout: 10000, success: function(res) { const resd = { longitude: 120.382665, latitude: 36.066938 } if (config.UsingSimulatedPositioning) { // 使用模拟定位 longitudeVal.value = resd.longitude latitudeVal.value = resd.latitude msg('用户位置获取成功') resole(resd) } else { longitudeVal.value = res.longitude latitudeVal.value = res.latitude msg('用户位置获取成功') resole(res) } }, fail: function(err) { // longitudeVal.value = '' // latitudeVal.value = '' // reject(err) const resd = { longitude: 120.382665, latitude: 36.066938 } longitudeVal.value = res.longitude latitudeVal.value = res.latitude msg('用户位置获取失败,使用模拟定位') resole(resd) }, complete: function(e) { console.warn('getUserLocation' + JSON.stringify(e)) } }) }) } function longitude() { return longitudeVal.value } function latitude() { return latitudeVal.value } // 导入 return { getLocation, longitudeVal, latitudeVal } }) export default useLocationStore;