微信小程序云開發(fā) 生成帶參小程序碼流程
本文實(shí)例為大家分享了小程序生成帶參小程序碼的具體步驟,供大家參考,具體內(nèi)容如下
生成帶參小程序碼流程
1、小程序端上傳生成二維碼所需的參數(shù)到云函數(shù)
2、云函數(shù)使用appid和appsecret請求access_token
3、云函數(shù)使用access_token + 小程序端上傳的參數(shù)生成二維碼
4、云函數(shù)將生成的二維碼返回到小程序端(或者存到數(shù)據(jù)庫返回fileID,小程序端用fileID進(jìn)行獲取,后續(xù)生成先在數(shù)據(jù)庫查找,數(shù)據(jù)庫沒有再執(zhí)行生成操作,防止重復(fù)生成小程序碼文件)
小程序端上傳小程序碼所需的參數(shù)
wx.cloud.callFunction({
name: 'getImage', // 云函數(shù)名稱
data: { // 小程序碼所需的參數(shù)
page: "pages/xxxx/xxxx",
id: id,
},
complete: res => {
console.log('callFunction test result: ', res)
this.setData({ // 獲取返回的小程序碼
xcxCodeImageData: res.result,
})
}
})
云函數(shù)用appid和appsecret請求access_token
創(chuàng)建云函數(shù)getImage,并在對應(yīng)云函數(shù)目錄中導(dǎo)入request 、request-promise、axios框架(用于數(shù)據(jù)請求),
npm install --save request // request框架 npm install --save request-promise // request框架promise風(fēng)格 npm install --save axios // 數(shù)據(jù)請求框架,可將返回的數(shù)據(jù)類型設(shè)置為流`stream` # 備注:install 可以簡寫為 i ;save 作用是將這個庫添加到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)入
請求獲取 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("請求 success:")
console.log(JSON.parse(resultValue))
})
.catch(function(err) {});
});
// Nodejs原生寫法
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');
// 請求微信access_token
rp('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret')
.then(function(resultValue) {
console.log("請求 success:" + resultValue)
console.log(JSON.parse(resultValue).access_token)
// 請求小程序碼
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)行輸出到本地,且可以打開二維碼
// 本地存放路徑
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)擊查看:小程序云開發(fā)文檔
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
談?wù)凧avaScript類型系統(tǒng)之Math
Math 對象并不像 Date 和 String 那樣是對象的類,因此沒有構(gòu)造函數(shù) Math(),像 Math.sin() 這樣的函數(shù)只是函數(shù),不是某個對象的方法。您無需創(chuàng)建它,通過把 Math 作為對象使用就可以調(diào)用其所有屬性和方法2016-01-01
antd項(xiàng)目實(shí)現(xiàn)彩蛋效果的詳細(xì)代碼
這篇文章主要介紹了antd項(xiàng)目如何實(shí)現(xiàn)彩蛋效果,首先在components目錄下創(chuàng)建Transform目錄,包括index.css、index.js,index.js是主要的邏輯代碼,下面對代碼進(jìn)行分析,需要的朋友可以參考下2022-09-09
基于MVC方式實(shí)現(xiàn)三級聯(lián)動(JavaScript)
這篇文章主要為大家詳細(xì)介紹了基于MVC方式實(shí)現(xiàn)三級聯(lián)動的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
JavaScript實(shí)現(xiàn)時間表動態(tài)效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)時間表動態(tài)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
JavaScript 網(wǎng)頁中實(shí)現(xiàn)一個計(jì)算當(dāng)年還剩多少時間的倒數(shù)計(jì)時程序
這篇文章主要介紹了JavaScript 網(wǎng)頁中實(shí)現(xiàn)一個計(jì)算當(dāng)年還剩多少時間的倒數(shù)計(jì)時程序,需要的朋友可以參考下2017-01-01

