欢迎您光临深圳塔灯网络科技有限公司!
电话图标 余先生:13699882642

网站百科

为您解码网站建设的点点滴滴

微信小程序授权登录取消授权重新授权处理方法 附可用代码

发表日期:2019-11 文章编辑:小灯 浏览次数:11348

微信小程序授权登录基本是小程序的标配了,但是官方的demo,取消授权后,就不能再重新点击登录,除非重新加载小程序才可以,这下怎么办?

我们可以先在首页引导用户点击,然后跳转到一个新的页面,在新的页面进行授权,然后新的页面授权成功,立马跳回首页,显示用户信息。

话不多说,直接上代码

代码结构:

index是首页
login是授权页

首页代码

index.wxml

<!-- 未授权,只显示一个授权按钮 -->
<view wx:if="{{result==false}}">
  <button bindtap="getinfo" class="loginbtn"> 授权登录 </button>
</view>

<!-- 授权后只显示头像和昵称 -->
<view elif="{{result==true}}" class="info">
  <image src="{{head}}" class="headimg"></image>
  <text class="nickname">{{name}}</text>
</view>

index.wxss

/**index.wxss**/
.loginbtn{
  width: 150px;
  height: 45px;
  background: #06C05F;
  margin:100px auto 0;
  line-height: 45px;
  font-size: 15px;
  color: #fff;
}

.info{
  width: 80px;
  height: 100px;
  margin:50px auto 0;
}

.info .headimg{
  width: 80px;
  height: 80px;
  border-radius: 100%;
}

.info .nickname{
  text-align: center;
}

index.js

//index.js
Page({
  data: {
    userInfo: {},
    hasUserInfo: false
  },

  //事件处理函数
  getinfo: function () {
    wx.navigateTo({
      url: '../login/index'
    })
  },

  onLoad: function (e) {
    let that = this;
    // 获取用户信息
    wx.getSetting({
      success(res) {
        // console.log("res", res)
        if (res.authSetting['scope.userInfo']) {
          console.log("已授权")
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success(res) {
              console.log("获取用户信息成功", res)
              that.setData({
                name: res.userInfo.nickName,
                head: res.userInfo.avatarUrl,
                result: true
              })
            },
            fail(res) {
              console.log("获取用户信息失败", res)
              that.setData({
                result: "取消授权"
              })
            }
          })
        } else {
          console.log("未授权")
          that.setData({
            result: false
          })
        }
      }
    })
  }
})

授权页代码

index.wxml

<!--index.wxml-->
<button open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 授权获取用户信息 </button>

index.js

//index.js
Page({
  data: {
    userInfo: {},
    hasUserInfo: false
  },

  getUserInfo: function (e) {
    let that = this;
    // 获取用户信息
    wx.getSetting({
      success(res) {
        // console.log("res", res)
        if (res.authSetting['scope.userInfo']) {
          console.log("已授权=====")
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success(res) {
              console.log("获取用户信息成功", res)
              that.setData({
                name: res.userInfo.nickName,
                head: res.userInfo.avatarUrl
              })
              wx.reLaunch({
                url: '../index/index'
              })
            },
            fail(res) {
              console.log("获取用户信息失败", res)
            }
          })
        } else {
          console.log("未授权=====")
        }
      }
    })
  }
})

不懂可以咨询我

WeChat:face6009
Web:http:likeyunba.com
Date:2019-10-17
Author:TANKING


本页内容由塔灯网络科技有限公司通过网络收集编辑所得,所有资料仅供用户学习参考,本站不拥有所有权,如您认为本网页中由涉嫌抄袭的内容,请及时与我们联系,并提供相关证据,工作人员会在5工作日内联系您,一经查实,本站立刻删除侵权内容。本文链接:https://www.dengtar.com/25236.html
相关小程序
 八年  行业经验

多一份参考,总有益处

联系深圳网站公司塔灯网络,免费获得网站建设方案及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:余经理:13699882642

Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.