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

配置旧版本基础库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
        })
      }
    })
  }
})
  • 控制台打印微信登录用户基本信息
相关推荐
拖孩2 小时前
给小程序加「一键发公众号贴图」,我把 wx.shareToOfficialAccount 的三个坑趟了一遍
前端·后端·微信小程序
微擎应用市场2 小时前
鲸飞云 PMS 系统 —— 现代化酒店运营的智能管理解决方案
小程序
前端小白乘风4 小时前
VS Code 终于能连微信了!发现一个硬核开源神仙插件:WeChat AHP
微信·visual studio code·deepseek
M哥支付4 小时前
商户池是什么?
服务器·网络·其他·微信·金融
Ztt6666666665 小时前
一抹霞红停在圆肩壶上,留白让颜色更醒目
经验分享·笔记·其他·百度·微信公众平台
疯狂小猫咪6 小时前
教务管理系统课时消课模块设计思路
大数据·小程序
孙严Pay7 小时前
代付 VS 纯代付
笔记·科技·计算机网络·其他·微信
meiying88388 小时前
微信商城小程序怎么开发?2026 从店铺架构到下单链路的技术选型拆解
小程序
iPad协议个微协议8 小时前
微信消息进入工单系统为什么需要候选层:WechatApi 接入后的服务流程设计
微信·自动化·微信开发·wechatapi·个人微信号二次开发·微信api·微信二次开发
扶风ff21 小时前
练题簿小程序:家庭在线练题,课后复习更简单
学习·小程序