需求
分享活動,截圖保存到相冊,圖片上包含小程序碼,掃碼可進(jìn)入該活動詳情
實現(xiàn)
使用小程序云函數(shù)動態(tài)生成小程序碼:
- scene: 掃碼進(jìn)入頁面需要的參數(shù)
- page: 掃碼進(jìn)入的頁面,必須是已經(jīng)發(fā)布的小程序存在的頁面(否則報錯)
- width: 生成碼的寬度
- is_hyaline: 是否需要透明底色
// 云函數(shù)入口函數(shù)
exports.main = async (event, context) => {
try {
const result = await cloud.openapi.wxacode.getUnlimited({
scene: 'id=' + event.id,
page: 'pages/activityInfo/activityInfo',
width: 280,
isHyaline: true,
})
return result
} catch (err) {
return err
}
}
其余參數(shù)參考文檔鏈接:云函數(shù)生成小程序碼
注意事項
- page傳參問題:page傳入的參數(shù)必須是已發(fā)布的小程序存在的頁面,初次發(fā)布或舊版本中沒有的頁面,生成會報錯??梢韵炔粋鱬age參數(shù),測試云函數(shù)和顯示是否正常,上線前加上參數(shù)即可。
- scene傳參問題:scene傳參在頁面上獲取參數(shù)的方式為:
比如在云函數(shù)中傳入的scene為id=1,那在onLoad中獲取scene,options.scene為id%3D1,decodeURIComponent(options.scene)為id=1,所以想要獲取1就要自己處理一下了。
也可以直接設(shè)置scene傳入id,不拼接字符串,這種適合只有一個參數(shù)的情況。
onLoad: function (options) {
let scene = options.scene;
let decodeScene = decodeURIComponent(options.scene)
},