設置API:wx.openSetting,wx.getSetting使用說明(示例:地圖授權與取消授權后的重新授權)


這個API解決了過去一個長久以來無法解決的問題,如何讓用戶重復授權;

打開小程序的設置界面:就是主動調取授權



目前資料極少,但是已經可以讓大家先看看了;

官方文檔地址:
https://mp.weixin.qq.com/debug/wxadoc/dev/api/setting.html#opensettingobject

基礎庫版本 1.1.0 開始支持,低版本需做兼容處理
調起客戶端小程序設置界面,返回用戶設置的操作結果



目前的授權影響范圍:

scope 說明:

 

scope 對應接口
scope.userInfo wx.getUserInfo
scope.userLocation wx.getLocation, wx.chooseLocation
scope.address wx.chooseAddress
scope.record wx.startRecord



相關文章:

微信小程序重新調起授權用戶信息,掃碼進入小程序獲取二維碼攜帶參數
wx.openSetting,wx.setClipboardData,wx.getClipboardData
微信小程序最新更新--api測試一覽

微信小程序之新版本測試

相關討論:
wx.openSetting()怎么判斷success里面 用戶有沒有授權了我寫的

[AppleScript]  純文本查看 復制代碼
?
1
2
3
4
5
6
7
8
9
wx.openSetting ( {
       success : ( res ) = > {
           console. log ( "授權結果.." )
         console. log ( res )
         if ( !res.authSetting.scope.userInfo || !res.authSetting.scope.userLocation ) {
             applyNotice ( )
         }
       }
     } )


不正常啊,res的authSetting兩個對象,兩個對象的鍵名是scope.userInfo和scope.userLocation都是帶了 . 的,怎么判斷用戶都授權了呢?我的小程序需要用戶授權才能使用服務呢。做的事基於LBS的共享服務,沒有定位和用戶登錄,就無法獲取服務器上的區域划分的數據。用戶就看不了自己周圍的地圖和自己的用戶信息

[AppleScript]  純文本查看 復制代碼
?
1
2
3
4
5
6
7
8
9
wx.openSetting ( {
       success : ( res ) = > {
           console. log ( "授權結果.." )
         console. log ( res )
         if ( !res.authSetting[ "scope.userInfo" ] || !res.authSetting[ "scope.userLocation" ] ) {
             applyNotice ( )
         }
       }
     } )
試試上述改動。



打印出來的console的可以判斷了,對象還可以像數組一樣鍵值調用,謝謝啦!

 

 


相關討論:
用戶取消后如何再次調出允許授權的modal?
如果用戶錯點了拒絕,就沒辦法再繼續進入使用小程序了。能重新再調出許可的小窗讓用戶重新再選擇嗎?
答:林超

使用 wx.openSetting接口

 

相關討論:

 

關於用戶授權界面的喚醒
用戶如果不小心拒絕了授權,之前是過十分鍾之后還可以被喚醒,最近發現 不醒了,一直是:"getUserInfo:fail auth deny",該如何應對?
答: Tïedào

//如下,在getUserInfo失敗后調用wx.openSetting即可

 

如下是我的真實示例:

// pages/contactus/contactus.js
var app = getApp()
Page({

    /**
     * 頁面的初始數據
     */
    data: {
        loadingHidden: false,
        latitude: 23.099994,
        longitude: 113.324520,
        markers: [{
            iconPath: '../../images/location.png',
            id: 1,
            latitude: 31.245442,
            longitude: 121.506337,
            title: '千卉攝影',
            width: 50,
            height: 50,
            callout: {
                content: '千卉攝影',
                color: '#ff00000',
                fontSize: '18',
                borderRadius: '5',
                bgColor: '#ffffff',
                padding: '10',
                display: 'ALWAYS',
                textAlign: 'center'
            }
        }],
    },

    /**
     * 生命周期函數--監聽頁面加載
     */
    onLoad: function (options) {
        var that = this
        wx.getSystemInfo({
            success: function (res) {
                // console.log(res)
                that.setData({
                    scrollHeight: res.windowHeight
                });
            }
        });
      wx.getSetting({
        success(res) {
          console.log(!res.authSetting['scope.userLocation']);
          if (!res.authSetting['scope.userLocation']) {
            wx.authorize({
              scope: 'scope.userLocation',
              success() {
                // 用戶已經同意
                //其他操作...
                console.log("用戶已經同意位置授權");
              },
              fail() {
                console.log("用戶已經拒絕位置授權");
                that.openConfirm();//如果拒絕,在這里進行再次獲取授權的操作
              }
            })
          }
          //其他操作..
        }
      });
    },
  //當用戶第一次拒絕后再次請求授權
  openConfirm: function () {
    wx.showModal({
      content: '檢測到您沒打開此小程序的定位權限,是否去設置打開?',
      confirmText: "確認",
      cancelText: "取消",
      success: function (res) {
        console.log(res);
        //點擊“確認”時打開設置頁面
        if (res.confirm) {
          console.log('用戶點擊確認')
          wx.openSetting({
            success: (res) => { }
          })
        } else {
          console.log('用戶點擊取消')
        }
      }
    });
  },
    /**
     * 生命周期函數--監聽頁面初次渲染完成
     */
    onReady: function (e) {
        this.mapCtx = wx.createMapContext('myMap')
    },

    /**
     * 生命周期函數--監聽頁面顯示
     */
    onShow: function () {
        var that = this
        app.reqGetfunc.reqGet('qh_contact.html', {}, function (res) {
            // console.log(res)
            that.setData({
                tel: res.tel,
                address: res.address,
                map: res.map,
                qq: res.qq,
                wechat: res.wechat,
                code: res.code,
                loadingHidden: true
            })

        })
    },
    getLocation:function(){
        var that = this
        wx.getSetting({
          success(res) {
            console.log(res)
            if (!res.authSetting['scope.userLocation']) {
              wx.authorize({
                scope: 'scope.userLocation',
                success() {
                  // 用戶已經同意小程序使用此功能,后續調用 wx.getLocation 接口不會彈窗詢問
                  wx.getLocation({
                    type: 'gcj02',
                    success: function (res) {
                      //console.log(res)
                      var latitude = res.latitude
                      var longitude = res.longitude
                      var speed = res.speed
                      var accuracy = res.accuracy
                      console.log("latitude:" + latitude)
                      console.log("longitude:" + longitude)
                      console.log("speed:" + speed)
                      console.log("accuracy:" + accuracy)
                      wx.openLocation({
                        name: '山東省',
                        address: '山東省臨沂市千卉攝影',
                        //latitude: latitude,
                        //longitude: longitude,
                        latitude: Number(that.data.map.latitude),
                        longitude: Number(that.data.map.longitude),
                        scale: 28
                      })
                    }
                  })
                },
                fail() {
                  console.log("用戶已經拒絕位置授權");
                  that.openConfirm();//如果拒絕,在這里進行再次獲取授權的操作
                }
              })
            } 
          }
        })
    },
    chooseLocation:function(){
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.userLocation']) {
            wx.authorize({
              scope: 'scope.userLocation',
              success(){
                wx.chooseLocation({
                  success: function (res) {
                    var name = res.name
                    var address = res.address
                    var latitude = res.latitude
                    var longitude = res.longitude
                  }
                })
              }

            })
          }
        },
        fail() {
          console.log("用戶已經拒絕位置授權");
          that.openConfirm();//如果拒絕,在這里進行再次獲取授權的操作
        }
      })
        
    },
    /**
     * 生命周期函數--監聽頁面隱藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函數--監聽頁面卸載
     */
    onUnload: function () {

    },

    /**
     * 用戶點擊右上角分享
     */
    onShareAppMessage: function () {

    }
})


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM