小程序开发坑之---------微信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)
},
 
 
posted @ 2018-05-15 09:51  漠小飞  阅读(3201)  评论(0编辑  收藏  举报