国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 熱點 > 微信 > 正文

微信小程序云開發 生成帶參小程序碼流程

2024-07-22 01:18:25
字體:
來源:轉載
供稿:網友

本文實例為大家分享了小程序生成帶參小程序碼的具體步驟,供大家參考,具體內容如下

生成帶參小程序碼流程

1、小程序端上傳生成二維碼所需的參數到云函數
2、云函數使用appid和appsecret請求access_token
3、云函數使用access_token + 小程序端上傳的參數生成二維碼
4、云函數將生成的二維碼返回到小程序端(或者存到數據庫返回fileID,小程序端用fileID進行獲取,后續生成先在數據庫查找,數據庫沒有再執行生成操作,防止重復生成小程序碼文件)

小程序端上傳小程序碼所需的參數

wx.cloud.callFunction({ name: 'getImage', // 云函數名稱 data: { // 小程序碼所需的參數 page: "pages/xxxx/xxxx", id: id, }, complete: res => { console.log('callFunction test result: ', res) this.setData({ // 獲取返回的小程序碼 xcxCodeImageData: res.result, }) }})

云函數用appid和appsecret請求access_token

創建云函數getImage,并在對應云函數目錄中導入request 、request-promise、axios框架(用于數據請求),

npm install --save request // request框架npm install --save request-promise // request框架promise風格npm install --save axios // 數據請求框架,可將返回的數據類型設置為流`stream`# 備注:install 可以簡寫為 i ;save 作用是將這個庫添加到package.json里面

云函數文件中導入框架

const cloud = require('wx-server-sdk')const axios = require('axios')var rp = require('request-promise');const fs = require('fs');var stream = require('stream');# 不需要全部導入,根據實際下面實際使用情況酌情導入

請求獲取 access_token

// request框架promise風格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(`獲取數據失敗: ${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) //小程序碼圖片數據 }) .catch(function(err) {});

服務端完整代碼一

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 = { // 小程序碼參數 "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 () {  // 將返回的圖片數據轉化成uploadFile方法fileContent參數所需的文件流形式,且本地輸出數據正常,可以試著用此方法執行uploadFile進行獲取小程序碼,作者采用了方法二 var bufferStream = new stream.PassThrough(); bufferStream.end(new Buffer(imgData)); console.log('uploadFile方法fileContent參數所需的文件流----') console.log(bufferStream)  // Sublime Text可以運行輸出到本地,且可以打開二維碼 // 本地存放路徑 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) {});            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙河市| 新和县| 台北市| 张家界市| 延川县| 安义县| 望谟县| 嘉鱼县| 苍溪县| 油尖旺区| 曲麻莱县| 义乌市| 武宁县| 井陉县| 邯郸县| 赞皇县| 老河口市| 普定县| 泰兴市| 哈尔滨市| 澜沧| 铜鼓县| 房山区| 临沧市| 望奎县| 武穴市| 梅河口市| 荥经县| 驻马店市| 绥德县| 双辽市| 漳浦县| 临沭县| 凉城县| 普洱| 莆田市| 宁阳县| 禄丰县| 江孜县| 黑山县| 宜兴市|