From 440b4ab9d4278d3f9c13b004d2c2289f45254cbc Mon Sep 17 00:00:00 2001
From: Liuyi <candymxq888@outlook.com>
Date: 星期二, 19 十一月 2024 17:29:06 +0800
Subject: [PATCH] 添加积分记录,修改位置获取

---
 pages/index/index.vue |   71 ++++++++++++++++++++++++++++++++++-
 1 files changed, 69 insertions(+), 2 deletions(-)

diff --git a/pages/index/index.vue b/pages/index/index.vue
index 2609622..3cb01bd 100644
--- a/pages/index/index.vue
+++ b/pages/index/index.vue
@@ -35,6 +35,7 @@
 	 async function getVipInfo(){
 		await getVipInfoApi().then((res) =>{
 			if(res.code == 200 && res.data.id){
+				console.log('获取会员卡返回响应-首页',res)
 				cardInfo.value.waterCardNumber = res.data.cardNumber
 				cardInfo.value.cardType = res.data.cardType
 				//用户信息处理存储
@@ -205,9 +206,77 @@
 			icon:'none',
 		});
 	}
+	//申请获取用户位置权限----------------------------------------------------
+	let userLocation = {
+		lat:'',
+		lon:'',
+	}
+	function getUserLocation(){
+		// 请求用户授权,第一次进入首页会有位置授权的提示
+		uni.authorize({
+		  scope: 'scope.userLocation',
+		  success() {
+			 console.log("成功授权位置信息1")
+			 storageLocation()
+		  },
+		  fail() {
+			  console.log("用户拒绝授权位置信息,再次提示用户授权")
+			  showToast()
+		  }
+		})
+		//获取用户位置并存储
+		function storageLocation(){
+			uni.getLocation({
+				type:'gcj02',
+				isHighAccuracy:true,
+				success:(res) =>{
+					userLocation.lat = res.latitude
+					userLocation.lon = res.longitude
+					uni.setStorageSync('userLocation',JSON.stringify(userLocation))
+				},
+				fail:() =>{
+					showToast()
+				}
+			})
+		}
+		//用户拒绝授权
+		function showToast(){
+			uni.showModal({
+				title: "请求授权当前位置",
+				content: "请求获取您的位置,加载附近饮水设备信息!",
+				confirmText: "前往设置",
+				success: (res) => {
+					if (res.confirm) {
+						uni.openSetting({
+							 success:(res1) =>{ //打开设置成功
+								if (res1.authSetting['scope.userLocation']){
+									console.log('用户二次授权成功')
+									setTimeout(() =>{
+										storageLocation()
+									},1000)
+								}else{
+									showToast()
+									console.log('用户拒绝授权')
+								}
+							},
+						})
+					}else{
+						uni.showToast({
+							title: '请先授权!',
+							duration: 2000,
+							icon:'none'
+						});
+						showToast()
+					}
+				},
+			});
+		}
+	}
 	onMounted( async() =>{
 		getTopHeight()
 		await getInfo()
+		await getUserLocation()
+		console.log('进入首页了')
 	})
 	onShow(async() =>{
 		getTopHeight()
@@ -223,8 +292,6 @@
 	    console.error('分享菜单显示失败', err);
 	  }
 	});
-	
-
 </script>
 
 <template>

--
Gitblit v1.9.3