微信小程序中开放能力(一):获取微信头像

624 阅读1分钟

1.将button组件open-type属性设置为chooseAvatar

<button open-type="chooseAvatar">获取头像</button>

2.通过bindchooseavatar事件回调获取头像信息的临时路径

  • event.detail.avatarUrl是微信头像的url。
  • 用event获取的微信头像是临时路径,临时路径是有时效时间的,在实际开发中要将图片上传至服务器。
<button open-type="chooseAvatar" bindchooseavatar="bindchooseavatar">获取头像</button>
  bindchooseavatar(event) {
    const url = event.detail.avatarUrl;
    this.setData({avatarUrl: url,});
    console.log(event, event.detail.avatarUrl);
  },