Browse Source

下单添加地理位置信息

gcz 8 months ago
parent
commit
2a2553d956
2 changed files with 68 additions and 3 deletions
  1. 5 1
      manifest.json
  2. 63 2
      pages/bookticket.vue

+ 5 - 1
manifest.json

@@ -57,7 +57,11 @@
             "postcss" : true
         },
         "usingComponents" : true,
-        "permission" : {},
+        "permission" : {
+			"scope.userLocation": {
+			"desc": "你的位置信息将用于小程序位置接口的效果展示"
+		  }
+		},
         // "scope.userLocation" : {
         //     "desc" : "你的位置信息将用于小程序位置接口的效果展示"
         // },

+ 63 - 2
pages/bookticket.vue

@@ -209,6 +209,8 @@ import {
 				label:null,//scene解析出来的
 				retailId:null,
 				couponList:[],
+				latitude:null,
+				longitude:null
 
 			}
 		},
@@ -467,7 +469,9 @@ import {
 					retailId:this.retailId,
 					coupon:{
 						memberCouponId:null
-					}
+					},
+					longitude:this.longitude,
+					latitude:this.latitude,
 				}
 				if(this.coupon.quota){
 					// console.log('this.coupon',this.coupon);
@@ -544,8 +548,39 @@ import {
 					}
 				});
 			},
+			showOpenLocationSettings() {
+			  uni.showModal({
+			    title: '提示',
+			    content: '需要获取您的地理位置,请前往设置中开启',
+			    confirmText: '去设置',
+			    success: (res) => {
+			      if (res.confirm) {
+			        uni.openSetting({
+			          success: (res) => {
+			            if (res.authSetting['scope.userLocation']) {
+			              this.setTemplate();
+			            }
+			          }
+			        });
+			      }
+			    }
+			  });
+			},
+			getUserLocation() {
+			  return new Promise((resolve, reject) => {
+			    uni.getLocation({
+			      type: 'wgs84',
+			      success: (res) => {
+			        resolve(res);
+			      },
+			      fail: (err) => {
+			        reject(err);
+			      }
+			    });
+			  });
+			},
 			// 设置小程序订阅消息
-			setTemplate() {
+			async setTemplate() {
 				if(this.needIdcardNumber&&this.needIdcardNumber!==this.visitors.length){
 					uni.showToast({
 						title:'观影人信息不正确',
@@ -553,6 +588,32 @@ import {
 					})
 					return false
 				}
+				
+				try {
+				    const location = await this.getUserLocation();
+				    this.longitude = location.longitude;
+				    this.latitude = location.latitude;
+				    if (!this.longitude || !this.latitude) {
+				      uni.showToast({
+				        title: '无法获取地理位置',
+				        icon: 'none'
+				      });
+				      return;
+				    }
+				    console.log('location', location);
+				  } catch (err) {
+				    if (err.errMsg === 'getLocation:fail auth deny') {
+				      // 用户拒绝授权,提示用户打开位置权限
+				      this.showOpenLocationSettings();
+				    } else {
+				      uni.showToast({
+				        title: '获取地理位置失败',
+				        icon: 'none'
+				      });
+				    }
+					return
+				  }
+				
 				let that = this;
 				// console.log('templateIdList', this.templateIdList);
 				// #ifdef MP