微信小程序用户登录授权指定(旧版本)

配置旧版本基础库2.12.3

实现效果

  • 点击登录按钮即可直接登录,获取用户昵称和头像

  • 点击获取头像昵称按钮则需要授权,才能成功登录

代码实现

  • my.xml
bash 复制代码
<!-- 登录页面,调试基础库为2.20.2库 -->
<view class="mylogin">
	<block wx:if="{{isLogin}}">
		<image src="{{src}}"></image>
		<text>{{nickName}}</text>
	</block>
	<button wx:else open-type="getUserInfo" bindgetuserinfo="getMyInfo">未登录,点此登录</button>
</view>

<view class="container">
  <view class="userinfo">
    <block wx:if="{{!hasUserInfo}}">
      <button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button>
      <button wx:else open-type="getUserInfo" bindgetuserinfo="getUserProfile"> 获取头像昵称 </button>
    </block>
    <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>
  • my.js
bash 复制代码
Page({
  /**
   * 页面的初始数据
   */
  data: {
    nickName:"未登录"
  },
	//获取个人信息,调试基础库为2.12.3
  getMyInfo:function(e){
    let that = this;
    wx.login({
      success (res) {
        if (res.code) {
          let info = e.detail.userInfo
        } else {
          console.log('登录失败!' + res.errMsg)
        }
        that.setData({
          src: e.detail.userInfo.avatarUrl,
          nickName: e.detail.userInfo.nickName,
          isLogin: true
        })
      }
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  }
})
  • 控制台打印微信登录用户基本信息
相关推荐
show4331 天前
2026小程序端视频转文字技术对比:识别引擎精度实测
小程序·音视频
sam-zy2 天前
微信小程序+ESP8266+Arduino 开发智能插座,有本地OTA功能
微信小程序·小程序
A24207349302 天前
微信小程序开发:常用基础语法与指令详解
微信小程序·小程序·notepad++
黄华SJ520it2 天前
青蓝送水类型社交裂变小程序系统技术拆解
小程序·系统开发
Ztt6666666662 天前
荷口花瓶从瓶口开出一朵花
笔记·其他·百度·微信公众平台·微信开放平台
weikecms2 天前
星巴克API接口,星巴克点餐接口开发
大数据·小程序
游戏开发爱好者83 天前
系统状态:专为iPhone和iPad设计的实时系统性能监控工具全面介绍
android·ios·小程序·uni-app·iphone·webview·ipad
Lemomate-AI3 天前
LemoChat vs 顶级模型全景对比
百度·微信公众平台·facebook·twitter·新浪微博
2501_915909063 天前
全面解析iOS应用上架到App Store的完整流程与关键注意事项
android·macos·ios·小程序·uni-app·cocoa·iphone
Lemomate-AI3 天前
LemoChat如何超越顶级闭源方案
百度·微信公众平台·facebook·twitter·新浪微博