tj
2025-05-28 6ef1b14f735acdc3ff77a50da1bb09a5bb983dcc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
  }
}