小程序開發坑之---------微信api更新,不會主動彈出授權框,getUserInfo使用報錯


解決方法:自寫一個展示框,判斷是否授權,未授權的情況下彈出授權框,授權后不會彈出

代碼:

彈出框頁面view:
 
<view wx:if="{{!canIUse&&showShouquan}}" style='height:100vh;width:100vw;opacity:.5;position:absolute;top:0'></view>
<view wx:if="{{!canIUse&&showShouquan}}" style='color:#333333;position:absolute;top:340rpx;width:70vw;left:9vw;padding:6vw'>
<view style='font-size:42rpx'>微信授權</view>
<view style='font-size:30rpx;font-weight:bold;text-align:center;padding:20rpx;border-bottom:1px solid #e9e9e9'>....網絡科技申請獲取以下權限:</view>
<view style='font-size:26rpx;text-align:center;margin:30rpx 30rpx 70rpx 30rpx'><text>獲得你的公開信息(昵稱、頭像等)</text></view>
<view style='font-size:28rpx;float:right;margin-top:10rpx'>
<text wx:if="{{!canIUse&&showShouquan}}" bindtap='hidthis' style='color:#999999;margin-left:60rpx;height:60rpx;line-height:60rpx;font-size:30rpx;border:none;position:absolute;top:300rpx;left:300rpx'>拒絕</text>
<button wx:if="{{!canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo" style='color:#31B531;margin-left:60rpx;height:60rpx;line-height:60rpx;font-size:30rpx;border:none;position:absolute;top:300rpx;left:400rpx'>允許</button>
</view>
<!-- userInfo:getApp().globalData.userInfo -->
</view>

js:

data: {
  canIUse: wx.canIUse('button.open-type.getUserInfo'),
  showShouquan:true
},
onLoad: function (options) {
  // 查看是否授權
  wx.getSetting({
    success: function (res) {
      console.log(res.authSetting['scope.userInfo'])
      if (res.authSetting['scope.userInfo']) {
      // 已經授權,可以直接調用 getUserInfo 獲取頭像昵稱
      wx.getUserInfo({
        success: function (res) {
        console.log(res)
        getApp().globalData.userInfo = res.userInfo   //將授權信息傳遞給全局變量
      }
    })
    }
  }
  })
},
bindGetUserInfo: function (e) {
  getApp().globalData.userInfo = e.detail.userInfo     //將授權信息傳遞給全局變量
  console.log(getApp().globalData.userInfo)
},
 
 


免責聲明!

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



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