Files
ks-app-employment-service/stores/useLocationStore.js

79 lines
2.4 KiB
JavaScript
Raw Normal View History

2025-03-28 15:19:42 +08:00
import {
defineStore
} from 'pinia';
import {
ref
} from 'vue'
import {
msg
} from '@/common/globalFunction.js'
2025-06-20 10:10:46 +08:00
import config from '../config';
2025-03-28 15:19:42 +08:00
const useLocationStore = defineStore("location", () => {
// 定义状态
2025-05-13 11:10:38 +08:00
const longitudeVal = ref(null) // 经度
const latitudeVal = ref(null) //纬度
2025-03-28 15:19:42 +08:00
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
}
2025-06-20 10:10:46 +08:00
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)
}
2025-03-28 15:19:42 +08:00
},
fail: function(err) {
// longitudeVal.value = ''
// latitudeVal.value = ''
// reject(err)
const resd = {
longitude: 120.382665,
latitude: 36.066938
}
2025-06-20 10:10:46 +08:00
longitudeVal.value = res.longitude
latitudeVal.value = res.latitude
2025-03-28 15:19:42 +08:00
msg('用户位置获取失败,使用模拟定位')
resole(resd)
},
complete: function(e) {
console.warn('getUserLocation' + JSON.stringify(e))
}
})
})
}
function longitude() {
return longitudeVal.value
}
function latitude() {
return latitudeVal.value
}
// 导入
return {
getLocation,
2025-05-13 11:10:38 +08:00
longitudeVal,
latitudeVal
2025-03-28 15:19:42 +08:00
}
})
export default useLocationStore;