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

微信小程序获取头像昵称问题解决

前言和传统方法

微信小程序是一种基于微信开发平台的轻应用,用户可以通过微信扫码、搜索等方式进入小程序并使用。在小程序中,获取用户昵称和头像是非常常见的需求,因为这些信息可以用来个性化展示内容,提高用户体验。以下是获取用户头像昵称的传统方法:

  • 在小程序中,可以通过调用微信提供的API获取用户信息。具体步骤如下:

    (1) 在小程序的app.json文件中添加“scope.userInfo”权限,表示需要获取用户信息。

    (2) 在小程序中使用wx.getUserInfo()方法获取用户信息。

    (3) 获取到用户信息后,可以通过userInfo.nickName和userInfo.avatarUrl属性获取用户昵称和头像。

代码示例:

wx.getUserInfo({
  success: function(res) {
    var userInfo = res.userInfo
    var nickName = userInfo.nickName
    var avatarUrl = userInfo.avatarUrl
  }
})
  • 另外,如果只需要获取用户头像,可以直接调用微信提供的wx.getUserProfile()方法。

(1) 在小程序的app.json文件中添加“scope.userProfile”权限,表示需要获取用户头像。

(2) 在小程序中使用wx.getUserProfile()方法获取用户头像。

(3) 获取到用户头像后,可以通过userProfile.avatarUrl属性获取用户头像。

代码示例:

wx.getUserProfile({
  desc: '用于展示用户头像',
  success: function(res) {
    var userProfile = res.userInfo
    var avatarUrl = userProfile.avatarUrl
  }
})

问题描述

但如果我们现在再使用类似的方法时,往往无法得到自动获取用户头像昵称的效果。微信小程序开发现已不支持使用wx.getUserProfile 接口获取用户头像,同时也无法使用wx.getUserInfo 接口获取用户头像和昵称,因此在使用微信小程序获取头像昵称时,可能出现模拟器中成功但真机调试无法获取头像昵称的情况。

在这里插入图片描述

解决方案

  • 头像选择:需要将 button 组件 open-type 的值设置为 chooseAvatar,当用户选择需要使用的头像之后,可以通过 bindchooseavatar 事件回调获取到头像信息的临时路径。
 //js文件
 // 用户选择头像
 
  onChooseAvatar(e) {
 
   const {
 
    avatarUrl
 
   } = e.detail
   
   console.log(avatarUrl);
 
   this.setData({
 
 ['userInfo.avatarUrl']: avatarUrl,
 
   })
 
  },
<!-- wxml文件 -->

 <button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar">
  修改头像 
 </button>
  • 昵称填写:需要将 input 组件 type 的值设置为 nickname,当用户在此input进行输入时,键盘上方会展示微信昵称。
//js文件
 // 用户修改昵称

 changeNickName(e) {

  let name = e.detail.value;

  if (name.length === 0) return;

  this.setData({

['userInfo.nickName']: e.detail.value

  })
 },
<!-- wxml文件 -->
 <input type="nickname" class="nick-name-input" placeholder="请输入昵称" bindblur="changeNickName"/>

实现效果

通过以上代码示例,我们可以实现获取微信小程序用户的昵称和头像的功能。当用户进入小程序后,程序会请求用户授权,用户同意授权后,即可获取到用户的信息。按照上述步骤修改代码后,无法直接获取到用户的昵称和头像,但可以在用户修改默认头像和昵称时选择使用自己的昵称和头像。
在小程序中,我们可以根据用户的昵称和头像信息,实现个性化展示,比如在页面顶部显示用户头像,页面中显示用户昵称等等。这些个性化的展示方式可以提高用户的体验,增强用户对小程序的好感度。
如在WXSS文件中进行如下设置:


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

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

.avatar-wrapper{
  margin: 10px 0;
}

.nick-name-input{
  border: 1px solid #f1f1f1;
  padding:5px;
}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

此外,由于小程序的用户信息获取需要用户授权,因此我们需要在代码中加入相应的授权请求代码,保障用户隐私安全,提升小程序的信誉度。
具体的实现代码以及小程序项目完整功能源码:https://github.com/WYXNICK/The-evening-scenery-is-like-spring

  • 19
    点赞
  • 108
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
hex编码实现AES加密解密功能示例可以按照以下步骤进行: 1. 导入所需的模块: ```python import binascii from Crypto.Cipher import AES from Crypto.Random import get_random_bytes ``` 2. 定义AES加密和解密函数: ```python def encrypt(plain_text, key): cipher = AES.new(key, AES.MODE_EAX) nonce = cipher.nonce ciphertext, tag = cipher.encrypt_and_digest(plain_text.encode()) return binascii.hexlify(nonce + ciphertext + tag).decode() def decrypt(cipher_text, key): cipher_data = binascii.unhexlify(cipher_text) nonce = cipher_data[:16] ciphertext = cipher_data[16:-16] tag = cipher_data[-16:] cipher = AES.new(key, AES.MODE_EAX, nonce) decrypted_text = cipher.decrypt_and_verify(ciphertext, tag) return decrypted_text.decode() ``` 3. 生成随机密钥: ```python key = get_random_bytes(16) ``` 4. 加密和解密示例数据: ```python plain_text = "This is a sample plaintext." cipher_text = encrypt(plain_text, key) decrypted_text = decrypt(cipher_text, key) ``` 完整的代码示例如下: ```python import binascii from Crypto.Cipher import AES from Crypto.Random import get_random_bytes def encrypt(plain_text, key): cipher = AES.new(key, AES.MODE_EAX) nonce = cipher.nonce ciphertext, tag = cipher.encrypt_and_digest(plain_text.encode()) return binascii.hexlify(nonce + ciphertext + tag).decode() def decrypt(cipher_text, key): cipher_data = binascii.unhexlify(cipher_text) nonce = cipher_data[:16] ciphertext = cipher_data[16:-16] tag = cipher_data[-16:] cipher = AES.new(key, AES.MODE_EAX, nonce) decrypted_text = cipher.decrypt_and_verify(ciphertext, tag) return decrypted_text.decode() key = get_random_bytes(16) plain_text = "This is a sample plaintext." cipher_text = encrypt(plain_text, key) decrypted_text = decrypt(cipher_text, key) print("Plain text:", plain_text) print("Encrypted text:", cipher_text) print("Decrypted text:", decrypted_text) ``` 这样,就可以使用hex编码实现AES加密解密功能了。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NICKMAN-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值