線上服務(wù)咨詢
Article/文章
記錄成長點滴 分享您我感悟
如何重新授權(quán)微信小程序如何編寫特定代碼
程序員每天都會遇到各種各樣的問題,但基本上無論遇到什么問題,都可以解決。因為他們有一顆無所畏懼的心,他們給了小程序的靈魂,但是如何重新授權(quán)微信小程序?今天我們將為每個人解決這個問題。
微信小程序怎么重新授權(quán)?
原點
·在做一些小程序時,授權(quán)問題一定不能少,但我們發(fā)現(xiàn)有時會有人點擊我們的拒絕授權(quán)。然后每個人在開發(fā)他們需要的數(shù)據(jù)時都會非常困擾。我在做小程序。我已經(jīng)應(yīng)用了這種方法,我目前正在分享它?。
·我的演示基于我的個人信息+地理授權(quán)
想法
·被授權(quán)進入主頁
·拒絕授權(quán)并留在具有授權(quán)訪問權(quán)的頁面上
·設(shè)置標(biāo)志值:authorizeInfo,根據(jù)此true和false確定是否呈現(xiàn)主頁或呈現(xiàn)頁面以顯示重新授權(quán)。
過程
在頁面顯示時獲取用戶信息和位置(當(dāng)然,這是我需要的)
//userInfo
wx.getUserInfo({
成功: res=> {
this.setData({userInfo: true})
},
失敗: res=> {
this.setData({userInfo: false})
}
})
//locationInfo
wx.getLocation({
成功: res=> {
this.setData({locationInfo: true})
},
失敗: res=> {
this.setData({locationInfo: false})
}
})
authorizeInfo的設(shè)置應(yīng)該應(yīng)用于剛剛獲得的兩個值。我們可以將計時器設(shè)置為連續(xù)執(zhí)行authorizeInfo,直到userInfo和locationInfo值都為true。清除計時器(設(shè)置計時器,因為它剛剛開始獲取userInfo并且locationInfo可能失敗),所以我們記得當(dāng)兩者都為真時,所有授權(quán)都已通過并跳轉(zhuǎn)到主頁。否則,它將保留在授權(quán)頁面上。
//所有授權(quán)
設(shè)timer=setInterval(()=> {
this.authorizeInfo();
如果(this.data.userInfo&& this.data.locationInfo){
clearInterval(定時器)
}
},100)
//authorizeInfo
authorizeInfo: function(){
如果(this.data.userInfo&& this.data.locationInfo){
this.setData({authorizeInfo: true})
//重新啟動
wx.reLaunch({
網(wǎng)址:'/pages/index/index'
})
} else {
this.setData({authorizeInfo: false})
}
}
重新授權(quán)此操作時,必須調(diào)用wx.openSetting接口。我們知道返回值用于確定授權(quán)操作后是否再次授權(quán)用戶。如果是,則跳轉(zhuǎn)到主頁,否則它將保留在授權(quán)頁面上。
//toAuthorize
toAuthorize: function(){
//重新授權(quán)許可
wx.openSetting({
成功:(res)=> {
if(res.authSetting ['scope.userInfo']&& res.authSetting ['scope.userLocation']){
this.setData({authorizeInfo: true})
//重新啟動
wx.reLaunch({
網(wǎng)址:'/pages/index/index'
})
}否則{
this.setData({authorizeInfo: false})
}
},
失敗:(res)=> {
Console.log('授權(quán)失敗')
}
})
如何使用
·我的演示是個人信息和位置的雙重授權(quán)
·可以直接使用頁面下的授權(quán)文件夾。將其復(fù)制并粘貼到您的頁面中!
通過上面的介紹,現(xiàn)在您知道如何重新授權(quán)微信小程序。其實這個方法比較簡單,調(diào)試比較多,基本上無論遇到什么問題都可以解決。當(dāng)然,如果您在操作過程中遇到任何問題,您可以隨時關(guān)注我們,我們會為您解決這個問題。
沈陽小程序開發(fā),微信小程序開發(fā),微信小程序