微信小程序登录获取用户头像昵称

登录获取用户头像昵称

代码

app.js

App({
  globalData: {
    userInfo: null
  },
  onLaunch: function () {
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log("全局登录成功",res)
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          console.log("全局--用户已经授权")
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }else{
          console.log("全局--用户还没有授权")
        }
      }
    })
  },
  
})

.
.
.

index.js

const app = getApp()
Page({
  data: {    
    userInfo: {},
    hasUserInfo: false, //获取头像昵称 按钮是否显示,false的时候就显示
    canIUse: wx.canIUse('button.open-type.getUserInfo') //利用canIUse变量去兼容低版本
  },
  //点击头像----事件
  bindViewTap: function() {
    console.log("点击了头像")
  },
  /**
   * 页面加载
   */
  onLoad: function() {
    if (app.globalData.userInfo) {//在全局中获得了用户信息
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  
  },
  
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  
})

.
.
index.wxml

<view class="container">
  <view class="userinfo">
    <!-- hasUserInfo为false表示没有用户信息,就显示获取头像昵称的按钮 -->
    <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
    <!-- 表示有用户信息,就显示头像和昵称 -->
    <block wx:else>
      <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
      <text class="userinfo-nickname">{{userInfo.nickName}}</text>
    </block>
  </view>

</view>

.
.
index.wxss

.userinfo {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.userinfo-avatar {
  width: 128rpx;
  height: 128rpx;
  margin: 20rpx;
  border-radius: 50%;
}

.userinfo-nickname {
  color: #aaa;
}

.usermotto {
  margin-top: 200px;
}
原创文章 38 获赞 39 访问量 6万+

猜你喜欢

转载自blog.csdn.net/wy313622821/article/details/105975436