uni-app 微信小程序 生成二维码链接 动态参数

文档地址

「auth.getAccessToken」
 
「wxacode.getUnlimited」
 
「第三方教程」
 

实战步骤

第一步:获取 access_token
 
GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}

 

第二步:获取小程序码(永久有效,数量暂无限制)
 
POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

 

示例代码

getWxCode() {
    // 业务参数(任务id + 企业id)—— 这两个参数应该由前端传给后端
    let scene= 'taskId=7lG9H99UW&companyId=100316100'

    // loading
    uni.showLoading({ title: '加载中', mask: true })

    // 请求微信接口(这一步应该让后端来做,因为后端顺便需要存储二维码图片。)
    uni.request({
        url: `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${ACCESS_TOKEN}`,
        method: "POST",
        // 数据格式为arraybuffer
        responseType: 'arraybuffer',
        data: {
            // 业务参数(任务id + 企业id)
            scene: scene,
            // 小程序的页面
            page: 'pages/index/index'
        },
        success: function(res) {
            uni.hideLoading()
            let src = wx.arrayBufferToBase64(res.data)
            const base64 = 'data:image/png;base64,' + src
            console.log(base64)
        }
    })
},
// 小程序如何获取扫码的参数?
onLoad: function (options) {
    let scene = decodeURIComponent(options.scene)
    console.log(scene)
},

 

posted @ 2022-06-03 10:42  贝尔塔猫  阅读(1619)  评论(0)    收藏  举报