微信小程序云開(kāi)發(fā) 生成帶參小程序碼流程
本文實(shí)例為大家分享了小程序生成帶參小程序碼的具體步驟,供大家參考,具體內(nèi)容如下
生成帶參小程序碼流程
1、小程序端上傳生成二維碼所需的參數(shù)到云函數(shù)
2、云函數(shù)使用appid和appsecret請(qǐng)求access_token
3、云函數(shù)使用access_token + 小程序端上傳的參數(shù)生成二維碼
4、云函數(shù)將生成的二維碼返回到小程序端(或者存到數(shù)據(jù)庫(kù)返回fileID,小程序端用fileID進(jìn)行獲取,后續(xù)生成先在數(shù)據(jù)庫(kù)查找,數(shù)據(jù)庫(kù)沒(méi)有再執(zhí)行生成操作,防止重復(fù)生成小程序碼文件)
小程序端上傳小程序碼所需的參數(shù)
wx.cloud.callFunction({
name: 'getImage', // 云函數(shù)名稱(chēng)
data: { // 小程序碼所需的參數(shù)
page: "pages/xxxx/xxxx",
id: id,
},
complete: res => {
console.log('callFunction test result: ', res)
this.setData({ // 獲取返回的小程序碼
xcxCodeImageData: res.result,
})
}
})
云函數(shù)用appid和appsecret請(qǐng)求access_token
創(chuàng)建云函數(shù)getImage,并在對(duì)應(yīng)云函數(shù)目錄中導(dǎo)入request 、request-promise、axios框架(用于數(shù)據(jù)請(qǐng)求),
npm install --save request // request框架 npm install --save request-promise // request框架promise風(fēng)格 npm install --save axios // 數(shù)據(jù)請(qǐng)求框架,可將返回的數(shù)據(jù)類(lèi)型設(shè)置為流`stream` # 備注:install 可以簡(jiǎn)寫(xiě)為 i ;save 作用是將這個(gè)庫(kù)添加到package.json里面
云函數(shù)文件中導(dǎo)入框架
const cloud = require('wx-server-sdk')
const axios = require('axios')
var rp = require('request-promise');
const fs = require('fs');
var stream = require('stream');
# 不需要全部導(dǎo)入,根據(jù)實(shí)際下面實(shí)際使用情況酌情導(dǎo)入
請(qǐng)求獲取 access_token
// request框架promise風(fēng)格
rp('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=secret=appSecret'
.then(function(resultValue) {
console.log("請(qǐng)求 success:")
console.log(JSON.parse(resultValue))
})
.catch(function(err) {});
});
// Nodejs原生寫(xiě)法
const http = require("https")
const url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=secret=appSecret"
http.get(url,(res)=>{
var resultValue = ""
res.on("data",(data)=>{
resultValue+=data
})
res.on("end",()=>{
console.log(resultValue)
})
}).on("error",(e)=>{
console.log(`獲取數(shù)據(jù)失敗: ${e.message}`)
})
獲取小程序碼
var options = {
method: 'POST',
url: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + access_token',
body: {
page: "pages/xxx/xxx
scene: "id=xxx"
},
json: true
};
rp(options)
.then(function(parsedBody) {
console.log(parsedBody) //小程序碼圖片數(shù)據(jù)
})
.catch(function(err) {});
服務(wù)端完整代碼一
var rp = require('request-promise');
const fs = require('fs');
var stream = require('stream');
// 請(qǐng)求微信access_token
rp('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret')
.then(function(resultValue) {
console.log("請(qǐng)求 success:" + resultValue)
console.log(JSON.parse(resultValue).access_token)
// 請(qǐng)求小程序碼
var http = require("http"),
data = {
// 小程序碼參數(shù)
"page": "pages/CardDetail/CardDetail",
"width": 300,
"scene": "id=W6MIjlJhFW5Pec-Y",
};
data = JSON.stringify(data);
var options = {
method: "POST",
host: "api.weixin.qq.com",
path: "/wxa/getwxacodeunlimit?access_token=" + JSON.parse(resultValue).access_token,
headers: {
"Content-Type": "application/json",
"Content-Length": data.length
}
};
var req = http.request(options, function (res) {
res.setEncoding("binary");
var imgData = '';
res.on('data', function (chunk) {
imgData += chunk;
});
res.on("end", function () {
// 將返回的圖片數(shù)據(jù)轉(zhuǎn)化成uploadFile方法fileContent參數(shù)所需的文件流形式,且本地輸出數(shù)據(jù)正常,可以試著用此方法執(zhí)行uploadFile進(jìn)行獲取小程序碼,作者采用了方法二
var bufferStream = new stream.PassThrough();
bufferStream.end(new Buffer(imgData));
console.log('uploadFile方法fileContent參數(shù)所需的文件流----')
console.log(bufferStream)
// Sublime Text可以運(yùn)行輸出到本地,且可以打開(kāi)二維碼
// 本地存放路徑
var path = 'public/'+ Date.now() +'.png';
fs.writeFile(path, imgData, "binary", function (err) {
if (err) {
console.log("down fail");
}
console.log("down success");
});
});
});
req.write(data);
req.end();
})
.catch(function(err) {});
服務(wù)端完整代碼二(可直接粘貼使用)
const cloud = require('wx-server-sdk')
const axios = require('axios')
var rp = require('request-promise');
cloud.init()
// 云函數(shù)入口函數(shù)
exports.main = async (event, context) => {
console.log(event)
try {
const resultValue = await rp('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret')
const token = JSON.parse(resultValue).access_token;
console.log('------ TOKEN:', token);
const response = await axios({
method: 'post',
url: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit',
responseType: 'stream',
params: {
access_token: token,
},
data: {
page: event.page,
width: 300,
scene: "id=" + event.id,
},
});
return await cloud.uploadFile({
cloudPath: 'xcxcodeimages/' + Date.now() + '.png',
fileContent: response.data,
});
} catch (err) {
console.log('>>>>>> ERROR:', err)
}
}
點(diǎn)擊查看:request框架相關(guān)文檔
點(diǎn)擊查看:request框架promise風(fēng)格相關(guān)文檔
點(diǎn)擊查看:axios框架相關(guān)文檔
點(diǎn)擊查看:小程序云開(kāi)發(fā)文檔
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS輸出空格的簡(jiǎn)單實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇JS輸出空格的簡(jiǎn)單實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09
IE8 引入跨站數(shù)據(jù)獲取功能說(shuō)明
今天看了一下msdn文檔,發(fā)現(xiàn)IE8打算增加 XDomainRequest (http://msdn.microsoft.com/en-us/library/cc288060(VS.85).aspx) 跨站數(shù)據(jù)獲取的接口2008-07-07
Bootstrap路徑導(dǎo)航與分頁(yè)學(xué)習(xí)使用
這篇文章主要為大家詳細(xì)介紹了Bootstrap路徑導(dǎo)航與分頁(yè)學(xué)習(xí)使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
超級(jí)簡(jiǎn)易的JS計(jì)算器實(shí)例講解(實(shí)現(xiàn)加減乘除)
下面小編就為大家?guī)?lái)一篇超級(jí)簡(jiǎn)易的JS計(jì)算器實(shí)例講解(實(shí)現(xiàn)加減乘除)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
js中自定義方法實(shí)現(xiàn)停留幾秒sleep
js中不存在自帶的sleep方法,要想休眠要自己定義個(gè)方法,需要的朋友可以參考下2014-07-07
使用coffeescript編寫(xiě)node.js項(xiàng)目的方法匯總
Node.js 基于JavaScript編寫(xiě)應(yīng)用,JavaScript是我的主要開(kāi)發(fā)語(yǔ)言。CoffeeScript是編譯為JavaScript的編程語(yǔ)言。CoffeeScript是一個(gè)非常高階的語(yǔ)言,將JavaScript、Ruby和Python中我最?lèi)?ài)的部分結(jié)合在了一起。小編給大家介紹下使用coffeescript編寫(xiě)node.js項(xiàng)目的方法2015-08-08
微信小程序?qū)崿F(xiàn)tab頁(yè)面切換功能
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)tab頁(yè)面切換功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
JavaScript面試必備技巧之手寫(xiě)一個(gè)Promise
很多同學(xué)在面試的時(shí)候都會(huì)被要求手寫(xiě)一個(gè)Promise,那么今天我總結(jié)了一些手寫(xiě)Promise的方法,可以跟著我的思路一起來(lái)實(shí)現(xiàn)一個(gè)Promise,讓我們的面試更有把握2023-02-02

