From 6ef1b14f735acdc3ff77a50da1bb09a5bb983dcc Mon Sep 17 00:00:00 2001
From: tj <1378534974@qq.com>
Date: 星期三, 28 五月 2025 17:37:06 +0800
Subject: [PATCH] location,userinfo

---
 composables/useLocation.ts |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 55 insertions(+), 0 deletions(-)

diff --git a/composables/useLocation.ts b/composables/useLocation.ts
new file mode 100644
index 0000000..cacb128
--- /dev/null
+++ b/composables/useLocation.ts
@@ -0,0 +1,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
+  }
+}

--
Gitblit v1.9.3