溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

微信小程序云開發(fā)如何使用云函數(shù)生成二維碼

發(fā)布時(shí)間:2020-08-19 14:05:54 來源:腳本之家 閱讀:177 作者:潘高 欄目:web開發(fā)

本文實(shí)例為大家分享了微信小程序云開發(fā)使用云函數(shù)生成二維碼的具體代碼,供大家參考,具體內(nèi)容如下

首先,需要給對應(yīng)的云函數(shù)安裝 request-promise 依賴。(不會(huì)給云函數(shù)安裝依賴的盆友請移步 微信小程序中的云開發(fā)如何使用npm安裝依賴)

生成二維碼的云函數(shù)如下:

// 云函數(shù)入口文件
const cloud = require('wx-server-sdk')
const rp = require('request-promise')

cloud.init()

// 云函數(shù)入口函數(shù)
exports.main = async (event, context) => {

 const page = event.page
 const scene = event.scene

 //appid和秘鑰
 const appid = '***',
 secret = '***';

 const AccessToken_options = {
 method: 'GET',
 url: 'https://api.weixin.qq.com/cgi-bin/token',
 qs: {
 appid,
 secret,
 grant_type: 'client_credential'
 },
 json: true

 };

 //獲取AccessToken
 const resultValue = await rp(AccessToken_options);
 const token = resultValue.access_token;

 //獲取小程序碼配置
 const code_options = {
 method: 'POST',
 url: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + token,
 body: {
 'page': page,
 'width': 430,
 'scene': scene
 },
 json: true,
 encoding: null
 };

 //獲取二進(jìn)制圖片
 const buffer = await rp(code_options);

 const upload = await cloud.uploadFile({
 cloudPath: 'wxacode.png',
 fileContent: buffer,
 })
 return {
 wxacodefileID: upload.fileID
 }

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI