微信小程序云開(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ù)名稱 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)文章
談?wù)凧avaScript類(lèi)型系統(tǒng)之Math
Math 對(duì)象并不像 Date 和 String 那樣是對(duì)象的類(lèi),因此沒(méi)有構(gòu)造函數(shù) Math(),像 Math.sin() 這樣的函數(shù)只是函數(shù),不是某個(gè)對(duì)象的方法。您無(wú)需創(chuàng)建它,通過(guò)把 Math 作為對(duì)象使用就可以調(diào)用其所有屬性和方法2016-01-01antd項(xiàng)目實(shí)現(xiàn)彩蛋效果的詳細(xì)代碼
這篇文章主要介紹了antd項(xiàng)目如何實(shí)現(xiàn)彩蛋效果,首先在components目錄下創(chuàng)建Transform目錄,包括index.css、index.js,index.js是主要的邏輯代碼,下面對(duì)代碼進(jìn)行分析,需要的朋友可以參考下2022-09-09基于MVC方式實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)(JavaScript)
這篇文章主要為大家詳細(xì)介紹了基于MVC方式實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01淺談JavaScript中的String對(duì)象常用方法
這篇文章主要介紹了JavaScript中的String對(duì)象常用方法,非常簡(jiǎn)單實(shí)用,有需要的小伙伴參考下2015-02-02JavaScript實(shí)現(xiàn)時(shí)間表動(dòng)態(tài)效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)時(shí)間表動(dòng)態(tài)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07JavaScript 網(wǎng)頁(yè)中實(shí)現(xiàn)一個(gè)計(jì)算當(dāng)年還剩多少時(shí)間的倒數(shù)計(jì)時(shí)程序
這篇文章主要介紹了JavaScript 網(wǎng)頁(yè)中實(shí)現(xiàn)一個(gè)計(jì)算當(dāng)年還剩多少時(shí)間的倒數(shù)計(jì)時(shí)程序,需要的朋友可以參考下2017-01-0115個(gè)非常實(shí)用的JavaScript代碼片段
這篇文章主要為大家詳細(xì)介紹了15個(gè)非常實(shí)用的JavaScript代碼片段,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12