Files
qingdao-employment-service/stores/useLocationStore.js

89 lines
2.9 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-12-19 11:35:00 +08:00
const defalutLongLat = {
longitude: 120.382665,
latitude: 36.066938
}
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
2025-12-16 20:24:03 +08:00
function getLocation() { // 获取经纬度两个平台
2025-03-28 15:19:42 +08:00
return new Promise((resole, reject) => {
2025-11-24 18:40:59 +08:00
try {
2025-12-16 20:24:03 +08:00
if (lightAppJssdk.map) {
lightAppJssdk.map.getLocation({
success: function(data) {
longitudeVal.value = Number(data.longitude)
latitudeVal.value = Number(data.latitude)
resole(data)
},
fail: function(data) {
2025-12-19 11:35:00 +08:00
longitudeVal.value = defalutLongLat.longitude
latitudeVal.value = defalutLongLat.latitude
resole(defalutLongLat)
2025-12-16 20:24:03 +08:00
msg('用户位置获取失败')
2025-12-19 11:35:00 +08:00
console.log('失败3', data)
2025-12-16 20:24:03 +08:00
}
})
} else {
uni.getLocation({
type: 'gcj02',
2025-12-19 11:35:00 +08:00
// highAccuracyExpireTime: 3000,
// isHighAccuracy: true,
// timeout: 2000,
2025-12-16 20:24:03 +08:00
success: function(data) {
longitudeVal.value = Number(data.longitude)
latitudeVal.value = Number(data.latitude)
resole(data)
},
fail: function(data) {
2025-12-19 11:35:00 +08:00
longitudeVal.value = defalutLongLat.longitude
latitudeVal.value = defalutLongLat.latitude
resole(defalutLongLat)
2025-12-16 20:24:03 +08:00
msg('用户位置获取失败')
2025-12-19 11:35:00 +08:00
console.log('失败2', data)
2025-12-16 20:24:03 +08:00
}
});
}
2025-11-24 18:40:59 +08:00
} catch (e) {
2025-12-19 11:35:00 +08:00
longitudeVal.value = defalutLongLat.longitude
latitudeVal.value = defalutLongLat.latitude
resole(defalutLongLat)
2025-11-24 18:40:59 +08:00
msg('测试环境,使用模拟定位')
2025-12-19 11:35:00 +08:00
console.log('失败1', e)
2025-11-24 18:40:59 +08:00
}
2025-03-28 15:19:42 +08:00
})
}
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
}
2025-12-19 11:35:00 +08:00
}, {
unistorage: true,
2025-03-28 15:19:42 +08:00
})
export default useLocationStore;