uniapp定制小程序碼+海報(bào)

需求

1.生成微信小程序自定義參數(shù)二維碼
2.將二維碼拼接背景圖生成活動海報(bào)保存至相冊


效果圖

文末已發(fā)布代碼,有需要請自行獲取,如若有問題歡迎交流

開發(fā)環(huán)境

服務(wù)端:php
客戶端:uniapp
應(yīng)用場景:微信小程序

問題

1.怎么生成微信小程序二維碼?
2.怎么生成活動海報(bào)?

生成小程序二維碼-服務(wù)端

第一步 獲取微信小程序 access_token

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=?&secret=?

請求方式:POST
請求參數(shù):
appid 微信小程序id
secret 微信小程序密鑰
請求返回:
buffer 小程序二維碼二維數(shù)組
微信官方文檔.小程序-獲取AccessToken

第二步 獲取二維碼 Buffer

https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

請求方式:POST
請求參數(shù):
scene 二維碼參數(shù)
page 二維碼打開路徑
請求返回:
buffer 小程序二維碼二維數(shù)組
微信官方文檔.小程序-獲取二維碼

第三步 將buffer轉(zhuǎn)換為base64字節(jié)碼

微信小程序無法直接將buffer保存到圖片,所以最好將其轉(zhuǎn)換為base64字節(jié)碼,通過轉(zhuǎn)換為base64字節(jié)碼寫入到畫布,通過保存畫布到相冊,即可實(shí)現(xiàn)保存海報(bào)

客戶端

創(chuàng)建活動海報(bào)

UI代碼

<template>
    <view class="container">
        <view class="contents">
            <canvas class="page-content" canvas-id="shareCanvas" style="width:100%;" :style="{ height: height + 'px' }"></canvas>
        </view>
        <view class="page-bottom flex text-center">
            <button class="cu-btn block bg-orange" @tap="selectImage">選擇圖片</button>
            <button class="cu-btn block bg-orange" @tap="saveImage">保存圖庫</button>
        </view>
    </view>
</template>
獲取二維碼
            const url ="服務(wù)器接口地址";
            //加載頁面參數(shù)
            let  scene = 'id=' + this.id + '&token=' + this.token + '&type=1';
            let params={
              page: 'pages/index/index',
              scene: scene
            }
            uni.request({
                url: url, 
                data: params,
                method: "POST",
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                dataType: 'arraybuffer',//請求字節(jié)
                success: (res) => {
                    //獲取base64 字符串
                    var data=res.data;
                    //拼裝base64圖片
                    var img='data:image/png;base64,'+data;
                    //調(diào)起繪制器
                    this.initImage(img);
                }
            });

*注意事項(xiàng):
1.由于從微信官方請求下來的圖片字節(jié)碼不帶base64數(shù)據(jù)頭,需要自行拼接,如:

var img='data:image/png;base64,'+data;

2.由于全部為字節(jié)碼建議dataType設(shè)置為arraybuffer,這樣可以避免請求亂碼

繪制畫布
uni.getSystemInfo({
                    success: function(e) {
                        console.log(e);
                        //獲取屏幕寬高設(shè)置畫布寬高
                        let width = e.windowWidth;
                        let height= e.screenHeight;
                        that.height=height;
                        let topheight = 320;//圖片距離上邊距離
                        const ctx = uni.createCanvasContext('shareCanvas');
                        // 底圖
                        ctx.drawImage(res[0].path, 0, 0, width, height);
                        //繪制序列從上到下, 通過距離上邊高度來排列顯示
                        // 文字內(nèi)容
                        ctx.setTextAlign('center'); // 文字居中
                        ctx.setFillStyle('#F0AD4E'); // 文字顏色:黑色
                        ctx.setFontSize(22); // 文字字號:22px
                        ctx.fillText('雙十一活動', width / 2, topheight-30);
                        ctx.setFillStyle('#FFFFFF'); 
                            ctx.setFontSize(20); 
                        ctx.fillText('掃碼領(lǐng)取更多獎', width / 2, topheight);
                        // 小程序碼
                        const qrImgSize = 140;
                        ctx.drawImage(res[1], (width - qrImgSize) / 2, topheight+15 , qrImgSize, qrImgSize);
                        ctx.stroke();
                        ctx.draw();
                    }
                });

*注意事項(xiàng):
1.畫布組件顯示內(nèi)容為圖層,所以為了鋪墊元素,需要注意從上到下元素的間距。

保存到相冊

const wxCanvasToTempFilePath = tools.promisify(uni.canvasToTempFilePath);
            const wxSaveImageToPhotosAlbum = tools.promisify(uni.saveImageToPhotosAlbum);
            wxCanvasToTempFilePath(
                {
                    canvasId: 'shareCanvas'
                },
                this
            ).then(res => {
                    return wxSaveImageToPhotosAlbum({
                        filePath: res.tempFilePath
                    });
                })
                .then(res => {
                    wx.showToast({
                        title: '已保存到相冊'
                    });
                });

項(xiàng)目源碼

https://github.com/sunql0827/createposter.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容