当前位置: 首页 > 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.xdnf.cn/news/1442089.html

相关文章:

  • 20250901 搜索总结
  • 免费专业软件推荐 | 图片/PDF水印添加神器:艾克斯水印工具使用全攻略
  • java中二维数组笔记
  • Git或TortoiseGit的小BUG(可解决):空库报错Could not get hash of ““
  • Nginx中的内置变量、指令、URL重写功能及其虚拟主机配置、负载均衡配置
  • 关于linux编程——网络编程2
  • 工业4.0时代的通信革命:OPC UA Pub/Sub机制全面解析
  • 百万级并发下的微服务架构设计之道:从阿里双11看分布式系统核心原则与落地实践
  • 云计算培训为什么这么贵?
  • EagleTrader观察|你的固定心态,可能正在悄悄让你交易破产
  • Element UI MessageBox 渲染虚拟节点的坑与解决方案
  • 【深度学习新浪潮】用3DGS做三维重建有哪些主要的技术路线可供选择?
  • 【随手记】vscode中C语言满足KR风格的方法
  • Leetcode—695. 岛屿的最大面积【中等】
  • Docker实战指南:从安装到架构解析
  • 【Linux】网络(中)
  • 数据结构:栈和队列(上)
  • 数据结构从青铜到王者第十九话---Map和Set(2)
  • 下载必要软件
  • Qt读写Excel--QXlsx基本使用
  • 基于-轻量级文档搜索系统的测试报告
  • 【GM3568JHF】FPGA+ARM异构开发板 使用指南:WIFI
  • SQLite3 操作指南:SQL 语句与 ORM 方法对比解析​
  • 存算一体:重构AI计算的革命性技术(1)
  • K8s Pod CrashLoopBackOff:从镜像构建到探针配置的排查过程
  • react-android-0.80.2-debug.aar下载很慢
  • GitHub 宕机自救指南技术文章大纲
  • Flutter Android真机器调式,虚拟机调试以及在Vscode中开发Flutter应用
  • 充电座结构设计点-经验总结
  • 10.2 工程学中的矩阵(2)