如何通过微信小程序云开发生成带参数的小程序码?
- 内容介绍
- 文章标签
- 相关推荐
本文共计909个文字,预计阅读时间需要4分钟。
原文:本文实例为大家分享小程序生成带参小程序码的具体步骤,并提供大家参考,具体内容如下:1、生成带参小程序码流程;2、小程序端上传生成二维码所需参数到云函数;3、云函数使用appid和密钥生成带参小程序码。
改写后:本文展示了如何生成带参数的小程序码,包括生成流程、上传参数到云函数以及使用appid生成带参小程序码等步骤。
本文实例为大家分享了小程序生成带参小程序码的具体步骤,供大家参考,具体内容如下
生成带参小程序码流程
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('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 api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=secret=appSecret" 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('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 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: '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) } }
点击查看:request框架相关文档
点击查看:request框架promise风格相关文档
点击查看:axios框架相关文档
点击查看:小程序云开发文档
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计909个文字,预计阅读时间需要4分钟。
原文:本文实例为大家分享小程序生成带参小程序码的具体步骤,并提供大家参考,具体内容如下:1、生成带参小程序码流程;2、小程序端上传生成二维码所需参数到云函数;3、云函数使用appid和密钥生成带参小程序码。
改写后:本文展示了如何生成带参数的小程序码,包括生成流程、上传参数到云函数以及使用appid生成带参小程序码等步骤。
本文实例为大家分享了小程序生成带参小程序码的具体步骤,供大家参考,具体内容如下
生成带参小程序码流程
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('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 api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=secret=appSecret" 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('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 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: '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) } }
点击查看:request框架相关文档
点击查看:request框架promise风格相关文档
点击查看:axios框架相关文档
点击查看:小程序云开发文档
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

