微信小程序button受权页面,用户拒绝后仍可再次受权

微信小程序受权页面,进入小程序若是没受权跳转到受权页面,受权后跳转到首页,若是用户点拒绝下次进入小程序仍是能跳转到受权页面,受权页面以下html

app.js  中的 onLaunch或onShow中加以下代码,若是没受权跳转到受权页面小程序

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经受权,能够直接调用 getUserInfo 获取头像昵称,不会弹框
          wx.getUserInfo({
            success: res => {
              // 能够将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 因为 getUserInfo 是网络请求,可能会在 Page.onLoad 以后才返回
              // 因此此处加入 callback 以防止这种状况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }else{
          // 未受权,跳转到受权页面
          wx.reLaunch({
            url: '/pages/auth/auth',
          })
        }
      }
    })

auth.wxml 受权页面结构微信小程序

<view class="auth">
  <image src="https://res.wx.qq.com/wxopenres/htmledition/images/favicon32f740.ico" class="img" mode="aspectFill"></image>
  <view class="title">微信受权页面</view>
  <view class="describe">此页面是微信受权页面,点击下方按钮弹出受权或跳转页面</view>
  <button class="btn" open-type='getUserInfo' wx:if="{{canIUse}}" bindgetuserinfo='onAuth'>点击微信受权</button>
  <navigator wx:if="{{!canIUse}}" class="btn" url="/pages/auth/auth" open-type="reLaunch" hover-class="other-navigator-hover">已经受权点击调转</navigator>
</view>

auth.wxss  受权页面样式微信

/* 开始 */
page {
  height: 100%;
  display: table;
}

.auth {
  margin-top: 0;
  text-align: center;
  display: table-cell;
  flex-direction: column;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
  padding: 100rpx;
  vertical-align: middle;
}

.img {
  border-radius: 50%;
  border: 1px solid #fff;
  background-color: #fff;
  margin: 0 0 60rpx;
  display: inline-block;
  width: 200rpx;
  height: 200rpx;
  line-height: 0;
}

.title {
  display: inline-block;
  width: 100%;
  margin: 0 0 60rpx;
}

.describe {
  color: #a7aaa9;
  font-size: 26rpx;
  margin: 0 0 60rpx;
  border-radius: 50%;
  text-align: center;
  display: inline-block;
  width: 100%;
}

.btn {
  padding: 0 60rpx;
  background-color: #19be6b;
  margin: 20rpx 0 200rpx;
  text-align: center;
  vertical-align: middle;
  touch-action: manipulation;
  cursor: pointer;
  background-image: none;
  white-space: nowrap;
  user-select: none;
  font-size: 14px;
  border: 0 !important;
  position: relative;
  text-decoration: none;
  height: 44px;
  line-height: 44px;
  box-shadow: inset 0 0 0 1px #19be6b;  
  background: #fff !important;
  color: #19be6b !important;
  display: inline-block;
  border-radius: 10rpx;
}

auth.js  受权页面js,点击受权后跳转到首页网络

var app = getApp();
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onAuth() {
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          wx.reLaunch({
            url: '../index/index',
          })
        }
      }
    })
  }
})

==============================================app

 本文连接 http://www.javashuo.com/article/p-htwmofjf-dd.htmlxss

==============================================flex

相关文章
相关标签/搜索