惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

逐梦个人博客

使用原生JS监听浏览器的前进后退事件-逐梦个人博客 如何在WebWorker中使用psd.js-逐梦个人博客 如何根据权重裁剪FaceApi.js返回的人脸数据-逐梦个人博客 在WebWorker中使用FaceAPI.js的正确姿势-逐梦个人博客 解锁React组件函数this绑定新方式-逐梦个人博客 React通用带上下文的组件ID实现方案-逐梦个人博客 关于照片Exif Orientation信息的处理-逐梦个人博客 防止其他网站通过iframe嵌套自己站点的两种方式-逐梦个人博客 警惕!wordpress站长必知的重大安全漏洞合集-逐梦个人博客
微信小程序中使用微信头像的实现方式-逐梦个人博客
Dean · 2023-10-18 · via 逐梦个人博客

最近在很多与微信头像处理相关的微信小程序中,看到了这么一个小功能,点击按钮即可直接使用当前用户的微信头像:

这类小程序主要是对微信头像做处理,比如添加一些装饰之类的,大部分用户都会选择使用当前的微信头像,如果有一个按钮可以直接获取当前用户的微信头像会显得十分的贴心。

那么这个功能是如何实现的呢?

首先我们在wxml文件中放置Button组件并将open-type设置为chooseavatar,在chooseavatar事件的处理函数中接收获取的微信头像地址,代码如下:

wxml:

<button open-type="chooseAvatar" type="primary" bind:chooseavatar="onChooseAvatar">微信头像</button>

处理函数:

...,
onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    console.log(avatarUrl);
},
...

一切准备就绪,当点击我们上面定义的微信头像Button,就会弹出图1所示的ActionSheet,选择“用微信头像”,即可打印出微信头像的地址。这里我们留意到除了“用微信头像”之外,还有3个选项,除去“取消”外还有“从相册选择”和“拍照”,我们知道“用微信头像”返回的一定是满足微信头像尺寸要求的图片,但是通过“从相册选择”和“拍照”得到的图片就不一定,对于这2种情况获得的图片,我们需要进行一定的裁剪,但是处理函数是统一的,我们要怎么将它们与“用微信头像”区分开呢?其实也很简单,微信头像肯定是方形的,即宽=高,我们可以获取我们图片的信息,根据宽高情况,判定是不是需要裁剪,代码如下:

...,
onChooseAvatar(e) {
    const { avatarUrl } = e.detail;
    wx.getImageInfo({
        src: avatarUrl,
        success: (res) => {
          const {
              width,
              height
          } = res;
          if (width === height) {
              this.setData({
                avatarUrl,
              });
          } else {
              wx.navigateTo({
                url: `/pages/copper/index?url=${avatarUrl}`,
              });
          }
        },
        fail: (err) => {
          console.log(err)
        }
    });
},
...

注意:该功能从基础库2.21.2开始支持

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