From 9a139cc25da8396c1b257ccf99eb8d2fedfbc94d Mon Sep 17 00:00:00 2001
From: Liuyi <candymxq888@outlook.com>
Date: 星期三, 23 十月 2024 17:24:35 +0800
Subject: [PATCH] 添加校验,修改头像

---
 App.vue |   77 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 76 insertions(+), 1 deletions(-)

diff --git a/App.vue b/App.vue
index e636cbd..470a053 100644
--- a/App.vue
+++ b/App.vue
@@ -1,7 +1,9 @@
 <script setup>
 	import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
 	import { wxLoginApi } from './api/index.js'
-	onLaunch(() => {
+	
+	//用户打开小程序默认授权
+	function wxLogin(){
 		uni.removeStorageSync('openId');
 		if(!uni.getStorageSync('openId')){
 			wx.login({
@@ -14,10 +16,83 @@
 				  })
 				},
 				fail:(e) =>{
+					uni.showToast({
+						title: '登录失败,请退出重试!',
+						duration: 2000
+					});
 					console.log(e)
 				}
 			})
 		}
+	}
+	
+	//申请获取用户位置权限
+	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('用户二次授权成功')
+									storageLocation()
+								}else{
+									showToast()
+									console.log('用户拒绝授权')
+								}
+							},
+						})
+					}else{
+						uni.showToast({
+							title: '请先授权!',
+							duration: 2000,
+							icon:'none'
+						});
+						showToast()
+					}
+				},
+			});
+		}
+	}
+	onLaunch(async() => {
+		await wxLogin()
+		await getUserLocation()
 	});
 	onShow(() => {
 	  console.log("App Show");

--
Gitblit v1.9.3