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

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

逐梦个人博客

使用原生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