【微信小程序】引導重新打開定位權限

原文鏈接:https://blog.csdn.net/Maria028/article/details/82804955
問題來源:
用戶使用小程序時若關閉了定位權限,想要重新打開時操作不易,所以要引導去設置里重新打開
解決:
當第一次關閉授權定位后,wx.chooseLocation就會一直調用fail方法,所以當點擊打開位置功能時,最好先判斷當前是否關閉了定位授權(見wx.getSetting文檔詳情),再會跳出自定義彈框(showCon),引導前往設置打開定位

wx.chooseLocation({
      success: function(e) {
       //允許打開定位
      },
      fail: () => {
      //不允許打開定位
        wx.getSetting({
          success: (res) => {
            if (!res.authSetting['scope.userLocation']) {
            //打開提示框,提示前往設置頁面
              that.setData({
                showCon: true
              })
            }
          }
        })
      }
    })

由于前往設置需要使用button的open-type,因此彈框需要自定義,例如下方,不需要彈框的情況下引用button組件即可(詳情見官方文檔)


 //wxml
 <view  wx:if="{{showCon}}" class="modal-mask" bindtap="changeModalCancel">
    <view class="modal-dialog">
      <view class="modal-title">溫馨提示</view>
      <view class="modal-content">
        獲取定位失敗,請前往設置打開定位權限
      </view>
      <view class="modal-footer">
        <view class="btn-cancel" catchtap="changeModalCancel">取消</view>
        <button open-type="openSetting" class="btn-confirm button-on-view" style="padding:0rpx;" catchtap="changeModalCancel">設置</button>
      </view>
    </view>
  </view>
//wxss
    .modal-mask {
      width: 100%;
      height: 100%;
      position: fixed;
      top: 0;
      left: 0;
      background-color: rgba(0, 0, 0, 0.5);
      overflow: hidden;
      z-index: 9999;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .modal-dialog {
      width: 540rpx;
      overflow: hidden;
      z-index: 9999;
      background: #f9f9f9;
      border-radius: 5rpx;
    }
    .modal-title {
      padding-top: 30rpx;
      font-size: 32rpx;
      color: #030303;
      text-align: center;
    }
    .modal-content {
      padding: 20rpx 32rpx;
      font-size: 28rpx;  
    }
    .modal-footer {
      display: flex;
      flex-direction: row;
      height: 86rpx;
      border-top: 1px solid #dedede;
      font-size: 34rpx;
      line-height: 86rpx;
    }
    .btn-cancel {
      width: 50%;
      color: #abb4bd;
      text-align: center;
      border-right: 1px solid #dedede;
    }
    .btn-confirm {
      width: 50%;
      color: #6fb64b;
      text-align: center;
      font-weight: 500;
    }

以上主要是通過wx.chooseLocation、 wx.getSetting、button的open-type="openSetting"的運用,同樣也適用于打開用戶信息權限,希望有所幫助!

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容