import { ref } from 'vue'
|
import http from '@/plugins/http.js'
|
import { useUserStore } from '../store/user'
|
|
const userStore = useUserStore()
|
export function useLocation() {
|
const latitude = ref<number | null>(null)
|
const longitude = ref<number | null>(null)
|
const error = ref<string | null>(null)
|
const address = ref<string | null>(null)
|
const province= ref<string | null>(null)
|
|
const getLocation = () => {
|
uni.getLocation({
|
type: 'wgs84',
|
geocode: true,
|
success: async (res:any) => {
|
latitude.value = res.latitude
|
longitude.value = res.longitude
|
const {
|
code,data
|
} = await http.request('get', '/api/pub/customer/home/address/parse', {
|
data: {},
|
params: {
|
// https://apis.map.qq.com/ws/geocoder/v1/?location=39.984154,116.307490&key=[你的key]&get_poi=1
|
location:`${res.latitude},${res.longitude}`
|
}
|
})
|
|
if (data && data.result && data.result.ad_info) {
|
address.value = data.result.address
|
province.value = data.result.ad_info.province
|
console.log("省的值",province.value )
|
// 更新地址信息到缓存中
|
userStore.updateAddress(data.result)
|
}
|
},
|
fail: (err:any) => {
|
error.value = '获取位置失败:' + (err.errMsg || '未知错误')
|
},
|
complete: function () {
|
console.log('getLocation 请求已完成');
|
}
|
})
|
}
|
|
return {
|
latitude,
|
longitude,
|
address,
|
province,
|
error,
|
getLocation
|
}
|
}
|