当前位置: 首页 > news >正文

Uniapp中进行微信小程序头像和昵称的更改

一、官方文档:

1、wx.getUserInfouni.getUserInfo):基础库版本低于2.27.1可用

① 文档链接:

  • https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserInfo.html
  • https://uniapp.dcloud.net.cn/api/plugins/login.html#getuserinfo

② 官方说明:

2021年4月28日24时后发布的小程序新版本,无法通过wx.getUserInfo<button open-type="getUserInfo"/>获取用户个人信息(头像、昵称、性别与地区),将直接获取匿名数据(包括userInfoencryptedData中的用户个人信息),获取加密后的openIDunionID数据的能力不做调整。此前发布的小程序版本不受影响,但如果要进行版本更新则需要进行适配

③ 替代方案 - 已经废弃:

<button open-type="getUserInfo" @getuserinfo="handleGetUserInfo">获取用户信息</button>

2、wx.getUserProfileuni.getUserProfile):基础库版本2.9.5~2.27.1可用

① 文档链接:

  • https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html#%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81
  • https://uniapp.dcloud.net.cn/api/plugins/login.html#getuserprofile

② 官方说明:

自 2022 年 10 月 25 日 24 时起,小程序 wx.getUserProfile 接口将被收回:生效期后发布的小程序新版本,通过 wx.getUserProfile 接口获取用户头像将统一返回默认灰色头像,昵称将统一返回 “微信用户”。

③ 替代方案 - 已经废弃:

<button @click="getUserProfile">获取用户信息</button>

3、非静默获取用户头像和昵称:

① 文档链接:

https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html

② 完整代码:

<template><view class="myIndex" :style="{ paddingTop: navbarHeight + 'px' }"><view class="custom-navbar" :style="{ paddingTop: statusBarHeight + 'px' }"><view class="navbar-title">{{title}}</view></view><view class="content"><!-- 头像 --><button open-type="chooseAvatar" @chooseavatar="onChooseAvatar" class="avatar-button"><image :src="avatar" mode="aspectFill" class="img" /></button><!-- 昵称 --><input type="nickname" placeholder="请输入昵称" v-model="nickName" @input="onKeyInput"/></view></view>
</template><script>export default {data() {return {title: '个人中心',navbarHeight: 0, // 导航栏高度statusBarHeight: 0,avatar: 'https://img0.baidu.com/it/u=3824830576,2699714066&fm=253&app=138&f=JPEG?w=800&h=804', // 默认的头像nickName: '默认的昵称', // 默认的昵称}},onLoad() {// 获取系统信息计算导航栏高度const systemInfo = uni.getSystemInfoSync();this.statusBarHeight = systemInfo.statusBarHeight;this.navbarHeight = this.statusBarHeight + 44; // 44是导航栏标准高度},methods: {// 点击进行头像选择onChooseAvatar(e) {const that = thisconst {avatarUrl} = e.detailuni.showLoading({ title: '上传中...' });// 构造请求的参数let params = {}uni.uploadFile({url: _uploadUrl, // 后端接口地址filePath: avatarUrl, // 临时文件路径name: 'imageFile', // 后端接收文件的参数名(必须与后端一致)formData: params,header: {// 请求头信息},success: (res) => {uni.hideLoading();// 根据后端返回的准确地址进行存储与渲染if (res.code === 200) {console.log('线上的新头像地址为', res.url)uni.showToast({ title: '头像更新成功', icon: 'success' });} else {uni.showToast({ title: result.msg || '上传失败', icon: 'none' });}},fail: (err) => {uni.hideLoading();console.error('上传失败:', err);uni.showToast({ title: '网络错误,请重试', icon: 'none' });}});},// 输入框输入内容onKeyInput() {// 在这里调用后端的接口,存储昵称console.log('这个是输入的数据', this.nickName)}}}
</script><style scoped>.myIndex {width: 100vw;height: 100vh;background-color: #F9F9FB;}.myIndex .custom-navbar {position: fixed;top: 0;left: 0;right: 0;z-index: 999;display: flex;align-items: center;}.myIndex .navbar-title {flex: 1;height: 88rpx;line-height: 88rpx;text-align: center;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;font-weight: 600;font-size: 32rpx;color: #000000;}.myIndex .content {position: absolute;top: 0;left: 0;width: 100vw;height: 420rpx;background: linear-gradient(180deg, #C6EBFD 0%, #F9F9FB 100%);padding-top: calc(var(--status-bar-height) + 88rpx + 36rpx);text-align: center;}.myIndex .avatar-button {z-index: 1;position: relative;width: 120rpx;height: 120rpx;border-radius: 100rpx;padding: 0;margin: 0 16rpx 0 0;}.myIndex .content-top .img {z-index: 10;position: relative;box-sizing: border-box;width: 120rpx;height: 120rpx;border-radius: 100rpx;border: 6rpx solid #fff;object-fit: cover;}
</style>

③ 效果显示:

在这里插入图片描述
在这里插入图片描述

4、用户信息社区公告:

https://developers.weixin.qq.com/community/develop/doc/00022c683e8a80b29bed2142b56c01

http://www.dtcms.com/a/364389.html

相关文章:

  • Jenkins 可观测最佳实践
  • Flutter Android真机器调式,虚拟机调试以及在Vscode中开发Flutter应用
  • 【Linux操作系统】简学深悟启示录:进程控制
  • unity中的交互控制脚本
  • 如何选择适合企业的海外智能客服系统:6 大核心维度 + 实战选型指南
  • 【STL源码剖析】从源码看 deque :拆解双端队列的底层实现与核心逻辑
  • 用友T3、T6/U8批量作废凭证
  • 从数据生成到不确定性估计:用 LSTM + 贝叶斯优化实现时间序列多步预测
  • 基于SpringBoot的旅游管理系统
  • 【大前端】React 使用 Redux 实现组件通信的 Demo 示例
  • React实现点击按钮复制操作【navigator.clipboard与document.execCommand】
  • 基于单片机PWM信号发生器系统Proteus仿真(含全部资料)
  • 平衡车 - 电机调速
  • 基于单片机车内换气温度检测空气质量检测系统Proteus仿真(含全部资料)
  • 单片机点灯
  • Linux 网络编程中核心函数`recv`。
  • zynq 开发系列 新手入门:GPIO 连接 MIO 控制 LED 闪烁(SDK 端代码编写详解)
  • Spring Boot 实现数据库表变更监听的 Redis 消息队列方案
  • 单片机控制两只直流电机正反转C语言
  • 变频器实习DAY42 VF与IF电机启动方式
  • Excel 电影名匹配图片路径教程:自动查找并写入系统全路径
  • wpf 自定义控件,只能输入小数点,并且能控制小数点位数
  • 机器学习从入门到精通 - Python环境搭建与Jupyter魔法:机器学习起航必备
  • 如何在modelscope上上传自己的MCP服务
  • 【收藏】2025 前端开发者必备 SVG 资源大全
  • 【2025ICCV-持续学习方向】一种用于提示持续学习(Prompt-based Continual Learning, PCL)的新方法
  • 【CouponHub开发记录】SpringAop和分布式锁进行自定义注解实现防止重复提交
  • RAG|| LangChain || LlamaIndex || RAGflow
  • kafka概念之间关系梳理
  • mac idea 配置了Gitlab的远程地址,但是每次pull 或者push 都要输入密码,怎么办