如何實現微信小程序換頭像?三步幫你搞定!

背景

2019年10月1日在祖國70週年大慶之際,微信朋友圈被你們紛紛@微信官方要國旗的景象給刷屏了,在爲祖國慶生的同時,玩的不亦樂乎。前端

在9月25日,我心血來潮決定在國慶前開發一個換頭像的微信小程序爲祖國70週年獻禮!最終在國慶前夕上線發佈了製做頭像的小程序-【海豚趣圖】,幫助10000多名小夥伴成功換上了國旗頭像。git

小程序雖然已經上線,但在【海豚趣圖】這個小程序開發過程當中的一些有意思的技術點我但願能總結一下和你們進行分享。github

主要步驟

  • 獲取用戶頭像
  • 圖片模板
  • 圖片合成

獲取用戶頭像

製做自定義頭像的第一步就是先選擇圖片。在【海豚趣圖】的交互設計中,用戶有三種選擇圖片的方式:微信頭像、本地相冊和相機拍攝。獲取用戶頭像的產品設計以下圖所示:數據庫

因爲微信官方再也不支持經過 wx.getUserInfo 接口來獲取用戶信息,咱們必須經過使用 button 組件並將 open-type 指定爲 getUserInfo 類型來獲取或展現用戶信息。npm

爲優化用戶體驗,使用 wx.getUserInfo 接口直接彈出受權框的開發方式將逐步再也不支持。從2018年4月30日開始,小程序與小遊戲的體驗版、開發版調用 wx.getUserInfo 接口,將沒法彈出受權詢問框,默認調用失敗。正式版暫不受影響。json

上圖中彈出底部菜單的交互方式沒法經過 wx.showActionSheet 來實現(由於該接口只能指定字符串文本,不能使用 button, navigator 等組件)。小程序

所以,只能經過自定義 actionSheet 組件來實現以上功能。微信小程序

mmp-action-sheet 組件

如下是 mmp-action-sheet 組件的代碼。promise

index.wxml

<view hidden="{{!actionShow}}" class="mask {{mask}}" bindtap="actionHide">
  <view class="actionSheet animated {{animation}}">
    <slot></slot>
    <button class="close" bindtap="actionHide">{{closeText}}</button>
  </view>
</view>
複製代碼

經過 slot 在 action-sheet 中插入自定義的內容,好比 buttonnavigator 等。bash

index.wxss

.mask{
  position: fixed;
  top: 0;
  left: 0;
  width:100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 999;
}
.actionSheet{
  width: 100%;
  position: absolute;
  top: 100%;
  z-index: 1000;
  overflow: hidden;
}
.actionSheet button,
.actionSheet navigator{
  color: #000;
  text-align: center;
  background: #fff;
  border-radius: 0;
  line-height: 3.5;
  font-size: 32rpx;
  border-bottom: 1rpx solid rgb(236, 236, 236);
  opacity: 1;
}
.actionSheet button:active,
.actionSheet navigator:active{
  color:#000;
  background: rgb(236, 236, 236);
}
.actionSheet button::after,
.actionSheet navigator::after
{
  border: none;
  border-radius: 0;
}
.actionSheet .close{
  border-bottom: none;
  border-bottom: 50rpx solid #fff;
  border-top: 16rpx solid rgb(236, 236, 236);
}
.animated {
  animation-timing-function: ease-out;
  animation-duration: 0.2s;
  animation-fill-mode: both;
}


@keyframes fadeInBottom {
from{
   transform: translate3d(0, 0, 0);
 }
  to {
    transform: translate3d(0, -100%, 0);
  }
}
.fadeInBottom {
  animation-name: fadeInBottom;
}

@keyframes fadeOutBottom {
from{
   transform: translate3d(0, -100%, 0);
 }
  to {
    transform: translate3d(0, 0, 0);
  }
}
.fadeOutBottom {
  animation-name: fadeOutBottom;
}

@keyframes fadeIn {
from{
   opacity: 0;
 }
  to {
     opacity: 1;
  }
}
.fadeIn {
  animation-name: fadeIn;
}

@keyframes fadeOut {
from{
   opacity: 1;
 }
  to {
     opacity: 0;
  }
}
.fadeOut {
  animation-name: fadeOut;
}
複製代碼

index.js

Component({
  properties: {
    actionSheetStatus: {
      type: Boolean,
      value: false,
      observer(newVal) {
        if (newVal) {
          this.setData({
            actionSheetStatus: true,
            animationMask: 'fadeIn',
            animationSheet: 'fadeInBottom'
          })
        } else {
          this.setData({
            actionSheetStatus: false,
            animationMask: 'fadeOut',
            animationSheet: 'fadeOutBottom'
          })
        }
      }
    },
    closeText: {
      type: String,
      value: '取消'
    }
  },

  data: {
    animationMask: 'fadeIn',
    animationSheet: 'fadeInBottom'
  },

  methods: {
    closeActionSheet() {
      this.setData({
        animationMask: 'fadeOut',
        animationSheet: 'fadeOutBottom'
      })
      setTimeout(() => {
        this.setData({actionSheetStatus: false})
      }, 300)
    }
  }
})
複製代碼

組件只有兩個參數:

  • actionSheetStatus 指定組件的初始展現狀態,默認爲false,表示不顯示組件。
  • closeText 指定關閉按鈕的名字,默認爲 取消

index.json

{
  "component": true,
  "usingComponents": {}
}
複製代碼

接下來在頁面中調用組件,在組件中插入了3個 button 組件來實現來獲取用戶頭像:

<action-sheet actionSheetStatus="{{actionSheetStatus}}">
    <button open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">使用微信頭像</button>
    <button bindtap="pickPic" data-source-type="album">使用本地相冊</button>
    <button bindtap="pickPic" data-source-type="camera">拍照</button>
  </action-sheet>
複製代碼

以上咱們經過自定義組件 mmp-action-sheet 就解決了原生的 actionsheet 沒法指定 button,從而沒法獲取用戶微信頭像的問題。

該組件我已經發布到 npm 包,須要用到的同窗能夠經過 npm 安裝,也能夠在 github 上查看源碼和使用文檔。

圖片模板

有了原圖,接下來咱們須要選擇圖片模板。若是模板數量很少或者模板變化不頻繁,咱們能夠直接把模板放在本地。鑑於我提供的模板比較多,放在本地會增大小程序源碼的大小,我把模板上傳到了小程序的雲存儲中,經過雲函數來動態獲取圖片模板,方便之後模板擴展。

雲函數 tpl 的代碼以下:

// 雲函數入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 雲函數入口函數
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  // 1. 獲取數據庫引用
  const db = cloud.database()
  const MAX_LIMIT = 100
  // 2. 構造查詢語句
  const countResult = await db.collection('template').count()
  const total = countResult.total
  // 計算需分幾回取
  const batchTimes = Math.ceil(total / 100)
  const tasks = []
  for (let i = 0; i < batchTimes; i++) {
    const promise = db.collection('template').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
    tasks.push(promise)
  }

  return (await Promise.all(tasks)).reduce((acc, cur) => {
    return {
      data: acc.data.concat(cur.data),
      errMsg: acc.errMsg,
    }
  })
}
複製代碼

頁面中調用雲函數拉取模板:

getTpl() {
    const self = this
    // 調用雲函數獲取圖片模板
    wx.cloud.callFunction({
      name: 'tpl'
    }).then(res => {
      self.setData({
        templates: res.result.data
      })
    })
}
複製代碼

問題

到這裏模板的獲取邏輯已經沒有問題了,但在開發過程當中遇到了一個問題。模板圖片的連接我使用的是雲文件ID,當有大量圖片並行加載的時候,只有部分圖片可以顯示,我看了一下dom節點其實都已經存在了,image的src的地址也都是正確的。

微信官方自2.3.0開始已經支持在image中使用雲文件ID。雲文件ID的格式爲: cloud://xxx.xxx/templates/01.png

我猜想多是對微信雲存儲併發請求過多致使的(有知道的同窗能夠告知),由於我試了一下將雲文件ID換成正常的HTTPS的連接是沒問題的。

由此可知,能夠想到有三種可行的解決方案:

  1. 將圖片模板存儲到外部OSS,使用https協議的連接。
  2. 使用 wx.getTempFileURL 用雲文件 ID 換取真實連接,也就是https形式的連接。
  3. 控制圖的並行加載數量。個人實踐是將並行加載數量控制在20,當用戶滾動的時候再發起下一次請求。

總結

以上主要分享了用戶頭像獲取以及圖片模板功能的實現及所遇到的一些問題。關於圖片的合成是整個小程序的核心功能,下一篇單獨拉出來講一下實現思路和潛在的問題。敬請期待!

掃碼體驗

讀者能夠掃碼體驗咱們所講述的兩個功能點:

  1. 自定義actionsheet組件
  2. 圖片模板加載

關於咱們

快狗打車前端團隊專一前端技術分享,按期推送高質量文章,歡迎關注點贊。 文章同步發佈在公衆號喲,想要第一時間獲得最新的資訊,just scan it !

公衆號二維碼
相關文章
相關標籤/搜索